The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): rebuild adapter on confirmed polling stall instead of reusing unquiesced updater (#113618)
joaomarcos4a7ed0029d1b
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex 193f896e3b8..9cf5386c800 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -1196,6 +1196,24 @@ class TelegramAdapter(BasePlatformAdapter):return Truereturn False+ @classmethod+ def _looks_like_polling_stall(cls, error: Exception) -> bool:+ """True when the error represents a confirmed polling stall rather than a transient transport drop.++ A wedged getUpdates consumer (CLOSE-WAIT socket or unquiesced server session) cannot safely be recovered+ by restarting the same Updater in-place: stop() may return while the underlying action did not quiesce,+ leaving subsequent in-place polling attempts deaf (#113618).+ """+ for cur in _iter_exception_graph(error):+ text = str(cur).lower()+ if (+ "polling stall watchdog" in text+ or "general path healthy but getupdates stalled" in text+ or "getupdates made no progress" in text+ ):+ return True+ return False+def _coerce_bool_extra(self, key: str, default: bool = False) -> bool:value = self.config.extra.get(key) if getattr(self.config, "extra", None) else Noneif value is None:@@ -1996,6 +2014,18 @@ class TelegramAdapter(BasePlatformAdapter):await self._drain_polling_connections()if self._teardown_started:return+ if self._looks_like_polling_stall(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,+ )+ returntry:if not app:raise RuntimeError("Telegram application was torn down during reconnect")@@ -2227,6 +2257,9 @@ class TelegramAdapter(BasePlatformAdapter):"[%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.",self.name, stalled_for, getattr(self, "_polling_generation", 0))+ self._send_path_degraded = True+ if getattr(self, "_running", False):+ self._mark_degraded()self._spawn_polling_recovery(asyncio.get_running_loop(),self._handle_polling_network_error(diff --git a/tests/gateway/test_telegram_polling_stall_watchdog.py b/tests/gateway/test_telegram_polling_stall_watchdog.pyindex 8b2da920d88..c0c64947c9c 100644--- a/tests/gateway/test_telegram_polling_stall_watchdog.py+++ b/tests/gateway/test_telegram_polling_stall_watchdog.py@@ -71,6 +71,98 @@ async def test_stalled_long_poll_escalates_to_reconnect_ladder():recovery.assert_awaited_once()+async def test_confirmed_stall_hands_off_before_reusing_updater():+ """A stop that returns before the old getUpdates action quiesces is unsafe to reuse."""+ adapter = _make_adapter(stalled_seconds=400)+ adapter._running = True+ updater = adapter._app.updater+ updater.stop = AsyncMock(return_value=None)+ updater.start_polling = AsyncMock()+ adapter._drain_polling_connections = AsyncMock()+ adapter._notify_fatal_error = AsyncMock()++ try:+ with patch("asyncio.sleep", new=AsyncMock()):+ await adapter._check_polling_stall()+ task = adapter._polling_error_task+ assert task is not None+ await task++ assert adapter.has_fatal_error+ assert adapter.fatal_error_retryable is True+ adapter._notify_fatal_error.assert_awaited_once()+ updater.start_polling.assert_not_awaited()+ finally:+ background_tasks = tuple(adapter._background_tasks)+ for background_task in background_tasks:+ background_task.cancel()+ await asyncio.gather(*background_tasks, return_exceptions=True)+++async def test_verifier_stall_hands_off_before_reusing_updater(monkeypatch):+ """Post-reconnect verifier stall escalates to fatal rather than retrying on an unquiesced updater."""+ from plugins.platforms.telegram import adapter as tg_adapter++ adapter = _make_adapter(stalled_seconds=0)+ adapter._running = True+ updater = adapter._app.updater+ updater.stop = AsyncMock(return_value=None)+ updater.start_polling = AsyncMock()+ adapter._drain_polling_connections = AsyncMock()+ adapter._notify_fatal_error = AsyncMock()++ generation, progress = adapter._begin_polling_generation()+ monkeypatch.setattr(tg_adapter, "_POLLING_PROGRESS_TIMEOUT", 0)++ try:+ with patch("asyncio.sleep", new=AsyncMock()):+ await adapter._verify_polling_after_reconnect(generation, progress)+ task = adapter._polling_error_task+ assert task is not None+ await task++ assert adapter.has_fatal_error+ assert adapter.fatal_error_retryable is True+ adapter._notify_fatal_error.assert_awaited_once()+ updater.start_polling.assert_not_awaited()+ finally:+ background_tasks = tuple(adapter._background_tasks)+ for background_task in background_tasks:+ background_task.cancel()+ await asyncio.gather(*background_tasks, return_exceptions=True)+++async def test_generic_network_error_reconnects_without_fatal():+ """A generic network error (e.g. Bad Gateway / timeout) must reconnect in-place, not fatal."""+ adapter = _make_adapter(stalled_seconds=0)+ adapter._running = True+ updater = adapter._app.updater+ updater.stop = AsyncMock(return_value=None)+ updater.start_polling = AsyncMock()+ adapter._drain_polling_connections = AsyncMock()+ adapter._notify_fatal_error = AsyncMock()++ try:+ with patch("asyncio.sleep", new=AsyncMock()):+ task = asyncio.create_task(+ adapter._handle_polling_network_error(Exception("Bad Gateway 502"))+ )+ adapter._polling_error_task = task+ await task++ assert not adapter.has_fatal_error+ adapter._notify_fatal_error.assert_not_called()+ updater.start_polling.assert_awaited_once()+ finally:+ background_tasks = tuple(adapter._background_tasks)+ for background_task in background_tasks:+ background_task.cancel()+ await asyncio.gather(*background_tasks, return_exceptions=True)++@pytest.mark.asyncioasync def test_generation_with_no_progress_ever_uses_generation_age():"""A generation that never completes one round-trip still trips the