Repository navigation
fix: retry the socket when the first connect attempt fails - #7833
Conversation
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.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughThe SDK client now enables reopening after a failed connection. Integration tests cover retries after socket errors, immediate reopening, and replacing a connection. ChangesConnection retry handling
Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Suggested labels: Suggested reviewers: Merge Risk:
|
|
iOS Build Available Rocket.Chat 4.78.0.110065 |
|
Android Build Available Rocket.Chat 4.78.0.110064 Internal App Sharing: https://play.google.com/apps/test/RQQ8k09hlnQ/ahAO29uNRciXhnJ27mM34m9Iz2hJjBgFDUEwJRS6fyxxINeNqUaNhdefNwOZ18Ga6fFSOj68EN5ppz5l41R-vyXWoy |
diegolmello
left a comment
There was a problem hiding this comment.
The fix does what it says: a failed first open now enters the SDK's Reopen loop through a forced attempt. I checked these races and they are safe: a newer connect() or a logout, a foreground recoverSocket() at the same moment, and onerror followed by onclose. One inline cleanup on the tests, plus two decisions:
Fix it in the SDK instead? In the pinned SDK, Connection.open (lib/drivers/connection.ts) calls start(false, recovery), so a fresh open's failure is the only failure path that never schedules a Reopen. Having that failure enter the Reopen loop in the SDK would remove the app-side code here: the captured driver, the guard, the comment and the second test. The trade-off is that the first retry would wait the normal reopen delay (5s in prod) instead of running at once.
Retry forever for hosts that refuse the socket (app/lib/services/connect.ts:104-108). A stored server whose WebSocket is disabled or rejected (the 400 case getWebsocketInfo warns about), or a host that makes constructTransport throw, used to fail once and stop. Now it retries every 5s until logout or a server switch, including while Android is in the background. This matches what already happens after any unexpected disconnect, so it is probably intended, but it is a behaviour change worth confirming.
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.
diegolmello
left a comment
There was a problem hiding this comment.
No bugs found. The sdk.driver === driver guard holds against logout, a newer connect() and a concurrent recoverSocket(), and both new tests fail when the code they cover is removed. Two small cleanups inline, plus one decision:
Make the shared mock's close() settle by default (app/lib/testUtils/sdkIntegration.ts:36, used at app/lib/services/__tests__/connect.integration.test.ts:209-213). The default close = jest.fn() never fires onclose, so the SDK's close never settles. That is why the first version of the superseded-connect test passed without the guard. If close() set readyState = 3 and called onclose by default, the per-test mockImplementation would go away and future tests couldn't make the same mistake. The socketHealth tests share this mock, so it's your call.
diegolmello
left a comment
There was a problem hiding this comment.
Overall the fix is correct against the pinned SDK. The sdk.driver === driver guard holds on every disconnect and server-switch path, because Sdk.disconnect() nulls the SDK synchronously, and the retry joins a recoverSocket() already in flight instead of duplicating it. One cleanup is inline. One decision for you:
Unbounded retry now also covers the first connect (app/lib/services/connect.ts:105-106)
- An unreachable host, TLS or client-certificate failure, or a proxy that blocks WebSockets now retries every 5s for as long as the process lives. Before, the socket stayed dead until the foreground
recoverSocket(). - This matches the loop that runs after an established connection drops. It now also runs on the login screen and, on Android, in the background.
- Is that intended, or should the SDK cap or back off?
diegolmello
left a comment
There was a problem hiding this comment.
The fix works against the pinned SDK, and both new tests fail without it. I'd still like the retry to move into Rocket.Chat.js.SDK before this merges.
The failed first open not retrying is deliberate SDK behaviour, introduced in RocketChat/Rocket.Chat.js.SDK#421: a plain open() that fails gives the caller one answer and stays idle. The app's main client wants the opposite, but other clients rely on it. getWebsocketInfo returns on a 400 without disconnecting, and logoutFromServer builds a short-lived client of its own. So the SDK can't simply retry every failed open. It needs an opt-in instead.
Proposal:
- SDK: add a socket option, e.g.
reopenAfterFailedOpen. InConnection.open(), usethis.start(false, recovery || this.options.reopenAfterFailedOpen), and pass the option throughISocketOptions/ConnectionOptions. With it on, a failed plain open schedules the normal Reopen afterreopenDelayMs.connect()still rejects with the first failure, andclose()still cancels everything. - App:
- Bump the SDK pin.
- Pass
reopenAfterFailedOpen: trueininitializeSdk(app/lib/services/sdk.ts). - Delete
const { driver } = sdkand thereopenNow()branch inconnect.ts. - Leave the probes (
getWebsocketInfo,logoutFromServer) without the option.
- Tests: keep both new integration tests as regression tests over the real SDK, with two adjustments:
- The first retry now comes after the reopen delay, not immediately.
- The replaced-connect test should advance past the reopen delay before it asserts, so a delayed retry from the closed driver would be caught.
- SDK specs to add:
- A failed first open rejects and schedules exactly one retry.
- It keeps retrying until a handshake succeeds.
- A first open that times out also retries.
- A close during or after the failure leaves nothing scheduled.
reopenNow()replaces a scheduled or in-flight retry.- Without the option, the socket stays idle after a failed open.
I prototyped this against the SDK at ecf4fed: the existing suite plus the new specs pass, and tsc is clean.
A permanently unreachable server would still retry every 5s with no cap under this design, the same as after a dropped connection today. I think that's acceptable here, but it's worth knowing.
…ogin can't reopen it
diegolmello
left a comment
There was a problem hiding this comment.
The fix works with the pinned SDK. A failed first open now schedules the normal Reopen. A later connect(), a logout, an expired workspace and account deletion all cancel it, and a foreground recovery or a native call accept replaces it without opening a second socket. The probe clients stay one-shot. One cleanup is inline, plus one decision:
The SDK pin is an unmerged commit (package.json:60)
03b1d58is a commit on RocketChat/Rocket.Chat.js.SDK#441, which is still open.- Its code matches that PR's current head; the later commits only change docs and tests.
- If the SDK PR is squash-merged, this app is left pinned to a commit that is not on the SDK's main branch, and a fresh install could fail once the branch is gone.
- Merge the SDK PR first and re-pin to the merged commit?
diegolmello
left a comment
There was a problem hiding this comment.
The fix works with the pinned SDK. The new test "keeps retrying when the first attempt fails" fails without reopenAfterFailedOpen: true. The earlier test cleanup is done. No bugs or cleanups to raise inline. Two decisions:
A pending login on a dropped workspace now retries forever (app/lib/services/sdk.ts:50, app/lib/services/connect.ts:422)
disconnect()lets go of the client without aborting its requests. If a REST login finishes afterwards, the SDK'sDriver.logincallsconnect()on that dropped client. Its close has already settled, so the call opens a new socket.- Before this PR, a failed open there stopped after one try. With
reopenAfterFailedOpen, it schedules a Reopen every 5s, and nothing in the app holds that client any more to close it. The oldconnect()listeners stay attached, so each attempt dispatchesconnectRequest(). A success dispatchesconnectSuccess(), plus a resumeloginRequestif a token is still in Redux. - Steps:
- Start a password or resume login. Its REST
/api/v1/loginis still in flight. disconnect()runs without aconnect()after it: logout (app/lib/methods/logout.ts:148), an expired workspace (app/sagas/selectServer.ts:139), account deletion (app/sagas/login.js:482), clear cache (app/lib/methods/clearCache.ts:48), or Add workspace with no previous server (app/views/NewServerView/hooks/useConnectServer.tsx:27).- The login response arrives while the network is down or the WebSocket is blocked, so the dropped client's open fails.
- Start a password or resume login. Its REST
- Over 5 failing reopen cycles against the real SDK: this branch opened 7 transports and dispatched 5
connectRequests afterdisconnect(). Base opened 2 transports and dispatched none. - Likelihood is low: a login has to be in flight when one of those
disconnect()calls runs. - This branch fixed it in "fix: abort a client's requests when it is disconnected so a pending login can't reopen it", then reverted that to keep the PR narrow. Ship as is and track the abort in a follow-up issue, or bring the abort back here?
The SDK pin is still an unmerged commit (package.json:60)
03b1d58is a commit on RocketChat/Rocket.Chat.js.SDK#441, which is still open. Its head adds only docs and a spec on top.- If that PR is squash-merged, this app stays pinned to a commit that isn't on the SDK's main branch. Merge the SDK PR first and re-pin to the merged commit?
Proposed changes
A plain connect 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. The SDK socket takes an opt-in
reopenAfterFailedOpen(RocketChat/Rocket.Chat.js.SDK#441): with it, a failed connect schedules the normal Reopen, which a laterreopenNow()(foreground recovery, native call accept) can still replace. The app's main client turns it on; the probe clients (getWebsocketInfo,logoutFromServer) stay one-shot.Issue(s)
https://rocketchat.atlassian.net/browse/SUP-1128
Depends on RocketChat/Rocket.Chat.js.SDK#441
How to test or reproduce
Screenshots
Recorded on the iOS simulator through a local proxy to mobile.qa (stopping the proxy simulates no network), so the header shows
localhost:3999instead ofmobile.qa.rocket.chat.Before
connection-ios-BEFORE.mp4
After
connection-ios-AFTER.mp4
Types of changes
Checklist
Further comments
Summary by CodeRabbit