Skip to content

Ignore callbacks from a replaced WebSocket transport - #104

Merged
FZambia merged 1 commit into
masterfrom
auto/stale-transport-callbacks
Oct 12, 2026
Merged

FZambia merged 1 commit into
masterfrom
auto/stale-transport-callbacks

Conversation

@FZambia

@FZambia FZambia commented Sep 11, 2026

Copy link
Copy Markdown
Member

What changed

The WebSocketListener callbacks created in Client._connect (onOpen, onMessage, onClosed, onFailure) now return early if the socket they report on is no longer Client.ws. The check runs inside the executor task, where ws is always assigned (the task can only run after _connect returns).

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();:

  1. disconnect() calls ws.close(1000), which starts a close handshake.
  2. connect() runs _connect(), which calls ws.cancel() on the old socket while it is still closing and opens a new one.
  3. OkHttp reports onFailure for the cancelled old socket. Its handler:
    • emits an onError event the user did nothing to cause,
    • calls processDisconnect(..., reconnect=true) while the client is CONNECTING for the new attempt, which closes the new socket,
    • schedules a backoff reconnect.

So the connect() the user just made is thrown away and the client only comes back after the reconnect delay (up to maxReconnectDelay). With a live server, a late onClosed from the old socket does the same. It can even tear down a new connection that has already reached CONNECTED. Stale onMessage deliveries could also feed data from the old session into the new one.

centrifuge-js already guards against this: its transport onOpen/onError/onClose callbacks compare a captured transportId with 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/onFailure to schedule the reconnect (no ping, connect error, token error) are not affected, because that socket is still Client.ws when its callback arrives.

How it was verified

  • Regression test (kept): StaleTransportCallbackTest uses the in-process FakeCentrifugoServer. It connects, calls disconnect() then connect(), and asserts that:

    • the client reaches CONNECTED again within 5s,
    • no onError is emitted,
    • onConnecting fires exactly twice (once per connect()).

    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 stale onFailure closed 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:integrationTest against the repository's docker-compose Centrifugo: all 33 tests pass, including ReconnectIntegrationTest and testConnectDisconnectLoop, which exercise the reconnect paths this touches.

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.
@FZambia
FZambia merged commit 497fa7b into master Oct 12, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant