Skip to content

The agent that grows with you

fix(telegram): size ingress dispatch stall budget past a bounded slow handler

kshitijk4poor1809f4fec3cb
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 13f5bde82a5..0f425cc33b3 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -471,10 +471,11 @@ _POLLING_PROGRESS_TIMEOUT = 60.0 # generation unhealthy until getUpdates return
# positives while still recovering within a few heartbeat intervals.
_POLLING_STALL_TIMEOUT = 150.0
# Ingress dispatch stall (#102260, #130407): the transport probes prove getUpdates round-trips complete, not
-# that PTB's dispatcher ever handed the fetched updates to a handler. Two heartbeats (180s) with a
-# backlog and no dispatch progress: warn once per stall and hand the adapter to the supervisor for a
-# rebuild (an in-place polling restart keeps the wedged dispatcher). Re-arms on dispatch progress.
-_INGRESS_DISPATCH_STALL_HEARTBEATS = 2
+# that PTB's dispatcher ever handed the fetched updates to a handler. A backlog with no dispatch progress for
+# four heartbeats (360s) hands the adapter to the supervisor for a rebuild (an in-place restart keeps the
+# wedged dispatcher). Sized past _POLLING_ERROR_TASK_STUCK_TIMEOUT (300s), the bound on a slow handler: PTB
+# dispatches sequentially, so e.g. a sticker vision_analyze (120s default) must not trip it. Re-arms on progress.
+_INGRESS_DISPATCH_STALL_HEARTBEATS = 4
# sendVideo transcodes before answering, outlasting the 20s read timeout; also how long a user waits
# to hear the attachment failed, so kept modest.
_MEDIA_SEND_READ_TIMEOUT = 60.0
diff --git a/tests/gateway/test_telegram_ingress_delivery_gap.py b/tests/gateway/test_telegram_ingress_delivery_gap.py
index 2e4372c5474..2233b4c9684 100644
--- a/tests/gateway/test_telegram_ingress_delivery_gap.py
+++ b/tests/gateway/test_telegram_ingress_delivery_gap.py
@@ -53,7 +53,7 @@ def _deaf_reports(caplog) -> list[str]:
@pytest.mark.asyncio
async def test_stall_reported_once_on_backlog_regardless_of_update_age(caplog):
- """Healthy dispatch never reports; a wedged dispatcher is reported after two heartbeats even
+ """Healthy dispatch never reports; a wedged dispatcher is reported after four heartbeats even
while new updates keep arriving, and only once per stall."""
adapter = _polling_adapter()
caplog.set_level(logging.WARNING)
@@ -63,11 +63,11 @@ async def test_stall_reported_once_on_backlog_regardless_of_update_age(caplog):
assert _deaf_reports(caplog) == []
with patch.object(adapter, "_schedule_polling_recovery"):
- for _ in range(3): # a fresh update lands before every heartbeat, none dispatched
+ for _ in range(5): # a fresh update lands before every heartbeat, none dispatched
_receive(adapter, 1)
adapter._check_ingress_dispatch_stall()
(report,) = _deaf_reports(caplog)
- assert "2 update(s) fetched" in report and "4 received, 2 dispatched" in report
+ assert "4 update(s) fetched" in report and "6 received, 2 dispatched" in report
@pytest.mark.asyncio
@@ -76,13 +76,13 @@ async def test_dispatch_progress_rearms_the_report(caplog):
caplog.set_level(logging.WARNING)
with patch.object(adapter, "_schedule_polling_recovery"):
_receive(adapter, 3)
- _heartbeats(adapter, 3)
+ _heartbeats(adapter, 5)
assert len(_deaf_reports(caplog)) == 1
await _dispatch(adapter, 1) # partial drain: progress, backlog remains
adapter._check_ingress_dispatch_stall()
assert len(_deaf_reports(caplog)) == 1
- _heartbeats(adapter, 2)
+ _heartbeats(adapter, 4)
assert len(_deaf_reports(caplog)) == 2
diff --git a/tests/gateway/test_telegram_ingress_dispatch_recovery.py b/tests/gateway/test_telegram_ingress_dispatch_recovery.py
index 6aa3c7e0ef4..0018c780575 100644
--- a/tests/gateway/test_telegram_ingress_dispatch_recovery.py
+++ b/tests/gateway/test_telegram_ingress_dispatch_recovery.py
@@ -72,7 +72,8 @@ async def test_dispatch_stall_marks_degraded_and_goes_fatal():
try:
with patch("asyncio.sleep", new=AsyncMock()) as sleep:
- adapter._check_ingress_dispatch_stall()
+ for _ in range(3):
+ adapter._check_ingress_dispatch_stall()
assert adapter._polling_error_task is None
adapter._check_ingress_dispatch_stall()
task = adapter._polling_error_task
@@ -100,15 +101,17 @@ async def test_dispatch_progress_rearms_recovery_scheduling(caplog):
caplog.set_level(logging.WARNING)
_receive(adapter, 3)
with patch.object(adapter, "_schedule_polling_recovery") as sched:
- adapter._check_ingress_dispatch_stall()
+ for _ in range(3): # 270s: a slow-but-bounded (<=300s) sequential handler is not a wedge
+ adapter._check_ingress_dispatch_stall()
+ assert sched.call_count == 0
adapter._check_ingress_dispatch_stall()
assert sched.call_count == 1
assert len(_deaf_reports(caplog)) == 1
await _dispatch(adapter, 1)
adapter._check_ingress_dispatch_stall()
assert sched.call_count == 1
- adapter._check_ingress_dispatch_stall()
- adapter._check_ingress_dispatch_stall()
+ for _ in range(4):
+ adapter._check_ingress_dispatch_stall()
assert sched.call_count == 2
assert len(_deaf_reports(caplog)) == 2
for call in sched.call_args_list: