Repository navigation
Handle connect command failure on the client executor - #103
Merged
Merged
Conversation
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.
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
Client.sendConnect's failure handler (exceptionallyafterorTimeout) 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 stillCONNECTINGon 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:
CompletableFutureDelaySchedulerthread.onErrorwas delivered from that thread, and it read the non-volatilewsfield, 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.onErrorondisconnect()during connect.processDisconnectfails all pending futures synchronously. Whendisconnect()was called while the connect reply was still in flight, the handler emittedonError(IOException)just beforeonDisconnected. centrifuge-js avoids this:_connectErrorreturns 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-processFakeCentrifugoServerto hold the connect reply:testConnectTimeoutHandledOnExecutorThreadsets a 300 ms timeout and never answers connect. It asserts thatonErrorruns on the same thread asonConnecting(the executor). Before the fix it failed withexpected same:<Thread[pool-3-thread-1]> was not:<Thread[CompletableFutureDelayScheduler]>.testNoConnectErrorWhenDisconnectCalledDuringConnectcallsdisconnect()once the server has received connect. It waits foronDisconnected, drains the executor, and asserts noonErrorwas emitted. Before the fix it failed withexpected:<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:integrationTestagainst the repo'sdocker-compose.ymlCentrifugo: all 33 tests pass.