Skip to content

The agent that grows with you

refactor(telegram): drop no-loop fallback from ingress dispatch stall watchdog

kshitijk4poor3d3fdbd17009
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 8ab976d42d6..07891cc2c05 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -2388,22 +2388,13 @@ 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(
- "PTB dispatcher made no progress for %d heartbeats "
- "(%d received, %d dispatched, generation %d; ingress dispatch stall watchdog)"
- % (_INGRESS_DISPATCH_STALL_HEARTBEATS, received, dispatched,
- getattr(self, "_polling_generation", 0))),
- reason="ingress dispatch stall watchdog")
- except RuntimeError:
- # No running event loop (unit-test direct call): the warning above is the
- # diagnostic; the next heartbeat with a loop drives recovery.
- logger.debug("[%s] Ingress dispatch stall recovery deferred (no running loop)", self.name)
+ self._schedule_polling_recovery(
+ _IngressDispatchStallError(
+ "PTB dispatcher made no progress for %d heartbeats "
+ "(%d received, %d dispatched, generation %d; ingress dispatch stall watchdog)"
+ % (_INGRESS_DISPATCH_STALL_HEARTBEATS, received, dispatched,
+ getattr(self, "_polling_generation", 0))),
+ reason="ingress dispatch stall watchdog")
async def _check_polling_stall(self) -> None:
"""Watchdog the last successful getUpdates round-trip: a long-poll can wedge without raising
diff --git a/tests/gateway/test_telegram_ingress_dispatch_recovery.py b/tests/gateway/test_telegram_ingress_dispatch_recovery.py
index f16b1b3a78d..649c7133f12 100644
--- a/tests/gateway/test_telegram_ingress_dispatch_recovery.py
+++ b/tests/gateway/test_telegram_ingress_dispatch_recovery.py
@@ -198,39 +198,3 @@ async def test_dispatch_progress_rearms_recovery_scheduling(caplog):
for call in sched.call_args_list:
(err,) = call.args
assert isinstance(err, tg_adapter._IngressDispatchStallError)
-
-
-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. 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)
- adapter._check_ingress_dispatch_stall()
- assert _deaf_reports(caplog) == []
- # Second heartbeat crosses the threshold with no running loop in this sync test.
- 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)