Skip to content

The agent that grows with you

fix(telegram): unblock ingress stall recovery retry after no-loop fallback

JerryLiu36996efe44c1c85
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 33c5f828ea9..8ab976d42d6 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -2388,6 +2388,10 @@ class TelegramAdapter(BasePlatformAdapter):
"Polling is fine; PTB's dispatcher is not draining its queue.",
self.name, backlog, _INGRESS_DISPATCH_STALL_HEARTBEATS, received, dispatched,
getattr(self, "_polling_generation", 0))
+ # Reset the debounce before the hand-off: if there is no running loop the call raises
+ # below and recovery is deferred, so leaving the counter at the threshold would make the
+ # next heartbeat's guard swallow the retry this fallback promises.
+ self._ingress_stalled_heartbeats = 0
try:
self._schedule_polling_recovery(
_IngressDispatchStallError(
diff --git a/tests/gateway/test_telegram_ingress_dispatch_recovery.py b/tests/gateway/test_telegram_ingress_dispatch_recovery.py
index f8f2a1b1d78..f16b1b3a78d 100644
--- a/tests/gateway/test_telegram_ingress_dispatch_recovery.py
+++ b/tests/gateway/test_telegram_ingress_dispatch_recovery.py
@@ -202,7 +202,8 @@ async def test_dispatch_progress_rearms_recovery_scheduling(caplog):
def test_without_running_loop_falls_back_to_diagnostic(caplog):
"""A direct sync call with no event loop must not raise: the warning is the
- diagnostic and the next heartbeat with a loop drives recovery."""
+ diagnostic and the next heartbeat with a loop drives recovery. The fallback
+ resets the debounce counter so that promised retry is not blocked by the guard."""
adapter = _polling_adapter()
caplog.set_level(logging.WARNING)
_receive(adapter, 2)
@@ -212,3 +213,24 @@ def test_without_running_loop_falls_back_to_diagnostic(caplog):
adapter._check_ingress_dispatch_stall()
(report,) = _deaf_reports(caplog)
assert "2 update(s) fetched" in report
+ # Degraded, but no recovery task yet — and the counter is reset so the guard
+ # cannot swallow the retry the deferred diagnostic promises.
+ assert adapter._send_path_degraded is True
+ assert adapter._polling_error_task is None
+ assert adapter._ingress_stalled_heartbeats == 0
+
+ async def drive_retry():
+ recovery = AsyncMock()
+ with patch.object(adapter, "_handle_polling_network_error", new=recovery):
+ adapter._check_ingress_dispatch_stall() # arms the debounce again
+ assert adapter._polling_error_task is None
+ adapter._check_ingress_dispatch_stall() # crosses threshold, schedules
+ task = adapter._polling_error_task
+ assert task is not None
+ await task
+ return recovery
+
+ recovery = asyncio.run(drive_retry())
+ recovery.assert_awaited_once()
+ (err,) = recovery.await_args.args
+ assert isinstance(err, tg_adapter._IngressDispatchStallError)