From 056b537a4ac1fb5ba423ef3033ad27a36520df90 Mon Sep 17 00:00:00 2001 From: Rohan Rose Date: Sun, 18 Oct 2026 18:54:29 +0000 Subject: [PATCH 1/4] fix: close realtime connection on logout to prevent ghost sessions EmbeddedChatApi.logout() only cleared the auth token via auth.logout(). The DDP connection and its active subscriptions were left running, so the client kept receiving live messages and events for a session that was no longer authenticated. logout() now tears the connection down via the existing close() method. The teardown runs in a finally block so the socket is also closed when the logout request itself fails, and close() errors are logged rather than propagated so they cannot mask the logout result. Fixes #1158 --- packages/api/src/EmbeddedChatApi.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 6f2408414..8ff5f7043 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -231,6 +231,16 @@ export default class EmbeddedChatApi { } catch (err: any) { console.error(err instanceof Error ? err.message : err); return err; + } finally { + // Tear down the realtime connection even if the logout request + // failed, otherwise the socket stays subscribed to the room and + // keeps receiving messages for a session that is no longer + // authenticated. + try { + await this.close(); + } catch (err: any) { + console.error(err instanceof Error ? err.message : err); + } } } From dceb7938879ed712733f7dda38ed1ddef603fe1a Mon Sep 17 00:00:00 2001 From: Rohan Rose Date: Sun, 25 Oct 2026 17:56:05 +0000 Subject: [PATCH 2/4] fix: cancel in-flight connect on logout to prevent ghost subscriptions close() now invalidates a connect() which is still awaiting, so that subscriptions are not registered after the socket has been torn down. Previously, if logout happened while _doConnect() was awaiting joinRoom() (or the connection/login steps), the connect would resume afterwards and register the stream subscriptions on the closed connection, which the DDP client replays on the next connection. Addresses review feedback on #1394. --- packages/api/src/EmbeddedChatApi.ts | 31 +++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 8ff5f7043..05d62f780 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -22,6 +22,9 @@ export default class EmbeddedChatApi { auth: RocketChatAuth; private _connectPromise: Promise | null = null; private _activeSubscriptions: { stop: () => void }[] = []; + // Incremented by close() so that a connect() which is still awaiting + // can detect that it has been superseded or cancelled. + private _connectionGeneration = 0; private _authListener: ((user: any) => void) | null = null; constructor( @@ -283,16 +286,27 @@ export default class EmbeddedChatApi { } private async _doConnect(isChannelPrivate = false) { + // Take a snapshot of the generation: every close() invalidates it, so + // a connect() which is interrupted while awaiting (for example by a + // logout happening during joinRoom()) can stop before subscribing. + // Without this the subscriptions would be created after the socket + // was torn down, and the DDP client would replay them on reconnect. + this.close(); + const generation = this._connectionGeneration; + const isStale = () => generation !== this._connectionGeneration; try { - this.close(); // before connection, all previous subscriptions should be cancelled await this.sdk.connection.connect(); + if (isStale()) return; const currentUser = (await this.auth.getCurrentUser()) as any; + if (isStale()) return; const token = currentUser?.authToken || currentUser?.data?.authToken; if (token) { this._applyCredentials(currentUser); await this.sdk.account.loginWithToken(token); + if (isStale()) return; await this.joinRoom(isChannelPrivate); + if (isStale()) return; } // Subscribe to room messages @@ -369,6 +383,12 @@ export default class EmbeddedChatApi { ); this._activeSubscriptions.push(notifyUserUiSub); } + + // A close() may have happened while the subscriptions above were + // being registered, so drop them if this connect is no longer current. + if (isStale()) { + this._stopSubscriptions(); + } } catch (err) { this.close(); } @@ -561,9 +581,16 @@ export default class EmbeddedChatApi { } } - async close() { + private _stopSubscriptions() { this._activeSubscriptions.forEach((sub) => sub.stop()); this._activeSubscriptions = []; + } + + async close() { + // Invalidate any connect() which is currently in flight, so that it + // does not register subscriptions after this teardown. + this._connectionGeneration += 1; + this._stopSubscriptions(); this.sdk.connection.close(); } From d837c795e5fab7c5eb3a8f0e186c34104c1f51b3 Mon Sep 17 00:00:00 2001 From: Rohan Rose Date: Thu, 12 Nov 2026 18:06:00 +0000 Subject: [PATCH 3/4] fix: release cancelled connection handshakes --- packages/api/package.json | 3 +- packages/api/src/EmbeddedChatApi.ts | 48 ++++++--- .../api/test/connection-lifecycle.test.mjs | 97 +++++++++++++++++++ yarn.lock | 1 + 4 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 packages/api/test/connection-lifecycle.test.mjs diff --git a/packages/api/package.json b/packages/api/package.json index 5eeffcbc1..8990fd372 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -7,7 +7,7 @@ "typings": "dist/index.d.ts", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "yarn build && yarn esbuild src/EmbeddedChatApi.ts --bundle --platform=node --format=esm --main-fields=module,main --outfile=dist/EmbeddedChatApi.test.mjs && node --test test/*.test.mjs", "build": "rollup -c", "dev": "yarn parcel playground/index.html", "format": "prettier --write 'src/'", @@ -16,6 +16,7 @@ "author": "", "license": "ISC", "devDependencies": { + "esbuild": "^0.17.19", "parcel": "^2.10.3", "rollup": "^3.23.0", "rollup-plugin-dts": "^6.0.1", diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 05d62f780..78b291a0d 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -21,6 +21,7 @@ export default class EmbeddedChatApi { typingUsers: string[]; auth: RocketChatAuth; private _connectPromise: Promise | null = null; + private _cancelConnect: (() => void) | null = null; private _activeSubscriptions: { stop: () => void }[] = []; // Incremented by close() so that a connect() which is still awaiting // can detect that it has been superseded or cancelled. @@ -256,10 +257,30 @@ export default class EmbeddedChatApi { if (this._connectPromise) { return this._connectPromise; } - this._connectPromise = this._doConnect(isChannelPrivate).finally(() => { - this._connectPromise = null; + + // Cancel previous subscriptions and snapshot the new connection generation + // before registering this attempt's cancellation callback. + this.close(); + const generation = this._connectionGeneration; + let cancelConnect: () => void; + const cancelled = new Promise((resolve) => { + cancelConnect = resolve; }); - return this._connectPromise; + this._cancelConnect = cancelConnect!; + + const connectPromise = Promise.race([ + this._doConnect(generation, isChannelPrivate), + cancelled, + ]).finally(() => { + // A cancelled handshake may finish after a newer connect() has started. + // Do not let its cleanup clear the newer attempt. + if (this._connectPromise === connectPromise) { + this._connectPromise = null; + this._cancelConnect = null; + } + }); + this._connectPromise = connectPromise; + return connectPromise; } private _normalizeMessage(data: any) { @@ -285,14 +306,10 @@ export default class EmbeddedChatApi { } } - private async _doConnect(isChannelPrivate = false) { - // Take a snapshot of the generation: every close() invalidates it, so - // a connect() which is interrupted while awaiting (for example by a - // logout happening during joinRoom()) can stop before subscribing. - // Without this the subscriptions would be created after the socket - // was torn down, and the DDP client would replay them on reconnect. - this.close(); - const generation = this._connectionGeneration; + private async _doConnect(generation: number, isChannelPrivate = false) { + // Every close() invalidates this generation, so a connect() interrupted + // while awaiting can stop before subscribing. The cancellation promise in + // connect() separately releases callers if the DDP handshake never settles. const isStale = () => generation !== this._connectionGeneration; try { await this.sdk.connection.connect(); @@ -587,10 +604,15 @@ export default class EmbeddedChatApi { } async close() { - // Invalidate any connect() which is currently in flight, so that it - // does not register subscriptions after this teardown. + // Invalidate and release any connect() currently waiting on a DDP + // handshake. The underlying promise may remain pending in the DDP client, + // but it can no longer block a subsequent connect(). this._connectionGeneration += 1; this._stopSubscriptions(); + const cancelConnect = this._cancelConnect; + this._cancelConnect = null; + this._connectPromise = null; + cancelConnect?.(); this.sdk.connection.close(); } diff --git a/packages/api/test/connection-lifecycle.test.mjs b/packages/api/test/connection-lifecycle.test.mjs new file mode 100644 index 000000000..caca880e6 --- /dev/null +++ b/packages/api/test/connection-lifecycle.test.mjs @@ -0,0 +1,97 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import EmbeddedChatApi from "../dist/EmbeddedChatApi.test.mjs"; + +const withTimeout = (promise, message) => + Promise.race([ + promise, + new Promise((_, reject) => + setTimeout(() => reject(new Error(message)), 250) + ), + ]); + +const createApi = () => { + const api = Object.create(EmbeddedChatApi.prototype); + let connectCalls = 0; + let loginCalls = 0; + let currentUser = { authToken: "old-token", userId: "old-user" }; + let resolveFirstHandshake; + const firstHandshake = new Promise((resolve) => { + resolveFirstHandshake = resolve; + }); + + Object.assign(api, { + rid: "room-id", + _connectPromise: null, + _cancelConnect: null, + _activeSubscriptions: [], + _connectionGeneration: 0, + onMessageCallbacks: [], + onMessageDeleteCallbacks: [], + onTypingStatusCallbacks: [], + onActionTriggeredCallbacks: [], + onUiInteractionCallbacks: [], + typingUsers: [], + auth: { + logout: async () => { + currentUser = null; + }, + getCurrentUser: async () => currentUser, + }, + joinRoom: async () => undefined, + sdk: { + connection: { + connect: () => { + connectCalls += 1; + return connectCalls === 1 ? firstHandshake : Promise.resolve(); + }, + close: () => undefined, + }, + account: { + uid: null, + loginWithToken: async () => { + loginCalls += 1; + }, + }, + rest: { setCredentials: () => undefined }, + stream: () => ({ stop: () => undefined }), + }, + }); + + return { + api, + getConnectCalls: () => connectCalls, + getLoginCalls: () => loginCalls, + login: () => { + currentUser = { authToken: "new-token", userId: "new-user" }; + }, + resolveFirstHandshake, + }; +}; + +test("logout during handshake allows the next login to reconnect", async () => { + const { api, getConnectCalls, getLoginCalls, login, resolveFirstHandshake } = + createApi(); + + const interruptedConnect = api.connect(); + assert.equal(getConnectCalls(), 1); + + await api.logout(); + await withTimeout( + interruptedConnect, + "the interrupted connection did not settle" + ); + + login(); + await withTimeout(api.connect(), "the next connection did not settle"); + assert.equal(getConnectCalls(), 2); + assert.equal(getLoginCalls(), 1); + + // Let the abandoned DDP promise settle and verify its finalizer does not + // interfere with later connection attempts. + resolveFirstHandshake(); + await Promise.resolve(); + await withTimeout(api.connect(), "a later connection did not settle"); + assert.equal(getConnectCalls(), 3); +}); diff --git a/yarn.lock b/yarn.lock index 23688eafe..db1acbfed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2456,6 +2456,7 @@ __metadata: "@embeddedchat/auth": "workspace:^" "@rocket.chat/ddp-client": 1.0.9-rc.2 "@rocket.chat/emitter": ^0.31.25 + esbuild: ^0.17.19 parcel: ^2.10.3 rollup: ^3.23.0 rollup-plugin-dts: ^6.0.1 From ae81fd262dad1fb576b00eb2e13b5c0334910795 Mon Sep 17 00:00:00 2001 From: Rohan Rose Date: Sun, 15 Nov 2026 14:16:40 +0000 Subject: [PATCH 4/4] Fix cancelled connection attempts --- packages/api/src/EmbeddedChatApi.ts | 12 ++++++++++-- packages/api/test/connection-lifecycle.test.mjs | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 78b291a0d..ce5faccb7 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -9,6 +9,14 @@ import { // mutliple typing status can come at the same time they should be processed in order. let typingHandlerLock = 0; + +class ConnectionCancelledError extends Error { + constructor() { + super("Connection attempt was cancelled"); + this.name = "ConnectionCancelledError"; + } +} + export default class EmbeddedChatApi { host: string; rid: string; @@ -263,8 +271,8 @@ export default class EmbeddedChatApi { this.close(); const generation = this._connectionGeneration; let cancelConnect: () => void; - const cancelled = new Promise((resolve) => { - cancelConnect = resolve; + const cancelled = new Promise((_, reject) => { + cancelConnect = () => reject(new ConnectionCancelledError()); }); this._cancelConnect = cancelConnect!; diff --git a/packages/api/test/connection-lifecycle.test.mjs b/packages/api/test/connection-lifecycle.test.mjs index caca880e6..6235643c8 100644 --- a/packages/api/test/connection-lifecycle.test.mjs +++ b/packages/api/test/connection-lifecycle.test.mjs @@ -78,9 +78,9 @@ test("logout during handshake allows the next login to reconnect", async () => { assert.equal(getConnectCalls(), 1); await api.logout(); - await withTimeout( - interruptedConnect, - "the interrupted connection did not settle" + await assert.rejects( + withTimeout(interruptedConnect, "the interrupted connection did not settle"), + { name: "ConnectionCancelledError" } ); login();