Skip to content

The agent that grows with you

fix(telegram): wall-clock deadline on every Bot API send (text + media)

jollyroger148004057250a3a5
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 16b96222526..e0555c45e3b 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -49,7 +49,7 @@ def _consume_abandoned_task(task: asyncio.Task) -> None:
logger.debug("Abandoned Telegram init task failed after timeout", exc_info=True)
-async def _await_with_thread_deadline(awaitable, timeout: float, *, on_abandon=None):
+async def _await_with_thread_deadline(awaitable, timeout: float, *, on_abandon=None, label: str = "telegram-init"):
"""Wall-clock deadline that survives a blocked loop / cancellation-shielded PTB+httpcore init.
``on_abandon`` runs detached so an abandoned initialize() can't leak an httpx pool. Raises
@@ -62,9 +62,9 @@ async def _await_with_thread_deadline(awaitable, timeout: float, *, on_abandon=N
detached best-effort ``on_abandon`` cleanup so an abandoned initialize() can't leak an httpx pool per
retry attempt, and off-loop stack-dump diagnostics when the loop never processes the expiry.
"""
- result = await run_bounded_async(awaitable, timeout, label="telegram-init", on_abandon=on_abandon)
+ result = await run_bounded_async(awaitable, timeout, label=label, on_abandon=on_abandon)
if result.timed_out:
- raise asyncio.TimeoutError()
+ raise asyncio.TimeoutError(f"timed out after {timeout:.0f}s ({label})")
return result.value
@@ -363,6 +363,9 @@ _INGRESS_DISPATCH_STALL_HEARTBEATS = 2
# 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
+# 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
_POLLING_GENERATION_CONTEXT: ContextVar[Optional[int]] = ContextVar("telegram_polling_generation", default=None)
@@ -1125,7 +1128,8 @@ class TelegramAdapter(BasePlatformAdapter):
``send()`` so a file upload cannot land between two chunks of the text it accompanies."""
async with self._chat_send_lock(send_kwargs.get("chat_id")):
try:
- return await send_fn(**send_kwargs)
+ return await _await_with_thread_deadline(
+ send_fn(**send_kwargs), timeout=_MEDIA_SEND_READ_TIMEOUT, 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
@@ -1138,7 +1142,8 @@ class TelegramAdapter(BasePlatformAdapter):
retry_kwargs["reply_to_message_id"] = None
retry_kwargs.pop("message_thread_id", None)
retry_kwargs.pop("direct_messages_topic_id", None)
- return await send_fn(**retry_kwargs)
+ return await _await_with_thread_deadline(
+ send_fn(**retry_kwargs), timeout=_MEDIA_SEND_READ_TIMEOUT, label="telegram-media-send")
def _fallback_ips(self) -> list[str]:
"""Return validated fallback IPs from config (populated by _apply_env_overrides)."""
@@ -1459,7 +1464,9 @@ class TelegramAdapter(BasePlatformAdapter):
try:
# Raw Bot API result: return_type=Message would make PTB deserialize a 10.1 shape it doesn't
# fully model; a post-delivery parse error ≠ send failure.
- msg = await self._bot.do_api_request("sendRichMessage", api_kwargs=payload)
+ msg = await _await_with_thread_deadline(
+ self._bot.do_api_request("sendRichMessage", api_kwargs=payload),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as exc:
if self._rich_rejected(exc, "sendRichMessage", "MarkdownV2"):
return None
@@ -1503,7 +1510,9 @@ class TelegramAdapter(BasePlatformAdapter):
# No topic routing on edits: message_thread_id/direct_messages_topic_id make Telegram reject it.
payload = {**self._rich_payload_base(chat_id, content), "message_id": int(message_id)}
try:
- await self._bot.do_api_request("editMessageText", api_kwargs=payload)
+ await _await_with_thread_deadline(
+ self._bot.do_api_request("editMessageText", api_kwargs=payload),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as exc:
# "Message is not modified" = successful no-op; skip the redundant legacy edit.
if "not modified" in str(exc).lower():
@@ -1532,7 +1541,9 @@ class TelegramAdapter(BasePlatformAdapter):
"chat_id": normalize_telegram_chat_id(chat_id), "draft_id": int(draft_id), "rich_message": self._rich_message_payload(content)}
payload.update(self._thread_kwargs_for_draft(chat_id, metadata))
try:
- return bool(await self._bot.do_api_request("sendRichMessageDraft", api_kwargs=payload))
+ return bool(await _await_with_thread_deadline(
+ self._bot.do_api_request("sendRichMessageDraft", api_kwargs=payload),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send"))
except Exception as exc:
if self._is_rich_capability_error(exc):
self._rich_draft_disabled = True
@@ -2600,8 +2611,10 @@ class TelegramAdapter(BasePlatformAdapter):
self._persist_dm_topic_thread_id(int(chat_id), topic_name, thread_id)
# Seed message: Telegram's client hides empty topics until they contain one.
try:
- await self._bot.send_message(
- chat_id=normalize_telegram_chat_id(chat_id), message_thread_id=thread_id, text=f"\U0001f4cc {topic_name}")
+ await _await_with_thread_deadline(
+ self._bot.send_message(
+ chat_id=normalize_telegram_chat_id(chat_id), message_thread_id=thread_id, text=f"\U0001f4cc {topic_name}"),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as seed_err:
logger.debug("[%s] Could not send seed message to topic '%s': %s", self.name, topic_name, seed_err)
@@ -3332,11 +3345,15 @@ class TelegramAdapter(BasePlatformAdapter):
async def _send_chunk_markdown_or_plain(self, chunk: str, send_kwargs: Dict[str, Any]):
"""MarkdownV2 first; on a parse/markdown rejection resend as stripped plain text."""
try:
- return await self._bot.send_message(text=chunk, parse_mode=ParseMode.MARKDOWN_V2, **send_kwargs)
+ return await _await_with_thread_deadline(
+ self._bot.send_message(text=chunk, parse_mode=ParseMode.MARKDOWN_V2, **send_kwargs),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as md_error:
if "parse" in str(md_error).lower() or "markdown" in str(md_error).lower():
logger.warning("[%s] MarkdownV2 parse failed, falling back to plain text: %s", self.name, md_error)
- return await self._bot.send_message(text=_strip_mdv2(chunk), parse_mode=None, **send_kwargs)
+ return await _await_with_thread_deadline(
+ self._bot.send_message(text=_strip_mdv2(chunk), parse_mode=None, **send_kwargs),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
raise
async def _send_chunk_with_retries(
@@ -3654,7 +3671,8 @@ class TelegramAdapter(BasePlatformAdapter):
kwargs: Dict[str, Any] = {"chat_id": normalize_telegram_chat_id(chat_id), "message_id": int(message_id), "text": text}
if parse_mode is not None:
kwargs["parse_mode"] = parse_mode
- await self._bot.edit_message_text(**kwargs)
+ await _await_with_thread_deadline(
+ self._bot.edit_message_text(**kwargs), timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
async def _edit_markdown_or_plain(self, chat_id: str, message_id: str, formatted: str, plain: str, warn_fmt: str) -> bool:
"""MarkdownV2 edit with plain-text fallback. Returns True on a "not modified" no-op (caller may
@@ -3804,9 +3822,11 @@ class TelegramAdapter(BasePlatformAdapter):
else:
# Degrade to stripped text on finalize (raw ** / ``` would render literally); previews stay raw.
text = _strip_mdv2(chunk) if finalize else chunk
- return await self._bot.send_message(
+ return await _await_with_thread_deadline(
+ self._bot.send_message(
chat_id=normalize_telegram_chat_id(chat_id), text=text, parse_mode=ParseMode.MARKDOWN_V2 if use_markdown else None,
- reply_to_message_id=reply_to_id, **thread_kwargs, **base)
+ reply_to_message_id=reply_to_id, **thread_kwargs, **base),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as send_err:
if "reply message not found" in str(send_err).lower():
# Private DM topic fallback needs anchor + topic id together; forum topics keep thread id.
@@ -3814,9 +3834,11 @@ class TelegramAdapter(BasePlatformAdapter):
{} if self._dm_topic_fallback(metadata)
else self._thread_kwargs_for_send(chat_id, thread_id, metadata, reply_to_message_id=None))
try:
- return await self._bot.send_message(
+ return await _await_with_thread_deadline(
+ self._bot.send_message(
chat_id=normalize_telegram_chat_id(chat_id), text=_strip_mdv2(chunk) if finalize else chunk,
- **retry_thread_kwargs, **base)
+ **retry_thread_kwargs, **base),
+ timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as _retry_err:
logger.warning(
"[%s] Overflow continuation no-reply retry failed: %s", self.name, _redact_telegram_error_text(_retry_err))
@@ -3926,7 +3948,8 @@ class TelegramAdapter(BasePlatformAdapter):
kwargs["parse_mode"] = ParseMode.MARKDOWN_V2
kwargs.update(draft_thread_kwargs)
try:
- if await self._bot.send_message_draft(**kwargs):
+ if await _await_with_thread_deadline(
+ self._bot.send_message_draft(**kwargs), timeout=_TEXT_SEND_DEADLINE, label="telegram-send"):
return SendResult(success=True, message_id=None)
return SendResult(success=False, error="draft_rejected")
except Exception as e:
@@ -3953,7 +3976,8 @@ class TelegramAdapter(BasePlatformAdapter):
raise RuntimeError("Not connected")
message_thread_id = kwargs.get("message_thread_id")
try:
- return await self._bot.send_message(**kwargs)
+ return await _await_with_thread_deadline(
+ self._bot.send_message(**kwargs), timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
except Exception as send_err:
if (message_thread_id is not None and self._is_bad_request_error(send_err) and self._is_thread_not_found_error(send_err)):
logger.warning(
@@ -3962,7 +3986,8 @@ class TelegramAdapter(BasePlatformAdapter):
self._prune_stale_dm_topic_binding(kwargs.get("chat_id"), message_thread_id)
retry_kwargs = dict(kwargs)
retry_kwargs.pop("message_thread_id", None)
- return await self._bot.send_message(**retry_kwargs)
+ return await _await_with_thread_deadline(
+ self._bot.send_message(**retry_kwargs), timeout=_TEXT_SEND_DEADLINE, label="telegram-send")
raise
async def _send_control_message(
diff --git a/tests/gateway/test_telegram_send_path_health.py b/tests/gateway/test_telegram_send_path_health.py
index 91e0a01123f..3b46aebeb0c 100644
--- a/tests/gateway/test_telegram_send_path_health.py
+++ b/tests/gateway/test_telegram_send_path_health.py
@@ -5,11 +5,14 @@ can enter a wedged state where ``bot.send_message()`` returns a valid Message
but nothing reaches the recipient. ``_send_path_degraded`` short-circuits
``send()`` so cron's live-adapter branch falls through to standalone HTTP.
"""
+import asyncio
+import time
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from gateway.config import PlatformConfig
+from plugins.platforms.telegram import adapter as tg_adapter
from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402
@@ -76,6 +79,27 @@ async def test_send_short_flood_still_retries_inline(monkeypatch):
sleep.assert_awaited_once_with(2.0)
+async def test_send_times_out_instead_of_hanging(monkeypatch):
+ """A shielded/hung send_message must fail on the wall-clock deadline, not pin getUpdates."""
+ adapter = _make_adapter()
+ adapter._rich_send_disabled = True
+
+ async def _hang(**_kwargs):
+ await asyncio.sleep(1000)
+
+ adapter._bot.send_message = _hang
+ monkeypatch.setattr(tg_adapter, "_TEXT_SEND_DEADLINE", 0.05)
+
+ t0 = time.monotonic()
+ result = await adapter.send("123", "hello")
+ elapsed = time.monotonic() - t0
+
+ assert result.success is False
+ assert "timed out" in (result.error or "").lower()
+ assert elapsed < 5.0
+
+
def test_mark_connected_publishes_connected_when_healthy():
"""A normal connect (never degraded) still publishes platform_state=connected."""
adapter = _make_adapter()