fix(signaling): cancel in-flight connect on disconnect#1093
Merged
Conversation
… counter disconnect() returned early when _client==null (connect still awaiting factory), leaving _connecting=true and the in-flight _connectAsync running. The next connect() was silently dropped, and if the stale factory eventually resolved, the module set _client and emitted SignalingConnected even though disconnect() had been called. Root cause: the guard in disconnect() (`if (client == null) return`) prevented it from resetting _connecting or invalidating the async task. Fix: introduce a monotonically increasing _generation counter. - connect() captures the current generation before starting _connectAsync - disconnect() increments _generation and resets _connecting=false - _connectAsync checks its generation at every suspension point; if it no longer matches, it cleans up its client without emitting events - the finally block only resets _connecting for the current generation Adds four tests reproducing the race: two document the broken behavior (expected to pass after the fix), two verify correct behavior.
digiboridev
approved these changes
Apr 10, 2026
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.
Problem
SignalingModuleImpl.disconnect()was a no-op when called while aconnect()was in-flight (WebSocket handshake pending). This left the module in a state where the nextconnect()call was silently dropped.Root cause
disconnect()checked only_clientto decide whether there was anything to do:Between
connect()starting and_clientFactory(...)completing,_clientisnullwhile a connect is actively in progress. Ifdisconnect()is called in this window, it returns without stopping the in-flight async task. The subsequentconnect()is then silently dropped because the module still considers itself busy.Failure chain (observed on Pixel 9, Android 16)
The lifecycle bounce (
active → paused → resumed) is triggered on Android 16 when the user taps the call button — Android Telecom API fires the transition automatically. The pause hits exactly while the WebSocket handshake is pending.Fix
Replaced the two-field approach (
bool _connecting+ connect-in-progress tracking) with a singleObject? _connectTokenfield — the "latest wins" pattern for non-cancellable Dart Futures.How it works:
connect()mints a freshObject()and passes it to_connectAsync:disconnect()sets_connectToken = null— one line that both cancels the in-flight connect and allows the nextconnect()to proceed:_connectAsyncchecks token identity after every suspension point. If the token no longer matches, the connect was superseded — it cleans up silently without emitting events:The
finallyguard ensures a superseded_connectAsyncdoes not clear the token that belongs to a newer active connect.Tests
Four unit tests in
signaling_module_impl_test.dartusing_ControlledFactory— a test helper with per-callCompleterand call counter to control factory resolution timing precisely:BUG: second connect() silently droppedfactory.callCount == 2after disconnect + reconnectBUG: stale connect sets _client after disconnectisConnected == falsewhen stale factory resolvesafter fix: second connect() reaches the factoryafter fix: stale result discarded, no SignalingConnectedAll 4 pass.
Diagnostic logs
Both lines appearing in a log session confirm the race was triggered and correctly handled.
Scope
SignalingModuleImplinternals only — no public API changes, no event type changesSignalingReconnectController, hub layer, or any caller