Skip to content

fix(gateway): guard NoneType in Telegram reconnect after network error (#55992) - #56028

Closed
AlexFucuson9 wants to merge 1 commit into
NousResearch:mainfrom
AlexFucuson9:fix/telegram-reconnect-none-guard
Closed

fix(gateway): guard NoneType in Telegram reconnect after network error (#55992)#56028
AlexFucuson9 wants to merge 1 commit into
NousResearch:mainfrom
AlexFucuson9:fix/telegram-reconnect-none-guard

Conversation

@AlexFucuson9

Copy link
Copy Markdown
Contributor

Problem

After sustained network errors, the Telegram adapter's _handle_polling_network_error reconnect handler sleeps with exponential backoff (5s–60s) before calling start_polling(). During that sleep window the supervisor may tear down the adapter — disconnect() sets self._app = None (line 3028).

The existing stop() call at line 1777 was already guarded:

if self._app and self._app.updater and self._app.updater.running:
    await self._app.updater.stop()

But start_polling() at line 1785 was not guarded:

await self._app.updater.start_polling(...)  # AttributeError if self._app is None

This crashes with 'NoneType' object has no attribute 'updater', silently killing the reconnect loop. The gateway process stays alive (state file says "running" + "connected") but never processes another Telegram message.

Fix

Add a None guard for both self._app and self._app.updater before the start_polling() call, matching the pattern already used for stop(). If the app was destroyed during the reconnect delay, log a warning and abort cleanly.

Reproduction

  1. Gateway happily polling Telegram
  2. Network hiccup — httpx.ConnectError multiple times
  3. After 10 consecutive failures, adapter enters reconnect loop
  4. During the backoff sleep, supervisor restarts the adapter (self._app = None)
  5. Reconnect handler wakes up and crashes on self._app.updater.start_polling()
  6. Gateway appears healthy (state file says "running") but is completely silent

Testing

  • python3 -m py_compile plugins/platforms/telegram/adapter.py — syntax OK
  • Verified the guard pattern matches the existing stop() guard at line 1777

Fixes #55992

After sustained network errors, the reconnect handler sleeps with
exponential backoff before calling start_polling(). During that sleep
window the supervisor may tear down the adapter (disconnect() sets
self._app = None). The existing stop() call was already guarded but
start_polling() was not — accessing self._app.updater on a None
app raised AttributeError and silently killed the reconnect loop.

Add a None guard for both self._app and self._app.updater before
the start_polling() call, matching the pattern used in stop().

Fixes NousResearch#55992
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P1 High — major feature broken, no workaround sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 1, 2026
@teknium1

teknium1 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Superseded by PR #56200, which merged the root-cause fix for #55992. Your PR (submitted first — thank you) guarded the network-error reconnect path, but returning when self._app was gone left the adapter in the silent-limbo state that is the actual bug (no live polling, no scheduled retry, no fatal notification, so the gateway never requeues the platform). The merged fix captures a stable app ref and fails fast into the existing reschedule/fatal machinery, fixes the underlying TOCTOU double-disconnect in the gateway runner that nulls self._app in the first place, and widens the guard to the conflict-retry sibling path. Credited in the PR body. #56200

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Superseded by #56224 (salvage of #56036), which fixes the same #55992 NoneType ... updater crash. Your PR adds the right defensive None-guard before start_polling(); #56224 fixes the underlying concurrency races that produce that None (the _handle_adapter_fatal_error double-disconnect TOCTOU + the stale _polling_error_task guard) and includes the same fail-fast reconnect guard. Appreciate the fix and the clear #55992 reference — full credit noted.

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 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.

Telegram polling silently dies after network error + self-restart

4 participants