@devries-cameron20 / composio Mirror · read-only / commits / e93b4c851
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, 06:00 UTC

fix(core,python): keep waiting by ID through a 429 and time out on NaN (PLEN-4561)

- Python's ConnectedAccounts.wait_for_connection fetched the account before polling, so a 429 on that fetch still ended the wait. It now starts polling straight away, as TS does, which also saves a request. - A NaN timeout polled forever, with almost no delay in TS, because `remaining <= 0` is never true for NaN. It now times out after one poll. An infinite timeout keeps polling, as it did before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

viraj committed Nov 5, 2026 at 09:53 UTC e93b4c85194fa48fcfe0a9e821bf103f820efb4c
4 files changed +67 -11
python/composio/core/models/connected_accounts.py
+6 -2
@@ -139,7 +139,8 @@ class ConnectionRequest(Resource):
139 )
140
141 remaining = deadline - time.time()
142 - if remaining <= 0:
142 + # Written so a NaN timeout times out rather than polling forever.
143 + if not remaining > 0:
144 raise exceptions.ComposioSDKTimeoutError(
145 message=(
146 f"Timeout while waiting for connection {self.id} to be active"
@@ -886,8 +887,11 @@ class ConnectedAccounts:
887 """
888 Wait for connected account with given ID to be active
889 """
889 - return ConnectionRequest.from_id(
890 + # No fetch first: a 429 on it would end the wait before polling starts.
891 + return ConnectionRequest(
892 id=id,
893 + status="INITIATED",
894 + redirect_url=None,
895 client=self._client,
896 ).wait_for_connection(
897 timeout=timeout,
python/tests/test_connected_accounts.py
+43 -8
@@ -222,6 +222,43 @@ class TestConnectionRequestPolling:
222 assert clock["now"] == 6.0
223 assert len(requests) == 3
224
225 + def test_waiting_by_id_survives_a_429_on_its_first_request(self, monkeypatch):
226 + clock = self._clock(monkeypatch)
227 + active = Mock(status="ACTIVE")
228 + client = Mock()
229 + client.connected_accounts.retrieve.side_effect = [
230 + self._rate_limited("10"),
231 + active,
232 + ]
233 +
234 + result = ConnectedAccounts(client=client).wait_for_connection(
235 + id="conn-1", timeout=60.0
236 + )
237 +
238 + assert result is active
239 + assert clock["now"] == 10.0
240 + client.connected_accounts.retrieve.assert_called_with(nanoid="conn-1")
241 +
242 + def test_waiting_by_id_sends_one_request_when_already_active(self, monkeypatch):
243 + self._clock(monkeypatch)
244 + client = Mock()
245 + client.connected_accounts.retrieve.return_value = Mock(status="ACTIVE")
246 +
247 + ConnectedAccounts(client=client).wait_for_connection(id="conn-1", timeout=60.0)
248 +
249 + assert client.connected_accounts.retrieve.call_count == 1
250 +
251 + def test_a_nan_timeout_times_out_after_one_poll(self, monkeypatch):
252 + clock = self._clock(monkeypatch)
253 + client = Mock()
254 + client.connected_accounts.retrieve.return_value = Mock(status="PENDING")
255 +
256 + with pytest.raises(exceptions.ComposioSDKTimeoutError):
257 + self._request(client).wait_for_connection(timeout=float("nan"))
258 +
259 + assert clock["now"] == 0.0
260 + assert client.connected_accounts.retrieve.call_count == 1
261 +
262 def test_stops_its_own_delay_at_the_timeout_after_a_429(self, monkeypatch):
263 clock = self._clock(monkeypatch)
264 client = Mock()
@@ -741,17 +778,15 @@ class TestConnectedAccounts:
778 connected_accounts = ConnectedAccounts(client=mock_client)
779
780 with patch(
744 - "composio.core.models.connected_accounts.ConnectionRequest.from_id"
745 - ) as mock_from_id:
746 - mock_request = Mock()
747 - mock_request.wait_for_connection.return_value = "connected"
748 - mock_from_id.return_value = mock_request
749 -
781 + "composio.core.models.connected_accounts.ConnectionRequest.wait_for_connection",
782 + return_value="connected",
783 + ) as mock_wait:
784 result = connected_accounts.wait_for_connection(id="conn-123", timeout=42.0)
785
752 - mock_from_id.assert_called_once_with(id="conn-123", client=mock_client)
753 - mock_request.wait_for_connection.assert_called_once_with(timeout=42.0)
786 + mock_wait.assert_called_once_with(timeout=42.0)
787 assert result == "connected"
788 + # The wait does all the fetching, so its 429 handling covers every request.
789 + mock_client.connected_accounts.retrieve.assert_not_called()
790
791
792 def _make_status_error(error_type: t.Type[Exception], message: str):
ts/packages/core/src/models/ConnectionRequest.ts
+2 -1
@@ -143,7 +143,8 @@ export function createConnectionRequest(
143 }
144
145 const remaining = deadline - Date.now();
146 - if (remaining <= 0) {
146 + // Written so a NaN timeout times out rather than polling forever.
147 + if (!(remaining > 0)) {
148 throw new ConnectionRequestTimeoutError(`Connection request timed out for ${request.id}`);
149 }
150 // This delay stops at the deadline, so the last poll happens as the time runs out.
ts/packages/core/test/connectedAccounts/connectionRequest.test.ts
+16
@@ -392,6 +392,22 @@ describe('ConnectionRequest', () => {
392 expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(2);
393 });
394
395 + it('times out after one poll on a NaN timeout', async () => {
396 + mockClient.connectedAccounts.retrieve.mockResolvedValue(pending);
397 +
398 + expect(await start(NaN)).toBeInstanceOf(ConnectionRequestTimeoutError);
399 + expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(1);
400 + });
401 +
402 + it('keeps polling on an infinite timeout', async () => {
403 + mockClient.connectedAccounts.retrieve.mockResolvedValue(pending);
404 +
405 + void start(Infinity);
406 + await vi.advanceTimersByTimeAsync(2500);
407 +
408 + expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(3);
409 + });
410 +
411 it('reads a date-format Retry-After', async () => {
412 vi.useFakeTimers({ now: Date.UTC(2026, 0, 1) });
413 mockClient.connectedAccounts.retrieve