Skip to content

Handle connect command failure on the client executor - #103

Merged
FZambia merged 1 commit into
masterfrom
auto/connect-timeout-executor
Sep 11, 2026
Merged

FZambia merged 1 commit into
masterfrom
auto/connect-timeout-executor

Conversation

@FZambia

@FZambia FZambia commented Sep 11, 2026

Copy link
Copy Markdown
Member

What changed

Client.sendConnect's failure handler (exceptionally after orTimeout) now re-submits to the client executor, like every other command failure handler already does. Once there, it only reports the error and closes the transport if the client is still CONNECTING on the same WebSocket the connect was sent on.

Why

This fixes two problems with the old handler, which ran on whatever thread completed the connect future:

  1. Connect timeout ran off the executor. A timeout completes the future on the CompletableFutureDelayScheduler thread. onError was delivered from that thread, and it read the non-volatile ws field, at the same time as other callbacks ran on the executor. This is the same kind of race as Fix thread-safety race on unsubscribe timeout #96, Fix thread-safety race in Subscription.sendRefresh token callback #97 and Fix thread-safety race on subscribeEpoch in Subscription.sendRefresh #99.
  2. Spurious onError on disconnect() during connect. processDisconnect fails all pending futures synchronously. When disconnect() was called while the connect reply was still in flight, the handler emitted onError(IOException) just before onDisconnected. centrifuge-js avoids this: _connectError returns early unless the client is connecting.

The timeout path still works as before: the error is reported, the transport is closed, and the client reconnects.

Verification

Added ConnectFailureHandlingTest, which uses the in-process FakeCentrifugoServer to hold the connect reply:

  • testConnectTimeoutHandledOnExecutorThread sets a 300 ms timeout and never answers connect. It asserts that onError runs on the same thread as onConnecting (the executor). Before the fix it failed with expected same:<Thread[pool-3-thread-1]> was not:<Thread[CompletableFutureDelayScheduler]>.
  • testNoConnectErrorWhenDisconnectCalledDuringConnect calls disconnect() once the server has received connect. It waits for onDisconnected, drains the executor, and asserts no onError was emitted. Before the fix it failed with expected:<0> but was:<1>.

I kept both tests in the tree. They guard the executor-confinement rule that recent fixes established, and they aren't covered elsewhere.

Test runs:

  • ./gradlew :centrifuge:test: all unit tests pass, including both new ones.
  • ./gradlew :centrifuge:integrationTest against the repo's docker-compose.yml Centrifugo: all 33 tests pass.

The connect future's failure handler ran wherever the future was
completed. On a connect timeout that is the CompletableFuture delay
scheduler thread, so onError was delivered and the client's ws field was
read off the executor, concurrently with other callbacks. Every other
command failure handler already re-submits to the executor.

The handler also ran synchronously inside processDisconnect, which fails
all pending futures: calling disconnect() while the connect reply was
still in flight emitted a spurious onError(IOException) right before
onDisconnected.

Hop to the executor, and skip reporting/closing once the client has left
CONNECTING or the transport was replaced - matching centrifuge-js, whose
_connectError returns early unless the client is connecting.
@FZambia
FZambia merged commit cbb5ef3 into master Sep 11, 2026
12 checks passed
@FZambia
FZambia deleted the auto/connect-timeout-executor branch September 11, 2026 04:51
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