From 01323bd5da6d19964e5162b4a169c048522e7df0 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Fri, 23 Oct 2026 10:54:49 -0300 Subject: [PATCH 01/14] fix: retry the socket when the first connect attempt fails A plain open that fails schedules no reopen in the SDK, so a cold start without network stayed on "Connecting..." with a stale room list until the next foreground. Hand the failure to a forced attempt, which enters the SDK's reopen loop, unless the connect() was already superseded. --- .../__tests__/connect.integration.test.ts | 42 +++++++++++++++++++ app/lib/services/connect.ts | 6 +++ 2 files changed, 48 insertions(+) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 496849ce846..918d33ac390 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -177,6 +177,48 @@ describe('connect() over the real SDK', () => { expect(successCount()).toBe(before); }); + + it('keeps retrying when the first attempt fails and connects once the network is back', async () => { + redux.state.login.user = { token: 'stored-token' }; + + await connect({ server: 'https://example.com' }); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(1); + mockConnections[0].onerror(); + await flushMicrotasksAndTimers(); + + expect(mockConnections).toHaveLength(2); + mockConnections[1].onerror(); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(2); + + await jest.runOnlyPendingTimersAsync(); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(3); + mockConnections[2].onopen(); + await flushMicrotasksAndTimers(); + + expect(redux.store.dispatch).toHaveBeenCalledWith(connectSuccess()); + expect(redux.store.dispatch).toHaveBeenCalledWith(loginRequest({ resume: 'stored-token' }, false)); + }); + + it('does not retry an attempt that a later connect() replaced', async () => { + await connect({ server: 'https://a.example.com' }); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(1); + + await connect({ server: 'https://b.example.com' }); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(2); + expect(mockConnections[0].close).toHaveBeenCalled(); + + mockConnections[1].onopen(); + await flushMicrotasksAndTimers(); + await jest.runOnlyPendingTimersAsync(); + await flushMicrotasksAndTimers(); + + expect(mockConnections).toHaveLength(2); + }); }); describe('login() over the real SDK', () => { diff --git a/app/lib/services/connect.ts b/app/lib/services/connect.ts index 950fd2ff9ad..d3b2b00461a 100644 --- a/app/lib/services/connect.ts +++ b/app/lib/services/connect.ts @@ -93,6 +93,7 @@ function connect({ server, logoutOnError = false }: { server: string; logoutOnEr sdk.initialize(server); getSettings(server); + const { driver } = sdk; sdk .connect() .then(() => { @@ -100,6 +101,11 @@ function connect({ server, logoutOnError = false }: { server: string; logoutOnEr }) .catch((err: unknown) => { console.log('connect error', err); + // A failed plain open schedules no Reopen; a forced attempt enters the SDK's reopen loop. + // Skipped when this connect() was already superseded. + if (driver && sdk.driver === driver) { + driver.reopenNow().catch(() => {}); + } }); connectingListener = sdk.onStreamData('connecting', () => { From 4fac4ab736f35a01243263727c08fdd9d6bc4de7 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Fri, 23 Oct 2026 17:05:57 -0300 Subject: [PATCH 02/14] test: make the superseded-connect retry test depend on the driver guard The replaced transport never settled its close, so reopenNow() was rejected regardless of the guard. Let the mock answer close() so the old driver would reopen without it. --- app/lib/services/__tests__/connect.integration.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 918d33ac390..bad499c949d 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -206,6 +206,11 @@ describe('connect() over the real SDK', () => { await connect({ server: 'https://a.example.com' }); await flushMicrotasksAndTimers(); expect(mockConnections).toHaveLength(1); + const replaced = mockConnections[0]; + replaced.close.mockImplementation(() => { + replaced.readyState = 3; + replaced.onclose({ code: 1000 }); + }); await connect({ server: 'https://b.example.com' }); await flushMicrotasksAndTimers(); From 649457af41de5b83b39b2ee161141827788c611b Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Mon, 26 Oct 2026 18:11:12 -0300 Subject: [PATCH 03/14] cleanup --- app/lib/services/__tests__/connect.integration.test.ts | 3 --- app/lib/services/connect.ts | 1 - 2 files changed, 4 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index bad499c949d..32df14f61b6 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -179,8 +179,6 @@ describe('connect() over the real SDK', () => { }); it('keeps retrying when the first attempt fails and connects once the network is back', async () => { - redux.state.login.user = { token: 'stored-token' }; - await connect({ server: 'https://example.com' }); await flushMicrotasksAndTimers(); expect(mockConnections).toHaveLength(1); @@ -199,7 +197,6 @@ describe('connect() over the real SDK', () => { await flushMicrotasksAndTimers(); expect(redux.store.dispatch).toHaveBeenCalledWith(connectSuccess()); - expect(redux.store.dispatch).toHaveBeenCalledWith(loginRequest({ resume: 'stored-token' }, false)); }); it('does not retry an attempt that a later connect() replaced', async () => { diff --git a/app/lib/services/connect.ts b/app/lib/services/connect.ts index d3b2b00461a..e8c0dcf1111 100644 --- a/app/lib/services/connect.ts +++ b/app/lib/services/connect.ts @@ -102,7 +102,6 @@ function connect({ server, logoutOnError = false }: { server: string; logoutOnEr .catch((err: unknown) => { console.log('connect error', err); // A failed plain open schedules no Reopen; a forced attempt enters the SDK's reopen loop. - // Skipped when this connect() was already superseded. if (driver && sdk.driver === driver) { driver.reopenNow().catch(() => {}); } From cd8c32d1336a31b153109659ce91c87fd7a4d02f Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Mon, 26 Oct 2026 18:22:20 -0300 Subject: [PATCH 04/14] cleanup --- app/lib/services/__tests__/connect.integration.test.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 32df14f61b6..9a6aa4b2ffa 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -212,14 +212,6 @@ describe('connect() over the real SDK', () => { await connect({ server: 'https://b.example.com' }); await flushMicrotasksAndTimers(); expect(mockConnections).toHaveLength(2); - expect(mockConnections[0].close).toHaveBeenCalled(); - - mockConnections[1].onopen(); - await flushMicrotasksAndTimers(); - await jest.runOnlyPendingTimersAsync(); - await flushMicrotasksAndTimers(); - - expect(mockConnections).toHaveLength(2); }); }); From 4bb8179482a0e26ffc5b532112b30a3665fbeaa5 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 11:17:54 -0300 Subject: [PATCH 05/14] fix: let the SDK reopen after a failed connect instead of forcing a retry --- .../__tests__/connect.integration.test.ts | 21 +++++++++++++++++++ app/lib/services/connect.ts | 5 ----- app/lib/services/sdk.ts | 8 ++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 9a6aa4b2ffa..ff87a34db27 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -184,7 +184,10 @@ describe('connect() over the real SDK', () => { expect(mockConnections).toHaveLength(1); mockConnections[0].onerror(); await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(1); + await jest.runOnlyPendingTimersAsync(); + await flushMicrotasksAndTimers(); expect(mockConnections).toHaveLength(2); mockConnections[1].onerror(); await flushMicrotasksAndTimers(); @@ -199,6 +202,24 @@ describe('connect() over the real SDK', () => { expect(redux.store.dispatch).toHaveBeenCalledWith(connectSuccess()); }); + it('lets a socket recovery after a failed first attempt open a fresh connection immediately', async () => { + await connect({ server: 'https://example.com' }); + await flushMicrotasksAndTimers(); + mockConnections[0].onerror(); + await flushMicrotasksAndTimers(); + + const before = mockConnections.length; + + const recovering = sdk.driver!.reopenNow(); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(before + 1); + mockConnections[before].onopen(); + await flushMicrotasksAndTimers(); + + await expect(recovering).resolves.toBeUndefined(); + expect(redux.store.dispatch).toHaveBeenCalledWith(connectSuccess()); + }); + it('does not retry an attempt that a later connect() replaced', async () => { await connect({ server: 'https://a.example.com' }); await flushMicrotasksAndTimers(); diff --git a/app/lib/services/connect.ts b/app/lib/services/connect.ts index e8c0dcf1111..950fd2ff9ad 100644 --- a/app/lib/services/connect.ts +++ b/app/lib/services/connect.ts @@ -93,7 +93,6 @@ function connect({ server, logoutOnError = false }: { server: string; logoutOnEr sdk.initialize(server); getSettings(server); - const { driver } = sdk; sdk .connect() .then(() => { @@ -101,10 +100,6 @@ function connect({ server, logoutOnError = false }: { server: string; logoutOnEr }) .catch((err: unknown) => { console.log('connect error', err); - // A failed plain open schedules no Reopen; a forced attempt enters the SDK's reopen loop. - if (driver && sdk.driver === driver) { - driver.reopenNow().catch(() => {}); - } }); connectingListener = sdk.onStreamData('connecting', () => { diff --git a/app/lib/services/sdk.ts b/app/lib/services/sdk.ts index 1d3b8d4106f..2ee756e95c0 100644 --- a/app/lib/services/sdk.ts +++ b/app/lib/services/sdk.ts @@ -41,7 +41,13 @@ class Sdk { private initializeSdk(server: string): Rocketchat { // The app can't reconnect if reopen interval is 5s while in development - return new Rocketchat({ host: server, protocol: 'ddp', useSsl: isSsl(server), reopen: __DEV__ ? 20000 : 5000 }); + return new Rocketchat({ + host: server, + protocol: 'ddp', + useSsl: isSsl(server), + reopen: __DEV__ ? 20000 : 5000, + reopenAfterFailedOpen: true + }); } initialize(server: string): void { From 4eeb2be58c1dceea87ba1b7f25554588f04bda6b Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 12:24:55 -0300 Subject: [PATCH 06/14] test: make the replaced-connect test wait out the reopen delay --- .../services/__tests__/connect.integration.test.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index ff87a34db27..2c0c37958f2 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -223,15 +223,14 @@ describe('connect() over the real SDK', () => { it('does not retry an attempt that a later connect() replaced', async () => { await connect({ server: 'https://a.example.com' }); await flushMicrotasksAndTimers(); - expect(mockConnections).toHaveLength(1); - const replaced = mockConnections[0]; - replaced.close.mockImplementation(() => { - replaced.readyState = 3; - replaced.onclose({ code: 1000 }); - }); + mockConnections[0].onerror(); + await flushMicrotasksAndTimers(); await connect({ server: 'https://b.example.com' }); await flushMicrotasksAndTimers(); + await jest.runOnlyPendingTimersAsync(); + await flushMicrotasksAndTimers(); + expect(mockConnections).toHaveLength(2); }); }); From bccf17405368831c1bb4b0970b86b797bde21b0d Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 12:33:55 -0300 Subject: [PATCH 07/14] chore: pin the sdk to the commit that reopens after a failed open --- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 178452290be..af47d5c07dc 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "@rocket.chat/media-signaling": "1.0.0-rc.1", "@rocket.chat/message-parser": "0.31.36", "@rocket.chat/mobile-crypto": "RocketChat/rocket.chat-mobile-crypto#main", - "@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#59708d7b48aace43ef65b16de101fc12a96b69aa", + "@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#03b1d58652d826526ef026bbd69f8b7aae9726c5", "@rocket.chat/ui-kit": "^0.39.0", "@zoontek/react-native-navigation-bar": "^1.1.1", "axios": "0.30.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a7f33c1ee9..e5e8fd71e22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -102,8 +102,8 @@ importers: specifier: RocketChat/rocket.chat-mobile-crypto#main version: https://codeload.github.com/RocketChat/rocket.chat-mobile-crypto/tar.gz/69a0a250dd7c6ff0808eb659d7202be1cae7fa1c(react-native@0.86.3(@babel/core@7.29.7)(@react-native-community/cli@20.0.0(typescript@7.0.2))(@react-native/jest-preset@0.86.3(@babel/core@7.29.7)(react@19.2.3))(@react-native/metro-config@0.86.3(@babel/core@7.29.7))(@types/react@19.2.18)(react@19.2.3))(react@19.2.3) '@rocket.chat/sdk': - specifier: RocketChat/Rocket.Chat.js.SDK#59708d7b48aace43ef65b16de101fc12a96b69aa - version: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/59708d7b48aace43ef65b16de101fc12a96b69aa + specifier: RocketChat/Rocket.Chat.js.SDK#03b1d58652d826526ef026bbd69f8b7aae9726c5 + version: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5 '@rocket.chat/ui-kit': specifier: ^0.39.0 version: 0.39.0(@rocket.chat/icons@0.47.0)(@types/node@25.0.3)(typescript@7.0.2) @@ -3437,8 +3437,8 @@ packages: react: '*' react-native: '*' - '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/59708d7b48aace43ef65b16de101fc12a96b69aa': - resolution: {gitHosted: true, integrity: sha512-G6Jllr3C5VKJzPnlbiJshwx3QxgOcc70esEFRfdM/37ECPG9P46SRdkGQW8yi2bWgPJh1Dnh/vSwSRm2k48THQ==, tarball: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/59708d7b48aace43ef65b16de101fc12a96b69aa} + '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5': + resolution: {gitHosted: true, integrity: sha512-/RfCIwc9yeaLNbPwyl7knNNHdZHvS3t4uZTTI6NBw91LMgBa6RZVXlrSSTwDXufmxa96/HdCcQ7JtyfTxsN5nw==, tarball: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5} version: 2.0.0-mobile '@rocket.chat/ui-kit@0.39.0': @@ -12568,7 +12568,7 @@ snapshots: react: 19.2.3 react-native: 0.86.3(@babel/core@7.29.7)(@react-native-community/cli@20.0.0(typescript@7.0.2))(@react-native/jest-preset@0.86.3(@babel/core@7.29.7)(react@19.2.3))(@react-native/metro-config@0.86.3(@babel/core@7.29.7))(@types/react@19.2.18)(react@19.2.3) - '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/59708d7b48aace43ef65b16de101fc12a96b69aa': + '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5': dependencies: js-sha256: 0.9.0 tiny-events: 1.0.1 From d4537274a23196bb9eefbc5b09a9ef06118deb8f Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 12:51:58 -0300 Subject: [PATCH 08/14] fix: abort a client's requests when it is disconnected so a pending login can't reopen it --- .../__tests__/connect.integration.test.ts | 30 ++++++++++++++++++- app/lib/services/__tests__/connect.test.ts | 2 -- app/lib/services/connect.ts | 10 ------- app/lib/services/sdk.ts | 5 +--- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 2c0c37958f2..2b29fdfa746 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -1,6 +1,6 @@ jest.unmock('@rocket.chat/sdk'); -import { connect, login, loginWithPassword } from '../connect'; +import { connect, disconnect, login, loginWithPassword } from '../connect'; import sdk from '../sdk'; import { initStore } from '~/lib/store/auxStore'; import { connectRequest, connectSuccess, disconnect as disconnectAction } from '~/actions/connect'; @@ -233,6 +233,34 @@ describe('connect() over the real SDK', () => { expect(mockConnections).toHaveLength(2); }); + + it('does not reopen a disconnected client whose login was still in flight', async () => { + let answerLogin = () => {}; + global.fetch = jest.fn( + (_url: unknown, options?: { signal?: AbortSignal }) => + new Promise((resolve, reject) => { + options?.signal?.addEventListener('abort', () => reject(new Error('aborted'))); + answerLogin = () => + resolve({ + status: 200, + json: () => + Promise.resolve({ status: 'success', data: { userId: 'user-id', authToken: 'auth-token', me: REST_LOGIN_ME } }) + }); + }) + ) as unknown as typeof fetch; + await connect({ server: 'https://example.com' }); + await flushMicrotasksAndTimers(); + mockConnections[0].onerror(); + await flushMicrotasksAndTimers(); + login({ user: 'the-user', password: 'secret' }).catch(() => null); + await flushMicrotasksAndTimers(); + + disconnect(); + answerLogin(); + await flushMicrotasksAndTimers(); + + expect(mockConnections).toHaveLength(1); + }); }); describe('login() over the real SDK', () => { diff --git a/app/lib/services/__tests__/connect.test.ts b/app/lib/services/__tests__/connect.test.ts index 3909e1634a9..3f58ac8ef3e 100644 --- a/app/lib/services/__tests__/connect.test.ts +++ b/app/lib/services/__tests__/connect.test.ts @@ -22,7 +22,6 @@ const mockOnStreamData = jest.fn, [string, (...args return Promise.resolve({ stop }); }); const mockSdkConnect = jest.fn, []>(() => Promise.resolve()); -const mockSdkAbort = jest.fn(); const mockSdkDisconnect = jest.fn(); const mockSdkLogin = jest.fn, [unknown]>(() => Promise.resolve()); const mockSdkCurrent: Record = { @@ -41,7 +40,6 @@ jest.mock('../sdk', () => ({ await mockSdkLogin(credentials); return mockSdkCurrent.currentLogin ?? null; }, - abort: () => mockSdkAbort(), get currentLogin() { return mockSdkCurrent.currentLogin; } diff --git a/app/lib/services/connect.ts b/app/lib/services/connect.ts index b47ecd7053b..5316b8b2ae0 100644 --- a/app/lib/services/connect.ts +++ b/app/lib/services/connect.ts @@ -59,9 +59,6 @@ let logoutListener: any; function connect({ server, logoutOnError = false }: { server: string; logoutOnError?: boolean }): Promise { return new Promise(resolve => { - // Check for running requests and abort them before connecting to the server - abort(); - disconnect(); database.setActiveDB(server); @@ -413,12 +410,6 @@ async function loginOAuthOrSso(params: ILoginCredentials) { store.dispatch(loginRequest({ resume: result.token }, false)); } -function abort(): void { - if (sdk.isInitialized) { - sdk.abort(); - } -} - function disconnect(): void { sdk.disconnect(); mediaSessionInstance.reset(); @@ -519,7 +510,6 @@ export { loginTOTP, loginWithPassword, loginOAuthOrSso, - abort, connect, disconnect, getWebsocketInfo, diff --git a/app/lib/services/sdk.ts b/app/lib/services/sdk.ts index be955bb2300..3d8ab078471 100644 --- a/app/lib/services/sdk.ts +++ b/app/lib/services/sdk.ts @@ -82,16 +82,13 @@ class Sdk { return client.currentLogin ?? null; } - abort(): void { - this.activeSdk.abort(); - } - subscribeNotifyUser() { return this.activeSdk.subscribeNotifyUser(); } disconnect(): void { if (this.sdk) { + this.sdk.abort(); this.sdk.disconnect(); this.sdk = null; } From 62dbd7b90c1df9d5b1702bba17bc522438ef5a04 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 13:13:38 -0300 Subject: [PATCH 09/14] fix: clear a failed or pending login when a server is selected --- app/reducers/login.test.ts | 8 ++++++++ app/reducers/login.ts | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/app/reducers/login.test.ts b/app/reducers/login.test.ts index 35c270877ea..395b93b0b23 100644 --- a/app/reducers/login.test.ts +++ b/app/reducers/login.test.ts @@ -9,6 +9,7 @@ import { setLoginServices, setUser } from '../actions/login'; +import { selectServerRequest } from '../actions/server'; import { initialState } from './login'; import { createMockedStore, mockedStore } from './mockedStore'; @@ -30,6 +31,13 @@ describe('test selectedUsers reducer', () => { expect(state).toEqual('error'); }); + it('should not carry a failed login into the next selected server', () => { + mockedStore.dispatch(loginFailure({ error: 'error' })); + mockedStore.dispatch(selectServerRequest('https://other.example.com', '7.0.0')); + const { isFetching, failure, error } = mockedStore.getState().login; + expect({ isFetching, failure, error }).toEqual({ isFetching: false, failure: false, error: {} }); + }); + it('should return modified store after loginSuccess', () => { const user = { id: 'ajhsiahsa', diff --git a/app/reducers/login.ts b/app/reducers/login.ts index fd786147a97..079106d4f1c 100644 --- a/app/reducers/login.ts +++ b/app/reducers/login.ts @@ -78,6 +78,13 @@ export default function login(state = initialState, action: TActionsLogin): ILog }; case types.LOGOUT: return initialState; + case types.SERVER.SELECT_REQUEST: + return { + ...state, + isFetching: false, + failure: false, + error: {} + }; case types.USER.SET: if (isSubsetOf(action.user, state.user)) { return state; From 0da76347aaac3bcb6d30e85bfb72e2e429dd0439 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 13:13:38 -0300 Subject: [PATCH 10/14] test: share the REST login response --- .../__tests__/connect.integration.test.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 2b29fdfa746..6582a7ff4a8 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -83,6 +83,11 @@ const REST_LOGIN_ME = { requirePasswordChange: false }; +const restLoginResponse = () => ({ + status: 200, + json: () => Promise.resolve({ status: 'success', data: { userId: 'user-id', authToken: 'auth-token', me: REST_LOGIN_ME } }) +}); + let redux: ReturnType; let collections: Record>; @@ -103,11 +108,7 @@ beforeEach(() => { global.fetch = jest.fn((url: unknown) => { const target = String(url); if (target.includes('/api/v1/login')) { - return Promise.resolve({ - status: 200, - json: () => - Promise.resolve({ status: 'success', data: { userId: 'user-id', authToken: 'auth-token', me: REST_LOGIN_ME } }) - }); + return Promise.resolve(restLoginResponse()); } return Promise.resolve({ status: 200, json: () => Promise.resolve({ success: false }) }); }) as unknown as typeof fetch; @@ -240,12 +241,7 @@ describe('connect() over the real SDK', () => { (_url: unknown, options?: { signal?: AbortSignal }) => new Promise((resolve, reject) => { options?.signal?.addEventListener('abort', () => reject(new Error('aborted'))); - answerLogin = () => - resolve({ - status: 200, - json: () => - Promise.resolve({ status: 'success', data: { userId: 'user-id', authToken: 'auth-token', me: REST_LOGIN_ME } }) - }); + answerLogin = () => resolve(restLoginResponse()); }) ) as unknown as typeof fetch; await connect({ server: 'https://example.com' }); From 246c55c71baeafd16fffb207bcfc9778cc011e38 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 13:25:27 -0300 Subject: [PATCH 11/14] revert: keep the PR to the reopen after a failed connect --- .../__tests__/connect.integration.test.ts | 36 ++++--------------- app/lib/services/__tests__/connect.test.ts | 2 ++ app/lib/services/connect.ts | 10 ++++++ app/lib/services/sdk.ts | 5 ++- app/reducers/login.test.ts | 8 ----- app/reducers/login.ts | 7 ---- 6 files changed, 22 insertions(+), 46 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 6582a7ff4a8..2c0c37958f2 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -1,6 +1,6 @@ jest.unmock('@rocket.chat/sdk'); -import { connect, disconnect, login, loginWithPassword } from '../connect'; +import { connect, login, loginWithPassword } from '../connect'; import sdk from '../sdk'; import { initStore } from '~/lib/store/auxStore'; import { connectRequest, connectSuccess, disconnect as disconnectAction } from '~/actions/connect'; @@ -83,11 +83,6 @@ const REST_LOGIN_ME = { requirePasswordChange: false }; -const restLoginResponse = () => ({ - status: 200, - json: () => Promise.resolve({ status: 'success', data: { userId: 'user-id', authToken: 'auth-token', me: REST_LOGIN_ME } }) -}); - let redux: ReturnType; let collections: Record>; @@ -108,7 +103,11 @@ beforeEach(() => { global.fetch = jest.fn((url: unknown) => { const target = String(url); if (target.includes('/api/v1/login')) { - return Promise.resolve(restLoginResponse()); + return Promise.resolve({ + status: 200, + json: () => + Promise.resolve({ status: 'success', data: { userId: 'user-id', authToken: 'auth-token', me: REST_LOGIN_ME } }) + }); } return Promise.resolve({ status: 200, json: () => Promise.resolve({ success: false }) }); }) as unknown as typeof fetch; @@ -234,29 +233,6 @@ describe('connect() over the real SDK', () => { expect(mockConnections).toHaveLength(2); }); - - it('does not reopen a disconnected client whose login was still in flight', async () => { - let answerLogin = () => {}; - global.fetch = jest.fn( - (_url: unknown, options?: { signal?: AbortSignal }) => - new Promise((resolve, reject) => { - options?.signal?.addEventListener('abort', () => reject(new Error('aborted'))); - answerLogin = () => resolve(restLoginResponse()); - }) - ) as unknown as typeof fetch; - await connect({ server: 'https://example.com' }); - await flushMicrotasksAndTimers(); - mockConnections[0].onerror(); - await flushMicrotasksAndTimers(); - login({ user: 'the-user', password: 'secret' }).catch(() => null); - await flushMicrotasksAndTimers(); - - disconnect(); - answerLogin(); - await flushMicrotasksAndTimers(); - - expect(mockConnections).toHaveLength(1); - }); }); describe('login() over the real SDK', () => { diff --git a/app/lib/services/__tests__/connect.test.ts b/app/lib/services/__tests__/connect.test.ts index 3f58ac8ef3e..3909e1634a9 100644 --- a/app/lib/services/__tests__/connect.test.ts +++ b/app/lib/services/__tests__/connect.test.ts @@ -22,6 +22,7 @@ const mockOnStreamData = jest.fn, [string, (...args return Promise.resolve({ stop }); }); const mockSdkConnect = jest.fn, []>(() => Promise.resolve()); +const mockSdkAbort = jest.fn(); const mockSdkDisconnect = jest.fn(); const mockSdkLogin = jest.fn, [unknown]>(() => Promise.resolve()); const mockSdkCurrent: Record = { @@ -40,6 +41,7 @@ jest.mock('../sdk', () => ({ await mockSdkLogin(credentials); return mockSdkCurrent.currentLogin ?? null; }, + abort: () => mockSdkAbort(), get currentLogin() { return mockSdkCurrent.currentLogin; } diff --git a/app/lib/services/connect.ts b/app/lib/services/connect.ts index 5316b8b2ae0..b47ecd7053b 100644 --- a/app/lib/services/connect.ts +++ b/app/lib/services/connect.ts @@ -59,6 +59,9 @@ let logoutListener: any; function connect({ server, logoutOnError = false }: { server: string; logoutOnError?: boolean }): Promise { return new Promise(resolve => { + // Check for running requests and abort them before connecting to the server + abort(); + disconnect(); database.setActiveDB(server); @@ -410,6 +413,12 @@ async function loginOAuthOrSso(params: ILoginCredentials) { store.dispatch(loginRequest({ resume: result.token }, false)); } +function abort(): void { + if (sdk.isInitialized) { + sdk.abort(); + } +} + function disconnect(): void { sdk.disconnect(); mediaSessionInstance.reset(); @@ -510,6 +519,7 @@ export { loginTOTP, loginWithPassword, loginOAuthOrSso, + abort, connect, disconnect, getWebsocketInfo, diff --git a/app/lib/services/sdk.ts b/app/lib/services/sdk.ts index 3d8ab078471..be955bb2300 100644 --- a/app/lib/services/sdk.ts +++ b/app/lib/services/sdk.ts @@ -82,13 +82,16 @@ class Sdk { return client.currentLogin ?? null; } + abort(): void { + this.activeSdk.abort(); + } + subscribeNotifyUser() { return this.activeSdk.subscribeNotifyUser(); } disconnect(): void { if (this.sdk) { - this.sdk.abort(); this.sdk.disconnect(); this.sdk = null; } diff --git a/app/reducers/login.test.ts b/app/reducers/login.test.ts index 395b93b0b23..35c270877ea 100644 --- a/app/reducers/login.test.ts +++ b/app/reducers/login.test.ts @@ -9,7 +9,6 @@ import { setLoginServices, setUser } from '../actions/login'; -import { selectServerRequest } from '../actions/server'; import { initialState } from './login'; import { createMockedStore, mockedStore } from './mockedStore'; @@ -31,13 +30,6 @@ describe('test selectedUsers reducer', () => { expect(state).toEqual('error'); }); - it('should not carry a failed login into the next selected server', () => { - mockedStore.dispatch(loginFailure({ error: 'error' })); - mockedStore.dispatch(selectServerRequest('https://other.example.com', '7.0.0')); - const { isFetching, failure, error } = mockedStore.getState().login; - expect({ isFetching, failure, error }).toEqual({ isFetching: false, failure: false, error: {} }); - }); - it('should return modified store after loginSuccess', () => { const user = { id: 'ajhsiahsa', diff --git a/app/reducers/login.ts b/app/reducers/login.ts index 079106d4f1c..fd786147a97 100644 --- a/app/reducers/login.ts +++ b/app/reducers/login.ts @@ -78,13 +78,6 @@ export default function login(state = initialState, action: TActionsLogin): ILog }; case types.LOGOUT: return initialState; - case types.SERVER.SELECT_REQUEST: - return { - ...state, - isFetching: false, - failure: false, - error: {} - }; case types.USER.SET: if (isSubsetOf(action.user, state.user)) { return state; From c4c82095fa504805dae68eea424e52dbd1f0e063 Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 13:35:55 -0300 Subject: [PATCH 12/14] test: share the failed-first-attempt setup --- .../__tests__/connect.integration.test.ts | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 2c0c37958f2..857be5fe06c 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -125,6 +125,13 @@ async function connectAndDriveHandshake(server = 'https://example.com') { await flushMicrotasksAndTimers(); } +async function connectAndFailFirstAttempt(server = 'https://example.com') { + await connect({ server }); + await flushMicrotasksAndTimers(); + mockConnections[0].onerror(); + await flushMicrotasksAndTimers(); +} + describe('connect() over the real SDK', () => { it('dispatches connectRequest when connecting and connectSuccess once on the handshake', async () => { await connectAndDriveHandshake(); @@ -179,11 +186,7 @@ describe('connect() over the real SDK', () => { }); it('keeps retrying when the first attempt fails and connects once the network is back', async () => { - await connect({ server: 'https://example.com' }); - await flushMicrotasksAndTimers(); - expect(mockConnections).toHaveLength(1); - mockConnections[0].onerror(); - await flushMicrotasksAndTimers(); + await connectAndFailFirstAttempt(); expect(mockConnections).toHaveLength(1); await jest.runOnlyPendingTimersAsync(); @@ -203,11 +206,7 @@ describe('connect() over the real SDK', () => { }); it('lets a socket recovery after a failed first attempt open a fresh connection immediately', async () => { - await connect({ server: 'https://example.com' }); - await flushMicrotasksAndTimers(); - mockConnections[0].onerror(); - await flushMicrotasksAndTimers(); - + await connectAndFailFirstAttempt(); const before = mockConnections.length; const recovering = sdk.driver!.reopenNow(); @@ -221,10 +220,7 @@ describe('connect() over the real SDK', () => { }); it('does not retry an attempt that a later connect() replaced', async () => { - await connect({ server: 'https://a.example.com' }); - await flushMicrotasksAndTimers(); - mockConnections[0].onerror(); - await flushMicrotasksAndTimers(); + await connectAndFailFirstAttempt('https://a.example.com'); await connect({ server: 'https://b.example.com' }); await flushMicrotasksAndTimers(); From ba59a492f8e60e2f412bfea170481a487b68bd6a Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Wed, 25 Nov 2026 16:03:16 -0300 Subject: [PATCH 13/14] test: drop the reopenNow recovery test that passes without the fix --- .../services/__tests__/connect.integration.test.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/app/lib/services/__tests__/connect.integration.test.ts b/app/lib/services/__tests__/connect.integration.test.ts index 857be5fe06c..d833bbcacb0 100644 --- a/app/lib/services/__tests__/connect.integration.test.ts +++ b/app/lib/services/__tests__/connect.integration.test.ts @@ -205,20 +205,6 @@ describe('connect() over the real SDK', () => { expect(redux.store.dispatch).toHaveBeenCalledWith(connectSuccess()); }); - it('lets a socket recovery after a failed first attempt open a fresh connection immediately', async () => { - await connectAndFailFirstAttempt(); - const before = mockConnections.length; - - const recovering = sdk.driver!.reopenNow(); - await flushMicrotasksAndTimers(); - expect(mockConnections).toHaveLength(before + 1); - mockConnections[before].onopen(); - await flushMicrotasksAndTimers(); - - await expect(recovering).resolves.toBeUndefined(); - expect(redux.store.dispatch).toHaveBeenCalledWith(connectSuccess()); - }); - it('does not retry an attempt that a later connect() replaced', async () => { await connectAndFailFirstAttempt('https://a.example.com'); From 4c3286dc41f20886048846a0a6098f9dad89144a Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Thu, 26 Nov 2026 18:19:11 -0300 Subject: [PATCH 14/14] chore: pin the SDK to the merged reopenAfterFailedOpen commit --- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index af47d5c07dc..be5acca7647 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "@rocket.chat/media-signaling": "1.0.0-rc.1", "@rocket.chat/message-parser": "0.31.36", "@rocket.chat/mobile-crypto": "RocketChat/rocket.chat-mobile-crypto#main", - "@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#03b1d58652d826526ef026bbd69f8b7aae9726c5", + "@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#e90a19ffff954c4f86a337940de19cc6b29d0be8", "@rocket.chat/ui-kit": "^0.39.0", "@zoontek/react-native-navigation-bar": "^1.1.1", "axios": "0.30.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e5e8fd71e22..25e48558902 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -102,8 +102,8 @@ importers: specifier: RocketChat/rocket.chat-mobile-crypto#main version: https://codeload.github.com/RocketChat/rocket.chat-mobile-crypto/tar.gz/69a0a250dd7c6ff0808eb659d7202be1cae7fa1c(react-native@0.86.3(@babel/core@7.29.7)(@react-native-community/cli@20.0.0(typescript@7.0.2))(@react-native/jest-preset@0.86.3(@babel/core@7.29.7)(react@19.2.3))(@react-native/metro-config@0.86.3(@babel/core@7.29.7))(@types/react@19.2.18)(react@19.2.3))(react@19.2.3) '@rocket.chat/sdk': - specifier: RocketChat/Rocket.Chat.js.SDK#03b1d58652d826526ef026bbd69f8b7aae9726c5 - version: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5 + specifier: RocketChat/Rocket.Chat.js.SDK#e90a19ffff954c4f86a337940de19cc6b29d0be8 + version: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/e90a19ffff954c4f86a337940de19cc6b29d0be8 '@rocket.chat/ui-kit': specifier: ^0.39.0 version: 0.39.0(@rocket.chat/icons@0.47.0)(@types/node@25.0.3)(typescript@7.0.2) @@ -3437,8 +3437,8 @@ packages: react: '*' react-native: '*' - '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5': - resolution: {gitHosted: true, integrity: sha512-/RfCIwc9yeaLNbPwyl7knNNHdZHvS3t4uZTTI6NBw91LMgBa6RZVXlrSSTwDXufmxa96/HdCcQ7JtyfTxsN5nw==, tarball: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5} + '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/e90a19ffff954c4f86a337940de19cc6b29d0be8': + resolution: {gitHosted: true, integrity: sha512-cvlE1hyjERzNlzdW6iKVgkWhl0rqavfXfLilAmN5JXR5rz6gRXS/0mM1bWE9PkIgUoaFKfXVKNh4PG2NBXsshw==, tarball: https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/e90a19ffff954c4f86a337940de19cc6b29d0be8} version: 2.0.0-mobile '@rocket.chat/ui-kit@0.39.0': @@ -12568,7 +12568,7 @@ snapshots: react: 19.2.3 react-native: 0.86.3(@babel/core@7.29.7)(@react-native-community/cli@20.0.0(typescript@7.0.2))(@react-native/jest-preset@0.86.3(@babel/core@7.29.7)(react@19.2.3))(@react-native/metro-config@0.86.3(@babel/core@7.29.7))(@types/react@19.2.18)(react@19.2.3) - '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/03b1d58652d826526ef026bbd69f8b7aae9726c5': + '@rocket.chat/sdk@https://codeload.github.com/RocketChat/Rocket.Chat.js.SDK/tar.gz/e90a19ffff954c4f86a337940de19cc6b29d0be8': dependencies: js-sha256: 0.9.0 tiny-events: 1.0.1