Skip to content

The agent that grows with you

fix(telegram): a confirmed stall hands off before the reconnect backoff, not after it

kshitijk4poor5ca3e5546c5a
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 6383e10d05f..acdbcf35ecc 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -1967,9 +1967,21 @@ class TelegramAdapter(BasePlatformAdapter):
"""Reconnect polling after a transient network interruption (NetworkError/TimedOut).
Host connectivity loss (sleep, WiFi switch, VPN) kills the long-poll silently. Exponential back-off (5s→60s
- cap) up to MAX_NETWORK_RETRIES, then retryable-fatal so the supervisor restarts the gateway."""
+ cap) up to MAX_NETWORK_RETRIES, then retryable-fatal so the supervisor restarts the gateway.
+
+ A confirmed polling stall (``_PollingStallError``) skips the ladder entirely: the Updater's long-poll
+ action never quiesced, so it is handed to the supervisor for a rebuild before any backoff."""
if self._teardown_started or self.has_fatal_error:
return
+ if isinstance(error, _PollingStallError):
+ # Not a retry: no counter bump, no backoff, no in-place stop/drain. The supervisor's rebuild
+ # runs disconnect(), which performs the same bounded updater.stop() and app.shutdown().
+ message = (
+ "Telegram polling stall confirmed (getUpdates made no progress); "
+ "rebuilding the adapter instead of reusing an Updater whose long-poll action did not quiesce."
+ )
+ await self._go_fatal_network(message, "[%s] %s (rebuilding adapter via supervisor)", self.name, message)
+ return
MAX_NETWORK_RETRIES = 10
BASE_DELAY = 5
MAX_DELAY = 60
@@ -2004,18 +2016,6 @@ class TelegramAdapter(BasePlatformAdapter):
await self._drain_polling_connections()
if self._teardown_started:
return
- if any(isinstance(cur, _PollingStallError) for cur in _iter_exception_graph(error)):
- message = (
- "Telegram polling stall confirmed (getUpdates made no progress); "
- "rebuilding the adapter instead of reusing an Updater whose long-poll action did not quiesce."
- )
- await self._go_fatal_network(
- message,
- "[%s] %s (rebuilding adapter via supervisor)",
- self.name,
- message,
- )
- return
try:
if not app:
raise RuntimeError("Telegram application was torn down during reconnect")
@@ -2225,9 +2225,10 @@ class TelegramAdapter(BasePlatformAdapter):
async def _check_polling_stall(self) -> None:
"""Watchdog the last successful getUpdates round-trip: a long-poll can wedge without raising
(CLOSE-WAIT after a route flip) while every other probe stays blind; no round-trip for
- ``_POLLING_STALL_TIMEOUT`` ⇒ escalate through the bounded reconnect ladder.
+ ``_POLLING_STALL_TIMEOUT`` ⇒ raise ``_PollingStallError`` so the recovery path hands the
+ adapter to the supervisor for a rebuild instead of reusing the wedged Updater.
- See #92991.
+ See #92991, #113618.
"""
if self._webhook_mode or self._teardown_started or self.has_fatal_error or self._recovery_in_flight():
return
@@ -2245,7 +2246,7 @@ class TelegramAdapter(BasePlatformAdapter):
return
logger.error(
"[%s] Telegram polling stalled: no getUpdates progress for %.0fs "
- "(generation %d). Rebuilding the long-poll consumer through the reconnect ladder instead of staying silently deaf.",
+ "(generation %d). Handing the adapter to the supervisor for a rebuild instead of staying silently deaf.",
self.name, stalled_for, getattr(self, "_polling_generation", 0))
self._schedule_polling_recovery(
_PollingStallError("getUpdates made no progress for %.0fs (polling stall watchdog)" % stalled_for),
diff --git a/tests/gateway/test_telegram_polling_stall_watchdog.py b/tests/gateway/test_telegram_polling_stall_watchdog.py
index 5a1e6cfab17..8fbfd0fa1cf 100644
--- a/tests/gateway/test_telegram_polling_stall_watchdog.py
+++ b/tests/gateway/test_telegram_polling_stall_watchdog.py
@@ -86,9 +86,10 @@ async def test_confirmed_stall_hands_off_before_reusing_updater(monkeypatch, via
updater.start_polling = AsyncMock()
adapter._drain_polling_connections = AsyncMock()
adapter._notify_fatal_error = AsyncMock()
+ error_count_before = adapter._polling_network_error_count
try:
- with patch("asyncio.sleep", new=AsyncMock()):
+ with patch("asyncio.sleep", new=AsyncMock()) as sleep:
if via_verifier:
generation, progress = adapter._begin_polling_generation()
monkeypatch.setattr(tg_adapter, "_POLLING_PROGRESS_TIMEOUT", 0)
@@ -104,6 +105,12 @@ async def test_confirmed_stall_hands_off_before_reusing_updater(monkeypatch, via
adapter._notify_fatal_error.assert_awaited_once()
adapter._mark_degraded.assert_called_once()
updater.start_polling.assert_not_awaited()
+ # A confirmed stall is a handoff, not a retry: no backoff, no retry-counter bump,
+ # and no in-place stop/drain (the supervisor's disconnect() does the bounded stop).
+ sleep.assert_not_awaited()
+ assert adapter._polling_network_error_count == error_count_before
+ updater.stop.assert_not_awaited()
+ adapter._drain_polling_connections.assert_not_awaited()
finally:
background_tasks = tuple(adapter._background_tasks)
for background_task in background_tasks: