Repository navigation
Ignore callbacks from a replaced WebSocket transport - #104
Merged
Merged
Conversation
Every connect attempt builds a new WebSocket, but the OkHttp listener callbacks (onOpen, onMessage, onClosed, onFailure) never checked that they belong to the socket the client is currently using. Calling disconnect() and then connect() cancels the previous socket while its close handshake is still pending, so OkHttp reports onFailure for it after the new attempt has started. That stale callback emitted an onError event, ran processDisconnect with reconnect=true against the new connection (closing the new socket) and scheduled a backoff reconnect, so the connect() the user just made was thrown away. Drop callbacks whose socket is no longer Client.ws. The check runs on the executor, where ws is always assigned. centrifuge-js does the same with its transportId checks in the transport callbacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
The
WebSocketListenercallbacks created inClient._connect(onOpen,onMessage,onClosed,onFailure) now return early if the socket they report on is no longerClient.ws. The check runs inside the executor task, wherewsis always assigned (the task can only run after_connectreturns).Why
Each connect attempt builds a new WebSocket, but events from the previous socket can still arrive later, and nothing filtered them. The easiest way to trigger this is
client.disconnect(); client.connect();:disconnect()callsws.close(1000), which starts a close handshake.connect()runs_connect(), which callsws.cancel()on the old socket while it is still closing and opens a new one.onFailurefor the cancelled old socket. Its handler:onErrorevent the user did nothing to cause,processDisconnect(..., reconnect=true)while the client is CONNECTING for the new attempt, which closes the new socket,So the
connect()the user just made is thrown away and the client only comes back after the reconnect delay (up tomaxReconnectDelay). With a live server, a lateonClosedfrom the old socket does the same. It can even tear down a new connection that has already reached CONNECTED. StaleonMessagedeliveries could also feed data from the old session into the new one.centrifuge-js already guards against this: its transport
onOpen/onError/onClosecallbacks compare a capturedtransportIdwith the current one and ignore callbacks from a "non-actual transport". This change applies the same rule.Paths that close a socket and wait for its own
onClosed/onFailureto schedule the reconnect (no ping, connect error, token error) are not affected, because that socket is stillClient.wswhen its callback arrives.How it was verified
Regression test (kept):
StaleTransportCallbackTestuses the in-processFakeCentrifugoServer. It connects, callsdisconnect()thenconnect(), and asserts that:onErroris emitted,onConnectingfires exactly twice (once perconnect()).Reconnect delays are set to 60s, so a torn-down connection can't quietly come back during the test. Before the fix it failed with
connected again after disconnect() + connect(): the staleonFailureclosed the new socket and the client sat waiting for the 60s reconnect. With the fix it passes. I kept it because it guards the whole class of stale-transport callbacks through public behaviour only, and no existing test covers it../gradlew :centrifuge:test: all 63 unit tests pass../gradlew :centrifuge:integrationTestagainst the repository's docker-compose Centrifugo: all 33 tests pass, includingReconnectIntegrationTestandtestConnectDisconnectLoop, which exercise the reconnect paths this touches.