@devries-cameron20 / composio Mirror · read-only / commits / 909d3abdb
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 09:45 UTC

fix: follow pysher thread lifecycle in wait_forever (#4829)

## Summary `TriggerSubscription.wait_forever()` in the Python SDK ends on network drops that pysher goes on to recover from, and never ends after pysher gives up for good. It reads websocket-client's `has_errored` flag, which stays set through pysher's reconnect backoff, so it cannot tell a recoverable drop from a dead connection. Fixes #4811 ## Changes - `has_errored()` now follows pysher's connection thread instead of the socket flag: the thread stays alive across reconnects and exits once the connection is dead for good (unrecoverable `pusher:error`, or `stop()`). - Regression tests covering both directions. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## How Has This Been Tested? - 4 new tests in `TestTriggerSubscriptionStop`: socket flagged but thread alive -> not errored; thread dead -> errored; `wait_forever` returns on thread death; `wait_forever` survives a 2.5s reconnect backoff and unblocks on `stop()`. - Full `python/tests/test_triggers.py`: 128 passed. - `ruff check` and `ruff format` clean with the repo's `python/config/ruff.toml`. - Verified against installed pysher 1.0.8 / websocket-client 1.9.2 sources: `Connection` is a `Thread`, `run()` exits when pysher gives up. ## Checklist - [x] I have read and understand every line of this diff, including any LLM-generated code - [x] I ran linters/tests locally and they passed - [x] I added tests or explain why not applicable - [ ] I updated documentation as needed (no doc changes needed) - [ ] I added a changeset (Python-only change, changesets are for TS packages) --------- Signed-off-by: bunnysayzz <stfuazzo@gmail.com> Co-authored-by: jkomyno <alberto@composio.dev>

Azhar committed Oct 13, 2026 at 18:16 UTC 909d3abdbd9ba6792d9e981b8483c2baed07c505
2 files changed +144 -6
python/composio/core/models/triggers.py
+24 -6
@@ -861,15 +861,32 @@ class TriggerSubscription(Resource):
861 return self._alive
862
863 def has_errored(self) -> bool:
864 - """Check if the connection errored and disconnected."""
865 - return self._connection.socket is None or self._connection.socket.has_errored
864 + """Check if the connection errored and disconnected.
865 +
866 + This follows pysher's own lifecycle instead of websocket-client's
867 + ``has_errored`` flag. That flag stays set through pysher's reconnect
868 + backoff (and, on websocket-client 1.9.0 and older, for the rest of
869 + the reused socket), so it cannot tell a drop pysher will recover
870 + from apart from a connection pysher has given up on. pysher's
871 + connection thread stays alive across reconnects and exits once the
872 + connection is dead for good (unrecoverable ``pusher:error``, or
873 + ``stop()``), which is exactly when ``wait_forever`` should return.
874 + """
875 + connection = getattr(self, "_connection", None)
876 + if connection is None:
877 + return True
878 + return not connection.is_alive()
879
880 def set_alive(self) -> None:
881 """Set `_alive` to True."""
882 self._alive = True
883
884 def wait_forever(self) -> None:
872 - """Wait infinitely."""
885 + """Wait until stopped or the connection worker exits.
886 +
887 + Recoverable connection failures keep waiting while pysher retries,
888 + including throughout an extended network outage.
889 + """
890 while self.is_alive() and not self.has_errored():
891 time.sleep(1)
892
@@ -896,9 +913,10 @@ class TriggerSubscription(Resource):
913 thread.start()
914
915 def restart(self) -> None:
899 - """Restart the subscription connection"""
900 - self._connection.disconnect()
901 - self._connection._connect() # pylint: disable=protected-access
916 + """Request a reconnect on the running subscription worker."""
917 + # Disconnect joins the worker; calling _connect afterwards runs the
918 + # socket on the caller while has_errored sees a terminated worker.
919 + self._connection.reconnect(reconnect_interval=0)
920
921
922 def _silent_pysher_logger() -> logging.Logger:
python/tests/test_triggers.py
+120
@@ -1748,6 +1748,126 @@ class TestTriggerSubscriptionStop:
1748 assert subscription.is_alive() is False
1749 assert started.wait(timeout=2)
1750
1751 + def test_has_errored_ignores_socket_flag_while_pysher_retries(self, subscription):
1752 + """A recoverable drop is not an error while pysher keeps retrying.
1753 +
1754 + websocket-client's ``has_errored`` stays set through pysher's
1755 + reconnect backoff, so ``has_errored()`` must not consult it: only
1756 + the pysher connection thread (alive across reconnects) decides.
1757 + """
1758 + connection = Mock()
1759 + connection.is_alive.return_value = True
1760 + connection.socket = Mock()
1761 + connection.socket.has_errored = True
1762 + subscription._connection = connection
1763 +
1764 + assert subscription.has_errored() is False
1765 +
1766 + def test_has_errored_when_pysher_thread_died(self, subscription):
1767 + """Once pysher gives up for good, the connection counts as errored.
1768 +
1769 + After an unrecoverable ``pusher:error`` (4000-4099) pysher
1770 + disconnects and its connection thread exits; websocket-client's
1771 + flag may never have been set, but ``has_errored()`` must still
1772 + report the dead connection.
1773 + """
1774 + connection = Mock()
1775 + connection.is_alive.return_value = False
1776 + connection.socket = Mock()
1777 + connection.socket.has_errored = False
1778 + subscription._connection = connection
1779 +
1780 + assert subscription.has_errored() is True
1781 +
1782 + def test_wait_forever_returns_when_pysher_gives_up(self, subscription):
1783 + """``wait_forever`` unblocks once the pysher thread is gone."""
1784 + connection = Mock()
1785 + connection.is_alive.return_value = False
1786 + subscription._connection = connection
1787 +
1788 + waiter = threading.Thread(target=subscription.wait_forever, daemon=True)
1789 + waiter.start()
1790 + try:
1791 + waiter.join(timeout=2)
1792 + assert not waiter.is_alive()
1793 + finally:
1794 + subscription._alive = False
1795 + waiter.join(timeout=2)
1796 +
1797 + def test_restart_preserves_pysher_worker(self, subscription):
1798 + """Manual reconnect keeps the real worker and subscription wait alive."""
1799 + connected = threading.Event()
1800 + reconnected = threading.Event()
1801 + closed = threading.Event()
1802 + done = threading.Event()
1803 + attempts = []
1804 +
1805 + class Socket:
1806 + has_errored = False
1807 +
1808 + def run_forever(self, **kwargs):
1809 + closed.clear()
1810 + attempts.append(threading.current_thread())
1811 + (connected if len(attempts) == 1 else reconnected).set()
1812 + closed.wait(timeout=5)
1813 +
1814 + def close(self):
1815 + closed.set()
1816 +
1817 + connection = triggers_module.PusherConnection(
1818 + event_handler=Mock(), url="ws://localhost", reconnect_interval=0
1819 + )
1820 + subscription._connection = connection
1821 + waiter = threading.Thread(
1822 + target=lambda: (subscription.wait_forever(), done.set()), daemon=True
1823 + )
1824 + restarter = threading.Thread(target=subscription.restart, daemon=True)
1825 + with patch("pysher.connection.websocket.WebSocketApp", return_value=Socket()):
1826 + connection.start()
1827 + try:
1828 + assert connected.wait(timeout=2)
1829 + waiter.start()
1830 + restarter.start()
1831 + assert reconnected.wait(timeout=2)
1832 + assert subscription.has_errored() is False
1833 + assert attempts[:2] == [connection, connection]
1834 + restarter.join(timeout=2)
1835 + assert not restarter.is_alive()
1836 + assert not done.wait(timeout=1.1)
1837 + # Terminal worker death must end a wait already in progress.
1838 + connection.disconnect(timeout=2)
1839 + assert done.wait(timeout=2)
1840 + finally:
1841 + subscription._alive = False
1842 + connection.disconnect(timeout=2)
1843 + restarter.join(timeout=2)
1844 + if waiter.ident is not None:
1845 + waiter.join(timeout=2)
1846 +
1847 + def test_wait_forever_survives_reconnect_backoff(self, subscription):
1848 + """``wait_forever`` does not return early on a recoverable drop.
1849 +
1850 + The pysher thread stays alive through its reconnect backoff even
1851 + though the socket flagged an error; the wait must survive until
1852 + ``stop()`` clears ``_alive``.
1853 + """
1854 + connection = Mock()
1855 + connection.is_alive.return_value = True
1856 + connection.socket = Mock()
1857 + connection.socket.has_errored = True
1858 + subscription._connection = connection
1859 +
1860 + done = threading.Event()
1861 + waiter = threading.Thread(
1862 + target=lambda: (subscription.wait_forever(), done.set()),
1863 + daemon=True,
1864 + )
1865 + waiter.start()
1866 +
1867 + assert not done.wait(timeout=2.5)
1868 + subscription.stop()
1869 + assert done.wait(timeout=5)
1870 +
1871
1872 class TestSubscriptionBuilderConnectTimeout:
1873 """Tests for _SubcriptionBuilder.connect timeout teardown."""