Skip to content

Self-improving AI Employees for everyone

fix(telegram): release the getUpdates slot on shutdown

trohitgfa62d787b684
diff --git a/server/nodes/telegram/__init__.py b/server/nodes/telegram/__init__.py
index 72927acc..f0813186 100644
--- a/server/nodes/telegram/__init__.py
+++ b/server/nodes/telegram/__init__.py
@@ -35,6 +35,7 @@ from __future__ import annotations
from services.deployment.canary_registry import register_canary_trigger_type
from services.event_waiter import register_filter_builder, register_trigger_precheck
from services.node_output_schemas import register_output_schema
+from services.plugin.shutdown_hooks import register_shutdown_hook
from services.status_broadcaster import register_service_refresh
from services.ws_handler_registry import register_ws_handlers
@@ -69,6 +70,29 @@ register_output_schema("telegramSend", TelegramSendOutput)
# services/deployment/canary_registry.py.
register_canary_trigger_type(TelegramReceiveNode.type, "com.opencompany.telegram.message.received")
+
+async def _shutdown_telegram() -> None:
+ """Release the getUpdates slot before the process exits.
+
+ Telegram allows exactly one getUpdates consumer per bot token and keeps
+ an in-flight long poll reserved until it times out — up to the 30s read
+ timeout ``connect()`` configures. Without this hook the polling task was
+ never cancelled and ``Application.stop()`` never ran: the process simply
+ died holding the slot, so the next start collided with its own predecessor
+ and logged a Conflict for the remainder of the drain window. Restarts are
+ frequent in dev (uvicorn --reload), which made it look like a permanent
+ second instance.
+
+ Only helps on graceful teardown; a hard kill still leaves Telegram to time
+ the poll out. ``disconnect()`` is a no-op when nothing is connected.
+ """
+ service = get_telegram_service()
+ if service.connected:
+ await service.disconnect()
+
+
+register_shutdown_hook("telegram", _shutdown_telegram)
+
__all__ = [
"TelegramCredential",
"TelegramService",
diff --git a/server/tests/nodes/test_telegram_service.py b/server/tests/nodes/test_telegram_service.py
index f9c33633..50d0cc4d 100644
--- a/server/tests/nodes/test_telegram_service.py
+++ b/server/tests/nodes/test_telegram_service.py
@@ -501,6 +501,55 @@ def test_transient_and_fatal_polling_messages_are_distinguishable():
assert "bot disconnected" not in callback_src
+# ---------------------------------------------------------------------------
+# shutdown: release the getUpdates slot
+# ---------------------------------------------------------------------------
+
+
+async def test_shutdown_hook_releases_the_polling_slot(monkeypatch):
+ """Telegram reserves the single getUpdates slot until an in-flight long
+ poll times out (~30s). Without a shutdown hook the process died holding
+ it, so the next start collided with its own predecessor and logged a
+ Conflict for the whole drain window.
+ """
+ import nodes.telegram as tg
+ from services.plugin.shutdown_hooks import run_shutdown_hooks
+
+ service = MagicMock()
+ service.connected = True
+ service.disconnect = AsyncMock(return_value={"success": True})
+ monkeypatch.setattr(tg, "get_telegram_service", lambda: service)
+
+ await run_shutdown_hooks()
+
+ service.disconnect.assert_awaited_once()
+
+
+async def test_shutdown_hook_is_a_noop_when_not_connected(monkeypatch):
+ import nodes.telegram as tg
+ from services.plugin.shutdown_hooks import run_shutdown_hooks
+
+ service = MagicMock()
+ service.connected = False
+ service.disconnect = AsyncMock()
+ monkeypatch.setattr(tg, "get_telegram_service", lambda: service)
+
+ await run_shutdown_hooks()
+
+ service.disconnect.assert_not_awaited()
+
+
+def test_telegram_registers_a_shutdown_hook():
+ """Locks the registration itself — the hook body being correct is
+ useless if nobody wires it into lifespan teardown."""
+ import nodes.telegram # noqa: F401 (import registers)
+ from services.plugin.shutdown_hooks import registered_labels
+
+ assert "telegram" in registered_labels()
+
+
def test_media_content_types_covers_every_downloadable_kind():
assert _MEDIA_CONTENT_TYPES == {
"photo",