Personal AI, On Personal Devices
Python
fix(channels): make disconnect() actually stop the Telegram/Discord listener (#808)
havolline1ead3daba30a6
diff --git a/src/openjarvis/channels/discord_channel.py b/src/openjarvis/channels/discord_channel.pyindex e6146ff5..2405fb3d 100644--- a/src/openjarvis/channels/discord_channel.py+++ b/src/openjarvis/channels/discord_channel.py@@ -2,6 +2,7 @@from __future__ import annotations+import asyncioimport loggingimport osimport threading@@ -45,40 +46,89 @@ class DiscordChannel(BaseChannel):self._status = ChannelStatus.DISCONNECTEDself._listener_thread: Optional[threading.Thread] = Noneself._stop_event = threading.Event()+ # Serialize listener registration and shutdown so a concurrent+ # connect() cannot clear a stop request after disconnect() returns.+ self._lifecycle_lock = threading.RLock()+ # Published by _gateway_loop while it's running so disconnect()+ # can wake the async lifecycle owner (#784).+ self._client: Optional[Any] = None+ self._loop: Optional[Any] = None+ self._async_stop_event: Optional[asyncio.Event] = None+ self._runtime_lock = threading.Lock()# -- connection lifecycle ---------------------------------------------------def connect(self) -> None:"""Start listening for incoming messages via discord.py gateway."""- if not self._token:- logger.warning("No Discord bot token configured")- self._status = ChannelStatus.ERROR- return+ with self._lifecycle_lock:+ if self._listener_thread is not None and self._listener_thread.is_alive():+ # A gateway connection is already running; starting another+ # one duplicates message handling (#784).+ logger.warning(+ "Discord channel already connected; ignoring duplicate connect()"+ )+ return- self._stop_event.clear()- self._status = ChannelStatus.CONNECTING+ if not self._token:+ logger.warning("No Discord bot token configured")+ self._status = ChannelStatus.ERROR+ return- try:- import discord # noqa: F401+ self._stop_event.clear()+ self._status = ChannelStatus.CONNECTING- self._listener_thread = threading.Thread(- target=self._gateway_loop,- daemon=True,- )- self._listener_thread.start()- self._status = ChannelStatus.CONNECTED- logger.info("Discord channel connected (gateway)")- except ImportError:- logger.info("discord.py not installed; send-only mode")- self._status = ChannelStatus.CONNECTED+ try:+ import discord # noqa: F401++ listener = threading.Thread(+ target=self._gateway_loop,+ daemon=True,+ )+ self._listener_thread = listener+ self._status = ChannelStatus.CONNECTED+ listener.start()+ logger.info("Discord channel connected (gateway)")+ except ImportError:+ logger.info("discord.py not installed; send-only mode")+ self._status = ChannelStatus.CONNECTED+ except Exception:+ self._listener_thread = None+ self._status = ChannelStatus.ERROR+ logger.exception("Failed to start Discord listener")def disconnect(self) -> None:- """Stop the listener thread."""- self._stop_event.set()- if self._listener_thread is not None:- self._listener_thread.join(timeout=5.0)- self._listener_thread = None- self._status = ChannelStatus.DISCONNECTED+ """Stop the listener thread.++ The listener owns login, gateway connection, and client cleanup.+ ``disconnect`` publishes a durable stop request onto that loop; the+ listener races each async startup phase against it and performs the+ close itself. Status is only reported DISCONNECTED after the thread+ has actually terminated.+ """+ with self._lifecycle_lock:+ self._stop_event.set()++ with self._runtime_lock:+ loop = self._loop+ async_stop_event = self._async_stop_event+ if loop is not None and async_stop_event is not None:+ try:+ loop.call_soon_threadsafe(async_stop_event.set)+ except RuntimeError:+ logger.debug("Discord gateway event loop already closed")++ if self._listener_thread is not None:+ self._listener_thread.join(timeout=5.0)+ if self._listener_thread.is_alive():+ logger.warning(+ "Discord listener thread did not stop within timeout;"+ " leaving status unchanged to avoid a duplicate"+ " gateway connection"+ )+ return+ self._listener_thread = None++ self._status = ChannelStatus.DISCONNECTED# -- send / receive --------------------------------------------------------@@ -150,11 +200,64 @@ class DiscordChannel(BaseChannel):# -- internal helpers -------------------------------------------------------+ def _stop_requested(self, async_stop_event: asyncio.Event) -> bool:+ return self._stop_event.is_set() or async_stop_event.is_set()++ async def _run_stage_until_stopped(+ self,+ awaitable: Any,+ async_stop_event: asyncio.Event,+ ) -> bool:+ """Run one Discord startup stage, cancelling it when stop wins."""+ if self._stop_requested(async_stop_event):+ close = getattr(awaitable, "close", None)+ if callable(close):+ close()+ return False++ stage_task = asyncio.create_task(awaitable)+ stop_task = asyncio.create_task(async_stop_event.wait())+ try:+ done, _pending = await asyncio.wait(+ {stage_task, stop_task},+ return_when=asyncio.FIRST_COMPLETED,+ )+ if stop_task in done or self._stop_event.is_set():+ stage_task.cancel()+ await asyncio.gather(stage_task, return_exceptions=True)+ return False+ await stage_task+ return True+ finally:+ stop_task.cancel()+ await asyncio.gather(stop_task, return_exceptions=True)++ async def _run_gateway_lifecycle(+ self,+ client: Any,+ async_stop_event: asyncio.Event,+ ) -> None:+ """Own Discord login/connect/close with stop checks at each boundary."""+ try:+ if not await self._run_stage_until_stopped(+ client.login(self._token),+ async_stop_event,+ ):+ return+ if self._stop_requested(async_stop_event):+ return+ await self._run_stage_until_stopped(+ client.connect(reconnect=True),+ async_stop_event,+ )+ finally:+ await client.close()+def _gateway_loop(self) -> None:"""Run the discord.py client in a background thread."""+ client: Any | None = None+ loop: asyncio.AbstractEventLoop | None = Nonetry:- import asyncio-import discordintents = discord.Intents.default()@@ -190,10 +293,36 @@ class DiscordChannel(BaseChannel):loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)- loop.run_until_complete(client.start(self._token))+ async_stop_event = asyncio.Event()+ with self._runtime_lock:+ self._client = client+ self._async_stop_event = async_stop_event+ self._loop = loop++ # A disconnect before these references were published remains in+ # the durable threading event. The owned lifecycle checks it before+ # login and races it against every asynchronous startup phase.+ loop.run_until_complete(+ self._run_gateway_lifecycle(client, async_stop_event)+ )except Exception:logger.debug("Discord gateway loop error", exc_info=True)- self._status = ChannelStatus.ERROR+ if not self._stop_event.is_set():+ self._status = ChannelStatus.ERROR+ finally:+ if loop is not None and not loop.is_closed():+ try:+ if client is not None and not client.is_closed():+ loop.run_until_complete(client.close())+ except Exception:+ logger.debug("Discord client cleanup failed", exc_info=True)+ finally:+ asyncio.set_event_loop(None)+ loop.close()+ with self._runtime_lock:+ self._client = None+ self._async_stop_event = None+ self._loop = Nonedef _publish_sent(self, channel: str, content: str, conversation_id: str) -> None:"""Publish a CHANNEL_MESSAGE_SENT event on the bus."""diff --git a/src/openjarvis/channels/telegram.py b/src/openjarvis/channels/telegram.pyindex ab33bc7f..64fccb7a 100644--- a/src/openjarvis/channels/telegram.py+++ b/src/openjarvis/channels/telegram.py@@ -2,6 +2,7 @@from __future__ import annotations+import asyncioimport loggingimport osimport textwrap@@ -54,43 +55,97 @@ class TelegramChannel(BaseChannel):self._status = ChannelStatus.DISCONNECTEDself._listener_thread: Optional[threading.Thread] = Noneself._stop_event = threading.Event()+ # Serialize connect/disconnect all the way through listener+ # registration and shutdown. Without this lock, disconnect() can+ # complete just before a concurrent connect() clears the shared stop+ # event, losing the stop request and leaving a fresh poller behind.+ self._lifecycle_lock = threading.RLock()+ # Published by _poll_loop while it's running so disconnect() can+ # wake the async lifecycle owner (#784).+ self._app: Optional[Any] = None+ self._loop: Optional[Any] = None+ self._async_stop_event: Optional[asyncio.Event] = None+ self._runtime_lock = threading.Lock()# -- connection lifecycle ---------------------------------------------------def connect(self) -> None:"""Start listening for incoming messages via long polling."""- if not self._token:- logger.warning("No Telegram bot token configured")- self._status = ChannelStatus.ERROR- return+ with self._lifecycle_lock:+ if self._listener_thread is not None and self._listener_thread.is_alive():+ # A poller is already running against this bot token; starting+ # another one causes Telegram's getUpdates API to return 409+ # Conflict for one or both pollers (#784).+ logger.warning(+ "Telegram channel already connected; ignoring duplicate connect()"+ )+ return- self._stop_event.clear()- self._status = ChannelStatus.CONNECTING+ if not self._token:+ logger.warning("No Telegram bot token configured")+ self._status = ChannelStatus.ERROR+ return- try:- from telegram.ext import ApplicationBuilder # noqa: F401+ self._stop_event.clear()+ self._status = ChannelStatus.CONNECTING- self._listener_thread = threading.Thread(- target=self._poll_loop,- daemon=True,- )- self._listener_thread.start()- self._status = ChannelStatus.CONNECTED- logger.info("Telegram channel connected (long polling)")- except ImportError:- # python-telegram-bot not installed — send-only mode- logger.info(- "python-telegram-bot not installed; send-only mode",- )- self._status = ChannelStatus.CONNECTED+ try:+ from telegram.ext import ApplicationBuilder # noqa: F401++ listener = threading.Thread(+ target=self._poll_loop,+ daemon=True,+ )+ self._listener_thread = listener+ # Publish CONNECTED before starting. A listener that fails+ # immediately can then reliably overwrite it with ERROR;+ # connect() must not race afterward and hide that failure.+ self._status = ChannelStatus.CONNECTED+ listener.start()+ logger.info("Telegram channel connected (long polling)")+ except ImportError:+ # python-telegram-bot not installed — send-only mode+ logger.info(+ "python-telegram-bot not installed; send-only mode",+ )+ self._status = ChannelStatus.CONNECTED+ except Exception:+ self._listener_thread = None+ self._status = ChannelStatus.ERROR+ logger.exception("Failed to start Telegram listener")def disconnect(self) -> None:- """Stop the listener thread."""- self._stop_event.set()- if self._listener_thread is not None:- self._listener_thread.join(timeout=5.0)- self._listener_thread = None- self._status = ChannelStatus.DISCONNECTED+ """Stop the listener thread.++ The listener owns every async startup/shutdown phase. ``disconnect``+ only publishes a stop request onto that owned loop, which is observed+ between initialize, polling startup, and application startup as well+ as during steady-state polling. Status is only reported DISCONNECTED+ once the thread has actually terminated.+ """+ with self._lifecycle_lock:+ self._stop_event.set()++ with self._runtime_lock:+ loop = self._loop+ async_stop_event = self._async_stop_event+ if loop is not None and async_stop_event is not None:+ try:+ loop.call_soon_threadsafe(async_stop_event.set)+ except RuntimeError:+ logger.debug("Telegram poll loop's event loop already closed")++ if self._listener_thread is not None:+ self._listener_thread.join(timeout=5.0)+ if self._listener_thread.is_alive():+ logger.warning(+ "Telegram listener thread did not stop within timeout;"+ " leaving status unchanged to avoid a duplicate poller"+ )+ return+ self._listener_thread = None++ self._status = ChannelStatus.DISCONNECTED# -- send / receive --------------------------------------------------------@@ -187,12 +242,146 @@ class TelegramChannel(BaseChannel):# -- internal helpers -------------------------------------------------------+ def _stop_requested(self, async_stop_event: asyncio.Event) -> bool:+ return self._stop_event.is_set() or async_stop_event.is_set()++ async def _run_stage_until_stopped(+ self,+ awaitable: Any,+ async_stop_event: asyncio.Event,+ ) -> bool:+ """Run one startup stage, cancelling it when disconnect wins the race."""+ if self._stop_requested(async_stop_event):+ close = getattr(awaitable, "close", None)+ if callable(close):+ close()+ return False++ stage_task = asyncio.create_task(awaitable)+ stop_task = asyncio.create_task(async_stop_event.wait())+ try:+ done, _ = await asyncio.wait(+ {stage_task, stop_task},+ return_when=asyncio.FIRST_COMPLETED,+ )+ if stop_task in done or self._stop_event.is_set():+ stage_task.cancel()+ await asyncio.gather(stage_task, return_exceptions=True)+ return False++ await stage_task+ return True+ finally:+ stop_task.cancel()+ await asyncio.gather(stop_task, return_exceptions=True)++ async def _shutdown_partially_initialized_app(+ self,+ app: Any,+ updater: Any,+ ) -> None:+ """Release PTB resources even when ``Application.initialize`` was cut short.++ PTB intentionally makes ``Application.shutdown`` a no-op until its+ initialize method reaches the final bookkeeping assignment. A stop can+ arrive after the bot's HTTP clients are initialized but before that+ assignment, so explicitly shut down the partially initialized+ components in that narrow state.+ """+ app_initialized = getattr(app, "_initialized", None)+ try:+ await app.shutdown()+ finally:+ if app_initialized is False:+ seen: set[int] = set()+ for component in (+ updater,+ getattr(app, "update_processor", None),+ getattr(app, "bot", None),+ ):+ if component is None or id(component) in seen:+ continue+ seen.add(id(component))+ shutdown = getattr(component, "shutdown", None)+ if not callable(shutdown):+ continue+ try:+ await shutdown()+ except Exception:+ logger.debug(+ "Failed to clean up partially initialized "+ "Telegram component",+ exc_info=True,+ )++ async def _run_polling_lifecycle(+ self,+ app: Any,+ async_stop_event: asyncio.Event,+ ) -> None:+ """Own PTB's lifecycle so no stop can disappear inside run_polling.++ ``Application.run_polling`` checks its private stop marker before+ ``Updater.start_polling`` but not after it. A disconnect in that await+ therefore used to be lost and the subsequent ``run_forever`` stranded+ the thread. Explicit boundaries make the shared stop request durable.+ """+ updater = getattr(app, "updater", None)+ if updater is None:+ raise RuntimeError("Telegram application has no updater")++ try:+ if not await self._run_stage_until_stopped(+ app.initialize(), async_stop_event+ ):+ return+ if getattr(app, "post_init", None) is not None:+ if not await self._run_stage_until_stopped(+ app.post_init(app), async_stop_event+ ):+ return++ if not await self._run_stage_until_stopped(+ updater.start_polling(drop_pending_updates=True),+ async_stop_event,+ ):+ return++ if not await self._run_stage_until_stopped(app.start(), async_stop_event):+ return++ await async_stop_event.wait()+ finally:+ if getattr(updater, "running", False):+ await updater.stop()+ if getattr(app, "running", False):+ await app.stop()+ if getattr(app, "post_stop", None) is not None:+ await app.post_stop(app)+ await self._shutdown_partially_initialized_app(app, updater)+ if getattr(app, "post_shutdown", None) is not None:+ await app.post_shutdown(app)+def _poll_loop(self) -> None:"""Long-poll for updates using python-telegram-bot."""+ loop: Any | None = None+ app: Any | None = Nonetry:from telegram.ext import ApplicationBuilder, MessageHandler, filters+ # This thread owns the loop and every PTB startup/shutdown phase.+ # Publishing the stop event lets disconnect wake it safely from+ # another thread without asking PTB to stop a half-started app.+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)+ async_stop_event = asyncio.Event()+ with self._runtime_lock:+ self._async_stop_event = async_stop_event+ self._loop = loop+app = ApplicationBuilder().token(self._token).build()+ with self._runtime_lock:+ self._app = appdef _handle_msg(update, context):msg = update.message@@ -235,10 +424,21 @@ class TelegramChannel(BaseChannel):)app.add_handler(MessageHandler(filters.TEXT, _handle_msg))- app.run_polling(stop_signals=None, drop_pending_updates=True)+ loop.run_until_complete(self._run_polling_lifecycle(app, async_stop_event))except Exception:logger.debug("Telegram poll loop error", exc_info=True)- self._status = ChannelStatus.ERROR+ if not self._stop_event.is_set():+ self._status = ChannelStatus.ERROR+ finally:+ with self._runtime_lock:+ self._app = None+ self._async_stop_event = None+ self._loop = None+ if loop is not None and not loop.is_closed():+ asyncio.set_event_loop(None)+ loop.close()+ if self._stop_event.is_set():+ self._status = ChannelStatus.DISCONNECTEDdef _publish_sent(self, channel: str, content: str, conversation_id: str) -> None:"""Publish a CHANNEL_MESSAGE_SENT event on the bus."""diff --git a/tests/channels/test_discord_channel.py b/tests/channels/test_discord_channel.pyindex b4631068..ade62d05 100644--- a/tests/channels/test_discord_channel.py+++ b/tests/channels/test_discord_channel.py@@ -2,7 +2,11 @@from __future__ import annotations+import asyncioimport os+import sys+import threading+from types import SimpleNamespacefrom unittest.mock import MagicMock, patchimport pytest@@ -132,6 +136,247 @@ class TestStatus:assert ch.status() == ChannelStatus.ERROR+class TestDisconnectStopsRealListener:+ """Regression for #784: disconnect() must actually interrupt the+ running `client.start()` call (nothing previously observed+ `_stop_event`), and must not report DISCONNECTED unless the listener+ thread actually terminated -- otherwise a subsequent connect() spawns+ a second gateway connection, duplicating message handling."""++ def test_disconnect_publishes_stop_via_call_soon_threadsafe(self):+ """The gateway thread owns close(); disconnect only signals its loop."""+ ch = DiscordChannel(bot_token="my-bot-token")+ ch._status = ChannelStatus.CONNECTED++ mock_loop = MagicMock()+ async_stop_event = MagicMock()+ ch._loop = mock_loop+ ch._async_stop_event = async_stop_event++ mock_thread = MagicMock()+ mock_thread.is_alive.return_value = False+ ch._listener_thread = mock_thread++ ch.disconnect()++ mock_loop.call_soon_threadsafe.assert_called_once_with(async_stop_event.set)+ mock_thread.join.assert_called_once()+ assert ch._listener_thread is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_disconnect_does_not_report_disconnected_if_thread_still_alive(self):+ """If the listener thread is still running after the join timeout,+ disconnect() must not lie about the channel being DISCONNECTED --+ doing so would let a later connect() spawn a duplicate gateway+ connection."""+ ch = DiscordChannel(bot_token="my-bot-token")+ ch._status = ChannelStatus.CONNECTED++ ch._client = MagicMock()+ ch._loop = MagicMock()++ mock_thread = MagicMock()+ mock_thread.is_alive.return_value = True # join() timed out+ ch._listener_thread = mock_thread++ with patch("asyncio.run_coroutine_threadsafe", return_value=MagicMock()):+ ch.disconnect()++ assert ch.status() != ChannelStatus.DISCONNECTED+ assert ch._listener_thread is mock_thread++ def test_disconnect_without_client_or_loop_still_works(self):+ """Send-only mode (discord.py not installed, or connect() never+ actually started a listener): _client/_loop are None, so+ disconnect() must fall back to the old join-only behavior+ instead of raising."""+ ch = DiscordChannel(bot_token="my-bot-token")+ ch._status = ChannelStatus.CONNECTED++ ch.disconnect()++ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_connect_guards_against_duplicate_listener(self):+ """connect() must not spawn a second gateway thread while one is+ already running."""+ ch = DiscordChannel(bot_token="my-bot-token")++ existing_thread = MagicMock()+ existing_thread.is_alive.return_value = True+ ch._listener_thread = existing_thread+ ch._status = ChannelStatus.CONNECTED++ with patch("threading.Thread") as mock_thread_cls:+ ch.connect()++ mock_thread_cls.assert_not_called()+ assert ch._listener_thread is existing_thread++ def test_disconnect_before_client_publication_prevents_gateway_start(self):+ """A disconnect during client construction must not be lost."""+ constructing = threading.Event()+ release_client = threading.Event()+ started: list[str] = []+ closed: list[bool] = []+ loops: list[asyncio.AbstractEventLoop] = []+ new_event_loop = asyncio.new_event_loop++ def make_event_loop():+ loop = new_event_loop()+ loops.append(loop)+ return loop++ class FakeClient:+ def __init__(self, *, intents):+ self.user = object()+ self._closed = False+ constructing.set()+ assert release_client.wait(timeout=2)++ def event(self, handler):+ return handler++ async def start(self, token):+ started.append(token)++ async def close(self):+ self._closed = True+ closed.append(True)++ def is_closed(self):+ return self._closed++ intents = SimpleNamespace(message_content=False)+ discord = SimpleNamespace(+ Intents=SimpleNamespace(default=lambda: intents),+ Client=FakeClient,+ )+ ch = DiscordChannel(bot_token="my-bot-token")++ with (+ patch.dict(sys.modules, {"discord": discord}),+ patch(+ "openjarvis.channels.discord_channel.asyncio.new_event_loop",+ side_effect=make_event_loop,+ ),+ ):+ ch.connect()+ assert constructing.wait(timeout=2)++ disconnect_thread = threading.Thread(target=ch.disconnect)+ disconnect_thread.start()+ assert ch._stop_event.wait(timeout=2)+ release_client.set()+ disconnect_thread.join(timeout=2)++ assert not disconnect_thread.is_alive()+ assert started == []+ assert closed == [True]+ assert len(loops) == 1+ assert loops[0].is_closed()+ assert ch._listener_thread is None+ assert ch._client is None+ assert ch._loop is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ @pytest.mark.parametrize("blocked_stage", ["login", "connect"])+ def test_disconnect_during_every_async_startup_boundary(self, blocked_stage):+ """Login and gateway startup are both cancellable by disconnect."""+ entered = threading.Event()+ calls: list[str] = []+ loops: list[asyncio.AbstractEventLoop] = []+ new_event_loop = asyncio.new_event_loop++ async def run_stage(name: str) -> None:+ calls.append(f"{name}:enter")+ if name == blocked_stage:+ entered.set()+ try:+ await asyncio.Event().wait()+ finally:+ calls.append(f"{name}:cancelled")+ calls.append(f"{name}:exit")++ class FakeClient:+ def __init__(self, *, intents):+ self.user = object()+ self._closed = False++ def event(self, handler):+ return handler++ async def login(self, token):+ assert token == "my-bot-token"+ await run_stage("login")++ async def connect(self, *, reconnect):+ assert reconnect is True+ await run_stage("connect")++ async def close(self):+ self._closed = True+ calls.append("client:close")++ def is_closed(self):+ return self._closed++ intents = SimpleNamespace(message_content=False)+ discord = SimpleNamespace(+ Intents=SimpleNamespace(default=lambda: intents),+ Client=FakeClient,+ )++ def make_event_loop():+ loop = new_event_loop()+ loops.append(loop)+ return loop++ ch = DiscordChannel(bot_token="my-bot-token")+ with (+ patch.dict(sys.modules, {"discord": discord}),+ patch(+ "openjarvis.channels.discord_channel.asyncio.new_event_loop",+ side_effect=make_event_loop,+ ),+ ):+ ch.connect()+ assert entered.wait(timeout=2), calls+ ch.disconnect()++ assert f"{blocked_stage}:cancelled" in calls+ if blocked_stage == "login":+ assert "connect:enter" not in calls+ assert calls.count("client:close") == 1+ assert len(loops) == 1+ assert loops[0].is_closed()+ assert ch._listener_thread is None+ assert ch._client is None+ assert ch._async_stop_event is None+ assert ch._loop is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_disconnect_is_serialized_with_connect_lifecycle(self):+ """A disconnect cannot slip through while connect owns lifecycle state."""+ ch = DiscordChannel(bot_token="my-bot-token")+ attempted = threading.Event()++ def disconnect() -> None:+ attempted.set()+ ch.disconnect()++ with ch._lifecycle_lock:+ worker = threading.Thread(target=disconnect)+ worker.start()+ assert attempted.wait(timeout=2)+ assert not ch._stop_event.wait(timeout=0.05)++ worker.join(timeout=2)+ assert not worker.is_alive()+ assert ch._stop_event.is_set()+ assert ch.status() == ChannelStatus.DISCONNECTED++class TestWireChannelEndToEnd:"""Regression for #515/#516 — the full inbound→reply path throughJarvisSystem.wire_channel must call the real Discord REST API with thediff --git a/tests/channels/test_telegram.py b/tests/channels/test_telegram.pyindex 668dd4b8..7c6cff2d 100644--- a/tests/channels/test_telegram.py+++ b/tests/channels/test_telegram.py@@ -2,7 +2,11 @@from __future__ import annotations+import asyncioimport os+import sys+import threading+from types import SimpleNamespacefrom unittest.mock import MagicMock, patchimport pytest@@ -210,6 +214,372 @@ class TestStatus:assert ch.status() == ChannelStatus.ERROR+class TestDisconnectStopsRealListener:+ """Regression for #784: disconnect() must actually interrupt the+ running `Application.run_polling()` call (which nothing previously+ observed `_stop_event`), and must not report DISCONNECTED unless the+ listener thread actually terminated -- otherwise a subsequent+ connect() spawns a second poller against the same bot token, and+ Telegram's getUpdates API returns 409 Conflict."""++ def test_disconnect_publishes_stop_via_call_soon_threadsafe(self):+ """The listener owns PTB cleanup; disconnect only signals its loop."""+ ch = TelegramChannel(bot_token="123:ABC")+ ch._status = ChannelStatus.CONNECTED++ mock_loop = MagicMock()+ async_stop_event = MagicMock()+ ch._loop = mock_loop+ ch._async_stop_event = async_stop_event++ mock_thread = MagicMock()+ mock_thread.is_alive.return_value = False+ ch._listener_thread = mock_thread++ ch.disconnect()++ mock_loop.call_soon_threadsafe.assert_called_once_with(async_stop_event.set)+ mock_thread.join.assert_called_once()+ assert ch._listener_thread is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_disconnect_does_not_report_disconnected_if_thread_still_alive(self):+ """If the listener thread is still running after the join timeout,+ disconnect() must not lie about the channel being DISCONNECTED --+ doing so would let a later connect() spawn a duplicate poller."""+ ch = TelegramChannel(bot_token="123:ABC")+ ch._status = ChannelStatus.CONNECTED++ ch._app = MagicMock()+ ch._loop = MagicMock()++ mock_thread = MagicMock()+ mock_thread.is_alive.return_value = True # join() timed out+ ch._listener_thread = mock_thread++ ch.disconnect()++ assert ch.status() != ChannelStatus.DISCONNECTED+ # Must keep the reference so a future disconnect()/duplicate-guard+ # can still see the thread is running.+ assert ch._listener_thread is mock_thread++ def test_disconnect_without_app_or_loop_still_works(self):+ """Send-only mode (python-telegram-bot not installed, or connect()+ never actually started a listener): _app/_loop are None, so+ disconnect() must fall back to the old join-only behavior instead+ of raising."""+ ch = TelegramChannel(bot_token="123:ABC")+ ch._status = ChannelStatus.CONNECTED++ ch.disconnect()++ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_connect_guards_against_duplicate_listener(self):+ """connect() must not spawn a second polling thread while one is+ already running -- that's exactly what produces the 409 Conflict+ errors described in the issue."""+ ch = TelegramChannel(bot_token="123:ABC")++ existing_thread = MagicMock()+ existing_thread.is_alive.return_value = True+ ch._listener_thread = existing_thread+ ch._status = ChannelStatus.CONNECTED++ with patch("threading.Thread") as mock_thread_cls:+ ch.connect()++ mock_thread_cls.assert_not_called()+ assert ch._listener_thread is existing_thread++ def test_disconnect_before_app_publication_prevents_polling_start(self):+ """A disconnect while ApplicationBuilder.build() runs is not lost."""+ building = threading.Event()+ release_app = threading.Event()+ polling_calls: list[dict] = []++ class FakeApp:+ def add_handler(self, handler):+ pass++ def run_polling(self, **kwargs):+ polling_calls.append(kwargs)++ def stop_running(self):+ raise AssertionError("app was not published when disconnect ran")++ class FakeBuilder:+ def token(self, token):+ return self++ def build(self):+ building.set()+ assert release_app.wait(timeout=2)+ return FakeApp()++ telegram_ext = SimpleNamespace(+ ApplicationBuilder=FakeBuilder,+ MessageHandler=lambda *args: args,+ filters=SimpleNamespace(TEXT=object()),+ )+ ch = TelegramChannel(bot_token="123:ABC")++ with patch.dict(+ sys.modules,+ {+ "telegram": SimpleNamespace(ext=telegram_ext),+ "telegram.ext": telegram_ext,+ },+ ):+ ch.connect()+ assert building.wait(timeout=2)++ disconnect_thread = threading.Thread(target=ch.disconnect)+ disconnect_thread.start()+ assert ch._stop_event.wait(timeout=2)+ release_app.set()+ disconnect_thread.join(timeout=2)++ assert not disconnect_thread.is_alive()+ assert polling_calls == []+ assert ch._listener_thread is None+ assert ch._app is None+ assert ch._loop is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ @pytest.mark.parametrize(+ "blocked_stage",+ ["initialize", "post_init", "start_polling", "start", "steady"],+ )+ def test_disconnect_during_every_async_startup_boundary(self, blocked_stage):+ """A stop cancels every awaited PTB boundary without outside release."""+ entered = threading.Event()+ calls: list[str] = []+ loops: list[asyncio.AbstractEventLoop] = []+ new_event_loop = asyncio.new_event_loop++ async def run_stage(name: str) -> None:+ calls.append(f"{name}:enter")+ if name == blocked_stage:+ entered.set()+ try:+ await asyncio.Event().wait()+ finally:+ calls.append(f"{name}:cancelled")+ calls.append(f"{name}:exit")++ class FakeUpdater:+ running = False++ async def start_polling(self, *, drop_pending_updates):+ assert drop_pending_updates is True+ await run_stage("start_polling")+ self.running = True++ async def stop(self):+ calls.append("updater:stop")+ self.running = False++ class FakeApp:+ def __init__(self):+ self.updater = FakeUpdater()+ self.running = False++ def add_handler(self, handler):+ calls.append("handler:add")++ async def initialize(self):+ await run_stage("initialize")++ async def post_init(self, app):+ assert app is self+ await run_stage("post_init")++ async def start(self):+ await run_stage("start")+ self.running = True+ if blocked_stage == "steady":+ entered.set()++ async def stop(self):+ calls.append("app:stop")+ self.running = False++ async def post_stop(self, app):+ assert app is self+ calls.append("app:post_stop")++ async def shutdown(self):+ calls.append("app:shutdown")++ async def post_shutdown(self, app):+ assert app is self+ calls.append("app:post_shutdown")++ app = FakeApp()++ class FakeBuilder:+ def token(self, token):+ assert token == "123:ABC"+ return self++ def build(self):+ return app++ telegram_ext = SimpleNamespace(+ ApplicationBuilder=FakeBuilder,+ MessageHandler=lambda *args: args,+ filters=SimpleNamespace(TEXT=object()),+ )++ def make_event_loop():+ loop = new_event_loop()+ loops.append(loop)+ return loop++ ch = TelegramChannel(bot_token="123:ABC")+ with (+ patch.dict(+ sys.modules,+ {+ "telegram": SimpleNamespace(ext=telegram_ext),+ "telegram.ext": telegram_ext,+ },+ ),+ patch(+ "openjarvis.channels.telegram.asyncio.new_event_loop",+ side_effect=make_event_loop,+ ),+ ):+ ch.connect()+ assert entered.wait(timeout=2), calls++ disconnect_thread = threading.Thread(target=ch.disconnect, daemon=True)+ disconnect_thread.start()+ assert ch._stop_event.wait(timeout=2)+ disconnect_thread.join(timeout=2)++ assert not disconnect_thread.is_alive(), calls+ stage_order = ["initialize", "post_init", "start_polling", "start"]+ if blocked_stage in stage_order:+ assert f"{blocked_stage}:cancelled" in calls+ assert f"{blocked_stage}:exit" not in calls+ for later_stage in stage_order[stage_order.index(blocked_stage) + 1 :]:+ assert f"{later_stage}:enter" not in calls+ assert "app:shutdown" in calls+ assert "app:post_shutdown" in calls+ assert len(loops) == 1+ assert loops[0].is_closed()+ assert ch._listener_thread is None+ assert ch._app is None+ assert ch._async_stop_event is None+ assert ch._loop is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_cancelled_initialize_releases_partially_initialized_components(self):+ """PTB's pre-bookkeeping HTTP resources are explicitly shut down."""+ entered = threading.Event()+ calls: list[str] = []++ class FakeComponent:+ def __init__(self, name):+ self.name = name++ async def shutdown(self):+ calls.append(f"{self.name}:shutdown")++ class FakeUpdater(FakeComponent):+ running = False++ def __init__(self):+ super().__init__("updater")++ async def start_polling(self, *, drop_pending_updates):+ raise AssertionError("polling must not start")++ class FakeApp:+ def __init__(self):+ self._initialized = False+ self.running = False+ self.updater = FakeUpdater()+ self.update_processor = FakeComponent("processor")+ self.bot = FakeComponent("bot")++ def add_handler(self, handler):+ pass++ async def initialize(self):+ calls.append("app:initialize")+ entered.set()+ await asyncio.Event().wait()++ async def shutdown(self):+ # This mirrors PTB: Application.shutdown() is a no-op while+ # _initialized is false, even if Bot.initialize opened clients.+ calls.append("app:shutdown")++ app = FakeApp()++ class FakeBuilder:+ def token(self, token):+ return self++ def build(self):+ return app++ telegram_ext = SimpleNamespace(+ ApplicationBuilder=FakeBuilder,+ MessageHandler=lambda *args: args,+ filters=SimpleNamespace(TEXT=object()),+ )+ ch = TelegramChannel(bot_token="123:ABC")++ with patch.dict(+ sys.modules,+ {+ "telegram": SimpleNamespace(ext=telegram_ext),+ "telegram.ext": telegram_ext,+ },+ ):+ ch.connect()+ assert entered.wait(timeout=2)+ ch.disconnect()++ assert calls == [+ "app:initialize",+ "app:shutdown",+ "updater:shutdown",+ "processor:shutdown",+ "bot:shutdown",+ ]+ assert ch._listener_thread is None+ assert ch._app is None+ assert ch._async_stop_event is None+ assert ch._loop is None+ assert ch.status() == ChannelStatus.DISCONNECTED++ def test_disconnect_is_serialized_with_connect_lifecycle(self):+ """A disconnect cannot slip through while connect owns lifecycle state."""+ ch = TelegramChannel(bot_token="123:ABC")+ attempted = threading.Event()++ def disconnect() -> None:+ attempted.set()+ ch.disconnect()++ with ch._lifecycle_lock:+ worker = threading.Thread(target=disconnect)+ worker.start()+ assert attempted.wait(timeout=2)+ assert not ch._stop_event.wait(timeout=0.05)++ worker.join(timeout=2)+ assert not worker.is_alive()+ assert ch._stop_event.is_set()+ assert ch.status() == ChannelStatus.DISCONNECTED++class TestAllowedChatIds:"""Tests for the allowed_chat_ids enforcement in _poll_loop."""