Skip to content

Commit 0a14a20

Browse files
committed
Fix using signaling settings while being refetched
When the external signaling server returns the error "token_expired" the signaling settings are fetched again. However, if there are further requests and "token_expired" is returned again the previous fetch is canceled and a new one started instead, which caused the settings to be temporary set to "null". The signaling settings are expected to always be an object, so setting them to "null" could cause an error if they were used during that time (for example, during a reconnection, which caused the signaling object to "hang" and not do any further connection attempt). Preventing the settings to be nullified is only half of the story, though; "token_expired" can be thrown when trying to connect, and errors thrown when trying to connect cause a reconnection attempt. This could end in a reconnection loop, as each "token_expired" error would cause another fetch of the settings, cancelling the previous fetch and thus preventing the settings to be updated, and as the previous settings would be still used any connection attempt would end again in another "token_expired" error. To solve all that now any connection attempt done after receiving a "token_expired" error is deferred until the signaling settings were updated. Note that the previous signaling settings are kept to ensure that a signaling object is always available, even if outdated. However, any usage of the outdated signaling settings is expected to cause a "token_expired" error to be thrown; right now that only happens during connections, so only that code needs to wait for the settings to be fetched again. Signed-off-by: Daniel Calviño Sánchez <[email protected]>
1 parent c7e681c commit 0a14a20

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/utils/signaling.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ Signaling.Base.prototype._trigger = function(ev, args) {
141141
EventBus.$emit('signaling-' + kebabCase(ev), args)
142142
}
143143

144+
Signaling.Base.prototype.setSettings = function(settings) {
145+
if (!settings) {
146+
// Signaling object is expected to always have a settings object
147+
return
148+
}
149+
150+
this.settings = settings
151+
152+
if (this._pendingUpdateSettingsPromise) {
153+
this._pendingUpdateSettingsPromise.resolve()
154+
delete this._pendingUpdateSettingsPromise
155+
}
156+
}
157+
144158
Signaling.Base.prototype.isNoMcuWarningEnabled = function() {
145159
return !this.settings.hideWarning
146160
}
@@ -654,6 +668,19 @@ Signaling.Standalone.prototype.connect = function() {
654668
}, 2000)
655669
}
656670

671+
if (this._pendingUpdateSettingsPromise) {
672+
console.info('Deferring establishing signaling connection until signaling settings are updated')
673+
674+
this._pendingUpdateSettingsPromise.then(() => {
675+
// "reconnect()" is called instead of "connect()", even if that
676+
// slightly delays the connection, as "reconnect()" prevents
677+
// duplicated connection requests.
678+
this.reconnect()
679+
})
680+
681+
return
682+
}
683+
657684
console.debug('Connecting to ' + this.url + ' for ' + this.settings.token)
658685
this.callbacks = {}
659686
this.id = 1
@@ -1423,6 +1450,17 @@ Signaling.Standalone.prototype.processRoomParticipantsEvent = function(data) {
14231450
Signaling.Standalone.prototype.processErrorTokenExpired = function() {
14241451
console.info('The signaling token is expired, need to update settings')
14251452

1453+
if (!this._pendingUpdateSettingsPromise) {
1454+
let pendingUpdateSettingsPromiseResolve
1455+
this._pendingUpdateSettingsPromise = new Promise((resolve, reject) => {
1456+
// The Promise executor is run even before the Promise constructor has
1457+
// finished, so "this._pendingUpdateSettingsPromise" is not available
1458+
// yet.
1459+
pendingUpdateSettingsPromiseResolve = resolve
1460+
})
1461+
this._pendingUpdateSettingsPromise.resolve = pendingUpdateSettingsPromiseResolve
1462+
}
1463+
14261464
this._trigger('updateSettings')
14271465
}
14281466

src/utils/webrtc/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async function connectSignaling(token) {
134134
signaling.on('updateSettings', async function() {
135135
const settings = await getSignalingSettings(token)
136136
console.debug('Received updated settings', settings)
137-
signaling.settings = settings
137+
signaling.setSettings(settings)
138138
})
139139

140140
signalingTypingHandler?.setSignaling(signaling)

0 commit comments

Comments
 (0)