Skip to content

fix(gateway): recover Telegram after fatal disconnect wedge (#80598) - #80700

Closed
HexLab98 wants to merge 2 commits into
NousResearch:mainfrom
HexLab98:fix/80598-telegram-fatal-handler-wedge
Closed

fix(gateway): recover Telegram after fatal disconnect wedge (#80598)#80700
HexLab98 wants to merge 2 commits into
NousResearch:mainfrom
HexLab98:fix/80598-telegram-fatal-handler-wedge

Conversation

@HexLab98

@HexLab98 HexLab98 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Queue retryable platforms for background reconnection before any fatal-path disconnect() await, so a wedged Telegram close cannot leave _failed_platforms empty and the reconnect watcher with nothing to retry (#80598).
  • Add an outer detach-on-timeout around the fatal handler, and harden TelegramAdapter.disconnect() (release the bot-token lock immediately; bound lifecycle/PTB stop/shutdown with detach-on-timeout) so recovery cannot stall on a half-dead socket after a network outage.

Test plan

  • scripts/run_tests.sh tests/gateway/test_runner_fatal_adapter.py tests/gateway/test_telegram_network_reconnect.py tests/gateway/test_platform_reconnect.py tests/gateway/test_safe_adapter_disconnect.py -q
  • Simulate prolonged api.telegram.org unreachability until telegram_network_error fatal escalation; confirm logs show queued for background reconnection / gateway staying alive and that Telegram reconnects after the network returns without a manual gateway restart
  • Confirm a cancellation-swallowing / hung disconnect() no longer strands the platform while cron/MCP keep the process alive

Fixes #80598

…earch#80598)

After a network outage the Telegram fatal handler could hang inside
disconnect() and never populate _failed_platforms, so the reconnect
watcher had nothing to retry and the process stayed permanently deaf.
Queue retryable platforms before any disconnect await, bound the fatal
handler with an outer detach deadline, and release the Telegram token
lock / PTB close steps with detach-on-timeout so recovery cannot stall.
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/gateway Gateway runner, session dispatch, delivery comp/plugins Plugin system and bundled plugins platform/telegram Telegram bot adapter sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Aug 7, 2026
kshitijk4poor added a commit that referenced this pull request Aug 7, 2026
…laim keys

Follow-up to #80700:

1. _await_disconnect_step was missing the try/except CancelledError around
   asyncio.wait() that _await_adapter_cleanup_with_timeout already has.
   When the outer fatal-handler timeout cancels disconnect() mid-step,
   asyncio.wait does NOT cancel its inner task — the task was orphaned
   with no observer. Add the same cancel+detach+re-raise pattern.

2. _queue_retryable_fatal_platform omitted credential_claim/listener_claim
   keys that all 3 startup-path queue sites include. These are consumed by
   the multiplex reservation logic to prevent secondary profiles from
   taking the endpoint while a primary is queued. Pre-existing latent bug
   — now fixed since the extraction makes it trivial.
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Merged via #81036 — your commits were cherry-picked onto current main with authorship preserved via rebase-merge.

Your fix was correct and well-tested. During review we found two small follow-ups that we applied on top:

  1. _await_disconnect_step was missing a try/except CancelledError around asyncio.wait() — the similar _await_adapter_cleanup_with_timeout in gateway/run.py already has this. Without it, an outer timeout could orphan the inner task.
  2. _queue_retryable_fatal_platform (extracted from your inline code) was missing credential_claim/listener_claim keys that the startup-path queue sites include — a pre-existing latent bug that your extraction made trivial to fix.

Thanks for the thorough bug report (#80598) and the clean fix!

ma1138569845 pushed a commit to ma1138569845/dechnicAuditor-agent that referenced this pull request Aug 10, 2026
…laim keys

Follow-up to NousResearch#80700:

1. _await_disconnect_step was missing the try/except CancelledError around
   asyncio.wait() that _await_adapter_cleanup_with_timeout already has.
   When the outer fatal-handler timeout cancels disconnect() mid-step,
   asyncio.wait does NOT cancel its inner task — the task was orphaned
   with no observer. Add the same cancel+detach+re-raise pattern.

2. _queue_retryable_fatal_platform omitted credential_claim/listener_claim
   keys that all 3 startup-path queue sites include. These are consumed by
   the multiplex reservation logic to prevent secondary profiles from
   taking the endpoint while a primary is queued. Pre-existing latent bug
   — now fixed since the extraction makes it trivial.
randlee pushed a commit to randlee/hermes-agent that referenced this pull request Aug 11, 2026
…laim keys

Follow-up to NousResearch#80700:

1. _await_disconnect_step was missing the try/except CancelledError around
   asyncio.wait() that _await_adapter_cleanup_with_timeout already has.
   When the outer fatal-handler timeout cancels disconnect() mid-step,
   asyncio.wait does NOT cancel its inner task — the task was orphaned
   with no observer. Add the same cancel+detach+re-raise pattern.

2. _queue_retryable_fatal_platform omitted credential_claim/listener_claim
   keys that all 3 startup-path queue sites include. These are consumed by
   the multiplex reservation logic to prevent secondary profiles from
   taking the endpoint while a primary is queued. Pre-existing latent bug
   — now fixed since the extraction makes it trivial.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery comp/plugins Plugin system and bundled plugins P1 High — major feature broken, no workaround platform/telegram Telegram bot adapter sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram gateway goes permanently deaf after network outage — reconnect watcher never fires (fatal handler wedges)

3 participants