fix(gateway): detach-on-timeout for adapter connect so swallowed cancel can't wedge reconnect - #71176
Closed
kshitijk4poor wants to merge 1 commit into
Closed
Conversation
…ncel can't wedge reconnect _connect_adapter_with_timeout used a plain asyncio.wait_for(adapter.connect()), which cancels an overdue child and then awaits it. A connect() whose teardown swallows CancelledError would keep the background reconnect watcher blocked indefinitely, leaving the gateway alive but never rebuilding a deaf adapter. Run connect as a detached task (the pattern already used by _await_adapter_cleanup_with_timeout): on deadline the task is cancelled and drained via consume_detached_task_result and TimeoutError is raised, so the watcher's existing failure/backoff path is unchanged. A timed-out connect is never installed in self.adapters, so a late-completing swallowed-cancel connect stays unowned and is disposed rather than leaving a half-registered adapter.
kshitijk4poor
enabled auto-merge (rebase)
July 25, 2026 03:29
Collaborator
Contributor
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.
Summary
Switches
_connect_adapter_with_timeoutfromasyncio.wait_for()to the detach-on-timeout pattern, preventing aconnect()that swallowsCancelledErrorfrom wedging the reconnect watcher indefinitely.Root cause:
asyncio.wait_forcancels an overdue child task but then awaits it — aconnect()whose teardown trapsCancelledErrorblocks the caller forever, leaving the gateway alive but never rebuilding a deaf adapter.Changes
gateway/run.py:_connect_adapter_with_timeoutnow usesasyncio.wait({task}, timeout=...)+consume_detached_task_result(same pattern as_await_adapter_cleanup_with_timeout)tests/gateway/test_platform_reconnect.py: regression testtest_connect_timeout_releases_when_connect_swallows_cancel— aconnect()that trapsCancelledErrorand keeps runningCredit: @VaitaR's original commit cherry-picked with authorship preserved.
Validation
Closes #70344 (detach-on-timeout concern). The reconnect watcher respawn concern is addressed in a separate PR.
Infographic
Image generation is unavailable in this environment (no FAL_KEY / portal credits). The infographic will be attached once available.