Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(py/triggers): stop() deadlock + subscribe() timeout connection leak (#3890)
Checo2a2d77a7644b
diff --git a/python/composio/core/models/triggers.py b/python/composio/core/models/triggers.pyindex 83b38409e..17033b288 100644--- a/python/composio/core/models/triggers.py+++ b/python/composio/core/models/triggers.py@@ -5,6 +5,7 @@ import functoolsimport hashlibimport hmacimport json+import threadingimport timeimport tracebackimport typing as t@@ -779,9 +780,26 @@ class TriggerSubscription(Resource):time.sleep(1)def stop(self) -> None:- """Stop the trigger listener."""- self._connection.disconnect()+ """Stop the trigger listener.++ ``_alive`` is cleared first so a main thread parked in+ ``wait_forever`` unblocks immediately. The connection disconnect is+ then run off the calling thread: ``pysher.Connection.disconnect`` does+ ``socket.close(); join(timeout)`` on the websocket thread, but the+ documented usage runs ``stop()`` from inside a trigger callback, which+ pysher dispatches on that very websocket thread (``_handle_event``+ blocks on the callback's ``future.result()``). Joining the thread from+ a callback that runs on it deadlocks the whole process. Tearing the+ connection down on a background thread breaks that reentrancy while+ still ensuring the socket is closed.+ """self._alive = False+ thread = threading.Thread(+ target=self._connection.disconnect,+ name="composio-trigger-disconnect",+ daemon=True,+ )+ thread.start()def restart(self) -> None:"""Restart the subscription connection"""@@ -864,18 +882,26 @@ class _SubcriptionBuilder(WithLogger):)pusher.connect()- # Wait for connection to get established+ # Wait for connection to get established. On timeout, tear down the+ # connection before raising: pysher's run loop reconnects every+ # ``reconnect_interval`` until ``disconnect`` is called, so a plain+ # raise would leak a websocket thread (and, if it eventually connects,+ # silently subscribe a forgotten channel) for the life of the process.deadline = time.time() + timeout- while time.time() < deadline:- if not self.subscription.is_alive():- time.sleep(0.5)- continue-- self.subscription._pusher = pusher # pylint: disable=protected-access- return self.subscription- raise ComposioSDKTimeoutError(- "Timed out while waiting for trigger listener to be established"- )+ try:+ while time.time() < deadline:+ if not self.subscription.is_alive():+ time.sleep(0.5)+ continue++ self.subscription._pusher = pusher # pylint: disable=protected-access+ return self.subscription+ raise ComposioSDKTimeoutError(+ "Timed out while waiting for trigger listener to be established"+ )+ except Exception:+ pusher.disconnect()+ raiseclass Triggers(Resource):diff --git a/python/tests/test_triggers.py b/python/tests/test_triggers.pyindex 9f4c2d78b..107968cbe 100644--- a/python/tests/test_triggers.py+++ b/python/tests/test_triggers.py@@ -4,6 +4,7 @@ import base64import hashlibimport hmacimport json+import threadingimport timefrom datetime import datetime, timezonefrom unittest.mock import Mock, patch@@ -15,9 +16,11 @@ from composio_client import NotFoundError, omitfrom composio import exceptionsfrom composio.core.models.triggers import (_MAX_LOGGED_FRAME_CHARS,+ ComposioSDKTimeoutError,Triggers,TriggerSubscription,WebhookVersion,+ _SubcriptionBuilder,_truncate_frame,)@@ -1584,3 +1587,130 @@ class TestTriggerSubscriptionParsing:assert len(truncated) < len(long)assert truncated.startswith("y" * _MAX_LOGGED_FRAME_CHARS)assert str(len(long)) in truncated+++class TestTriggerSubscriptionStop:+ """Tests for TriggerSubscription.stop lifecycle handling."""++ @pytest.fixture+ def subscription(self):+ """Create a TriggerSubscription with a mock client."""+ sub = TriggerSubscription(client=Mock())+ sub._alive = True+ return sub++ def test_stop_clears_alive_synchronously(self, subscription):+ """``_alive`` is cleared before ``stop`` returns, not after disconnect.++ A main thread parked in ``wait_forever`` checks ``_alive`` every+ second; clearing it synchronously (rather than after a potentially+ blocking ``disconnect``) lets that loop exit promptly.+ """+ disconnect_started = threading.Event()++ def blocking_disconnect():+ disconnect_started.set()+ # Block so we can prove _alive was cleared without waiting on+ # the disconnect.+ threading.Event().wait(timeout=5)++ subscription._connection = Mock()+ subscription._connection.disconnect = blocking_disconnect++ subscription.stop()++ # _alive is False the moment stop() returns, even though disconnect+ # is still running in the background.+ assert subscription.is_alive() is False+ assert disconnect_started.wait(timeout=2)++ def test_stop_does_not_block_on_slow_disconnect(self, subscription):+ """``stop()`` returns even if ``disconnect`` would block forever.++ pysher's ``Connection.disconnect`` joins the websocket thread. When+ ``stop()`` is called from a callback that pysher dispatches on that+ same thread, the join deadlocks. Running the disconnect on a daemon+ thread breaks the reentrancy: ``stop`` returns immediately even when+ ``disconnect`` never completes.+ """+ started = threading.Event()+ never_set = threading.Event()++ def blocking_disconnect():+ started.set()+ # Simulate pysher joining a thread that never finishes (the+ # deadlock state from the bug report).+ never_set.wait(timeout=5)++ subscription._connection = Mock()+ subscription._connection.disconnect = blocking_disconnect++ # If stop() were to call disconnect() inline it would block here.+ subscription.stop()++ # stop() returned control and the disconnect is running in the+ # background.+ assert subscription.is_alive() is False+ assert started.wait(timeout=2)+++class TestSubscriptionBuilderConnectTimeout:+ """Tests for _SubcriptionBuilder.connect timeout teardown."""++ def _make_builder(self, pusher: Mock) -> _SubcriptionBuilder:+ """Build a _SubcriptionBuilder with a patched pusher factory."""+ client = Mock()+ client.base_url = "https://api.example.com"+ with patch.object(+ _SubcriptionBuilder, "_get_pusher_instance", return_value=pusher+ ):+ b = _SubcriptionBuilder(client=client)+ b.subscription = Mock()+ b.subscription.is_alive.return_value = False+ b.internal = Mock()+ b.internal.get_sdk_realtime_credentials.return_value = Mock(+ project_id="p", pusher_key="k", pusher_cluster="c"+ )+ b._get_connection_handler = Mock( # type: ignore[method-assign]+ return_value=lambda *a, **k: None+ )+ return b++ def test_connect_disconnects_pusher_on_timeout(self):+ """On timeout, the partially-connected pusher is torn down.++ pysher's connection loop redials every ``reconnect_interval`` until+ ``disconnect`` is called. If ``connect`` raises without disconnecting,+ each timed-out attempt leaks a websocket thread for the life of the+ process. The fix wraps the wait loop in try/except so the timeout+ path disconnects before re-raising.+ """+ pusher = Mock()+ builder = self._make_builder(pusher)+ # subscription.is_alive() returns False, so the wait loop never+ # returns early -> the deadline elapses -> timeout path runs.+ builder.subscription.is_alive.return_value = False++ with (+ patch.object(+ _SubcriptionBuilder, "_get_pusher_instance", return_value=pusher+ ),+ pytest.raises(ComposioSDKTimeoutError),+ ):+ builder.connect(timeout=0.1)++ pusher.disconnect.assert_called_once()++ def test_connect_returns_subscription_without_disconnecting(self):+ """On success, the pusher stays connected and is attached to the sub."""+ pusher = Mock()+ builder = self._make_builder(pusher)+ builder.subscription.is_alive.return_value = True++ with patch.object(+ _SubcriptionBuilder, "_get_pusher_instance", return_value=pusher+ ):+ result = builder.connect(timeout=2.0)++ assert result is builder.subscription+ pusher.disconnect.assert_not_called()