Skip to content

The agent that grows with you

fix(telegram): give media uploads their own 300 s wall-clock deadline

kshitijk4poor85e673b4a549
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index e0555c45e3b..62267ffadc4 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -366,6 +366,18 @@ _MEDIA_SEND_READ_TIMEOUT = 60.0
# Text send used to hang forever on a shielded httpcore socket (NordVPN/Telegram sticky IP).
# A wedged send froze getUpdates on the same loop. Bound every Bot API write.
_TEXT_SEND_DEADLINE = 30.0
+# Wall-clock cap on one media upload (whole request: pool wait + connect + body upload + server
+# processing). NOT `_MEDIA_SEND_READ_TIMEOUT`: that is httpx's per-phase stall budget (time-to-first-byte
+# after the body is sent), whereas this bounds the entire call, so it must leave room for bandwidth. The
+# Bot API upload cap is 50 MB; at ~2 Mbit/s that is ~200 s, plus connect (10 s) and sendVideo transcoding
+# (up to the 60 s read timeout) — 300 s covers it. It is also >2x the sum of the per-phase httpx budgets
+# (pool 8 + connect 10 + media_write 60 + read 60 = 138 s), so it only fires when a shielded socket has
+# stopped raising at all, never on a merely slow link.
+# On expiry `run_bounded_async` cancels and then abandons the upload task (never awaited), inside
+# `_chat_send_lock`: the lock is released while the abandoned task drains. Awaiting the cancel with a grace
+# period would re-hang the lock on exactly the wedged socket this bounds, so the rare late landing is
+# accepted; httpx's own timeouts free the pool slot.
+_MEDIA_SEND_DEADLINE = 300.0
_POLLING_GENERATION_CONTEXT: ContextVar[Optional[int]] = ContextVar("telegram_polling_generation", default=None)
@@ -1129,7 +1141,7 @@ class TelegramAdapter(BasePlatformAdapter):
async with self._chat_send_lock(send_kwargs.get("chat_id")):
try:
return await _await_with_thread_deadline(
- send_fn(**send_kwargs), timeout=_MEDIA_SEND_READ_TIMEOUT, label="telegram-media-send")
+ send_fn(**send_kwargs), timeout=_MEDIA_SEND_DEADLINE, label="telegram-media-send")
except Exception as send_err:
if not self._should_retry_without_dm_topic_reply_anchor(send_err, metadata, reply_to_message_id):
raise
@@ -1143,7 +1155,7 @@ class TelegramAdapter(BasePlatformAdapter):
retry_kwargs.pop("message_thread_id", None)
retry_kwargs.pop("direct_messages_topic_id", None)
return await _await_with_thread_deadline(
- send_fn(**retry_kwargs), timeout=_MEDIA_SEND_READ_TIMEOUT, label="telegram-media-send")
+ send_fn(**retry_kwargs), timeout=_MEDIA_SEND_DEADLINE, label="telegram-media-send")
def _fallback_ips(self) -> list[str]:
"""Return validated fallback IPs from config (populated by _apply_env_overrides)."""