From b3e6f4b4947750dae00e5f98d4add610502734e4 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 3 Mar 2022 16:27:43 +0100 Subject: [PATCH] Stop the token refresher when disposing the client --- src/matrix/Client.js | 13 ++++++++----- src/matrix/net/OidcApi.ts | 4 +++- src/matrix/net/TokenRefresher.ts | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 7d0d87a0..d0530d1f 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -269,8 +269,7 @@ export class Client { crypto: this._platform.crypto, }); - // TODO: stop/pause the refresher? - const tokenRefresher = new TokenRefresher({ + this._tokenRefresher = new TokenRefresher({ oidcApi, clock: this._platform.clock, accessToken: sessionInfo.accessToken, @@ -279,13 +278,13 @@ export class Client { anticipation: 30 * 1000, }); - tokenRefresher.token.subscribe(t => { + this._tokenRefresher.token.subscribe(t => { this._platform.sessionInfoStorage.updateToken(sessionInfo.id, t.accessToken, t.accessTokenExpiresAt, t.refreshToken); }); - await tokenRefresher.start(); + await this._tokenRefresher.start(); - accessToken = tokenRefresher.accessToken; + accessToken = this._tokenRefresher.accessToken; } else { accessToken = new ObservableValue(sessionInfo.accessToken); } @@ -500,6 +499,10 @@ export class Client { this._sync.stop(); this._sync = null; } + if (this._tokenRefresher) { + this._tokenRefresher.stop(); + this._tokenRefresher = null; + } if (this._session) { this._session.dispose(); this._session = null; diff --git a/src/matrix/net/OidcApi.ts b/src/matrix/net/OidcApi.ts index f7c08dca..023ba485 100644 --- a/src/matrix/net/OidcApi.ts +++ b/src/matrix/net/OidcApi.ts @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import type {RequestFunction} from "../../platform/types/types"; + const WELL_KNOWN = ".well-known/openid-configuration"; const RANDOM_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; @@ -53,7 +55,7 @@ function assert(condition: any, message: string): asserts condition { export class OidcApi { _issuer: string; _clientId: string; - _requestFn: any; + _requestFn: RequestFunction; _encoding: any; _crypto: any; _metadataPromise: Promise; diff --git a/src/matrix/net/TokenRefresher.ts b/src/matrix/net/TokenRefresher.ts index 489dfb11..2010cebe 100644 --- a/src/matrix/net/TokenRefresher.ts +++ b/src/matrix/net/TokenRefresher.ts @@ -32,6 +32,7 @@ export class TokenRefresher { private _clock: Clock; private _oidcApi: OidcApi; private _timeout: Timeout + private _running: boolean; constructor({ oidcApi, @@ -65,11 +66,15 @@ export class TokenRefresher { await this.renew(); } + this._running = true; this._renewingLoop(); } stop() { - // TODO + this._running = false; + if (this._timeout) { + this._timeout.dispose(); + } } get needsRenewing() { @@ -79,14 +84,19 @@ export class TokenRefresher { } async _renewingLoop() { - while (true) { + while (this._running) { const remaining = this._token.get().accessTokenExpiresAt - this._clock.now(); const anticipated = remaining - this._anticipation; if (anticipated > 0) { this._timeout = this._clock.createTimeout(anticipated); - await this._timeout.elapsed(); + try { + await this._timeout.elapsed(); + } catch { + // The timeout will throw when aborted, so stop the loop if it is the case + return; + } } await this.renew();