@devries-cameron20 / composio-1 / commits / 23360bb8d
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.

fix(core,python): stop rate-limited polling at the deadline (#5246)

This PR: - builds on top of https://github.com/ComposioHQ/composio/pull/5210 ([Glen review](https://app.tryglen.com/ComposioHQ/composio/pull/5210)) - closes [PLEN-4561](https://linear.app/composio/issue/PLEN-4561) - avoids another connection-status request and a fresh client retry cycle when an exhausted 429 reaches the polling deadline, in both SDKs - preserves the merged NaN-timeout behavior and the final deadline poll after ordinary pending responses - updates the API example with 1-second-to-5-second backoff, numeric and HTTP-date `Retry-After` handling, and the same deadline behavior - adds a `patch` changeset for `@composio/core` and `@composio/python-sdk` - regenerates the Python SDK reference, whose `wait_for_connection` source link drifted by four lines Validation, after rebasing onto `next`: - `@composio/core` suite: 2034 passed, 2 expected fail (78 files) - Python suite: 2493 passed, 215 skipped - core `typecheck`, Oxlint on the changed paths, Ruff `check`/`format` with `config/ruff.toml` - docs `check:code-examples`, `check:api-snippets`, and `lint:links` - the API example passed `bash -n` and mocked runtime scenarios: numeric and HTTP-date `Retry-After` retry then succeed, a terminal state exits, an exhausted 429 times out after exactly one request, and a pending connection still gets the final deadline poll Green on CI, including `SDK release note` and `SDK reference drift`. The three `E2E Test Node.js` jobs fail on `next` as well (`node-custom-tools` exits 1 there too); that is unrelated to this diff. No live Apollo 429 was exercised; in-flight client retries can still exceed the polling timeout.

Alberto Schiabel committed Nov 16, 2026 at 11:50 UTC 23360bb8da23f30b27d90c84a3e3d91240f69eb6
7 files changed +145 -66
.changeset/fix-connection-polling-deadline.md new
+6
@@ -0,0 +1,6 @@
1 +---
2 +'@composio/core': patch
3 +'@composio/python-sdk': patch
4 +---
5 +
6 +Stop connection polling at the deadline after a rate-limited response. When a `429` with `Retry-After` reaches the timeout, `waitForConnection()` and `wait_for_connection()` now time out instead of sending another status request with a fresh client retry cycle.
docs/content/docs/tools-direct/authenticating-tools.mdx
+50 -8
@@ -540,20 +540,62 @@ console.log(connectedAccount.id);
540 <Tab value="API">
541 ```bash
542 # CONNECTED_ACCOUNT_ID is the ID returned when you initiated or linked the account.
543 +# Requires Bash, curl, jq, and Python 3 to parse Retry-After dates.
544 +POLL_DIR=$(mktemp -d) || exit 1
545 +trap 'rm -rf "$POLL_DIR"' EXIT
546 DEADLINE=$((SECONDS + 60))
547 +POLL_DELAY=1
548 while true; do
545 - ACCOUNT=$(curl --fail-with-body -s https://backend.composio.dev/api/v3.1/connected_accounts/$CONNECTED_ACCOUNT_ID \
549 + HTTP_CODE=$(curl -sS \
550 + --dump-header "$POLL_DIR/headers" --output "$POLL_DIR/account" \
551 + --write-out '%{http_code}' \
552 + "https://backend.composio.dev/api/v3.1/connected_accounts/$CONNECTED_ACCOUNT_ID" \
553 -H "x-api-key: $COMPOSIO_API_KEY") || exit 1
547 - STATUS=$(echo "$ACCOUNT" | jq -r '.status')
548 - case "$STATUS" in
549 - ACTIVE) echo "$ACCOUNT" | jq -r '.id'; break ;;
550 - FAILED|EXPIRED|INACTIVE|REVOKED) echo "Connection ended: $STATUS" >&2; exit 1 ;;
554 + DELAY=$POLL_DELAY
555 + case "$HTTP_CODE" in
556 + 200)
557 + STATUS=$(jq -r '.status' "$POLL_DIR/account") || exit 1
558 + case "$STATUS" in
559 + ACTIVE) jq -r '.id' "$POLL_DIR/account"; break ;;
560 + FAILED|EXPIRED|REVOKED) echo "Connection ended: $STATUS" >&2; exit 1 ;;
561 + esac ;;
562 + 429)
563 + DELAY=$(python3 - "$POLL_DIR/headers" "$POLL_DELAY" <<'PY'
564 +import math
565 +import sys
566 +import time
567 +from email.utils import parsedate_to_datetime
568 +from pathlib import Path
569 +
570 +headers = Path(sys.argv[1]).read_text(encoding="iso-8859-1").splitlines()
571 +value = next((line.split(":", 1)[1].strip() for line in reversed(headers)
572 + if line.lower().startswith("retry-after:")), "")
573 +try:
574 + seconds = float(value)
575 +except ValueError:
576 + try:
577 + seconds = parsedate_to_datetime(value).timestamp() - time.time()
578 + except (TypeError, ValueError, OverflowError):
579 + seconds = 0
580 +print(max(float(sys.argv[2]), seconds if math.isfinite(seconds) else 0))
581 +PY
582 + ) || exit 1 ;;
583 + *) cat "$POLL_DIR/account" >&2; exit 1 ;;
584 esac
552 - if [ "$SECONDS" -ge "$DEADLINE" ]; then
585 + REMAINING=$((DEADLINE - SECONDS))
586 + if [ "$REMAINING" -le 0 ]; then
587 echo "Timed out waiting for authorization" >&2
588 exit 1
589 fi
556 - sleep 1
590 + SLEEP_FOR=$(awk -v delay="$DELAY" -v remaining="$REMAINING" \
591 + 'BEGIN { print (delay < remaining ? delay : remaining) }') || exit 1
592 + sleep "$SLEEP_FOR" || exit 1
593 + if [ "$HTTP_CODE" = 429 ] && [ "$SECONDS" -ge "$DEADLINE" ]; then
594 + echo "Timed out waiting for authorization" >&2
595 + exit 1
596 + fi
597 + POLL_DELAY=$(awk -v delay="$POLL_DELAY" \
598 + 'BEGIN { delay *= 1.5; print (delay < 5 ? delay : 5) }') || exit 1
599 done
600 ```
601 </Tab>
@@ -564,7 +606,7 @@ The method polls the Composio API, waiting 1 second between polls at first and u
606 - Enters a terminal state like **FAILED** or **EXPIRED** (throws an error)
607 - Exceeds the specified timeout (throws a timeout error)
608
567 -A rate-limited poll doesn't end the wait. The method waits as long as the response asks and polls again, and these retries can carry the wait past the timeout.
609 +A rate-limited poll waits for `Retry-After` before polling again. If the wait reaches the timeout, it times out without sending another request. The SDK clients also retry individual HTTP requests, which can run past the timeout. The API example handles 429 responses directly.
610
611 ### Checking Connection Status
612
docs/content/reference/sdk-reference/python/connected-accounts.mdx
+1 -1
@@ -262,4 +262,4 @@ def wait_for_connection(id: str, timeout: float | None = ...) -> connected_accou
262
263 ---
264
265 -[View source](https://github.com/composiohq/composio/blob/next/python/composio/core/models/connected_accounts.py#L396)
265 +[View source](https://github.com/composiohq/composio/blob/next/python/composio/core/models/connected_accounts.py#L400)
python/composio/core/models/connected_accounts.py
+13 -9
@@ -111,8 +111,9 @@ class ConnectionRequest(Resource):
111 The delay between polls grows from 1 second to 5 seconds, so a connection
112 can be up to 5 seconds old when this returns. A rate-limited poll doesn't
113 fail the wait: once the client's own retries run out, it waits as long as
114 - the response asks, up to the timeout, and polls again. The client's retries
115 - can carry the wait past the timeout, because they aren't bound by it.
114 + the response asks before polling again. If that delay reaches the timeout,
115 + the wait times out without another poll. The client's retries can carry
116 + the wait past the timeout, because they aren't bound by it.
117
118 :param timeout: The timeout to wait for the connection to be established.
119 :return: Connected account object.
@@ -122,9 +123,11 @@ class ConnectionRequest(Resource):
123 poll_delay = _INITIAL_POLL_DELAY_SECONDS
124 while True:
125 delay = poll_delay
126 + rate_limited = False
127 try:
128 connection = self._client.connected_accounts.retrieve(nanoid=self.id)
129 except RateLimitError as error:
130 + rate_limited = True
131 delay = max(delay, _retry_after_seconds(error))
132 else:
133 self.status = connection.status
@@ -141,16 +144,17 @@ class ConnectionRequest(Resource):
144 remaining = deadline - time.time()
145 # Written so a NaN timeout times out rather than polling forever.
146 if not remaining > 0:
144 - raise exceptions.ComposioSDKTimeoutError(
145 - message=(
146 - f"Timeout while waiting for connection {self.id} to be active"
147 - ),
148 - )
149 - # This delay stops at the deadline, so the last poll happens as the
150 - # time runs out.
147 + break
148 time.sleep(min(delay, remaining))
149 + # Don't start a fresh client retry cycle after a rate-limited wait expires.
150 + if rate_limited and time.time() >= deadline:
151 + break
152 poll_delay = min(poll_delay * _POLL_DELAY_GROWTH, _MAX_POLL_DELAY_SECONDS)
153
154 + raise exceptions.ComposioSDKTimeoutError(
155 + message=f"Timeout while waiting for connection {self.id} to be active",
156 + )
157 +
158 @classmethod
159 def from_id(cls, id: str, client: HttpClient) -> te.Self:
160 return cls(
python/tests/test_connected_accounts.py
+16 -8
@@ -197,8 +197,11 @@ class TestConnectionRequestPolling:
197 assert self._request(client).wait_for_connection(timeout=60.0) is active
198 assert clock["now"] == 10.0
199
200 - def test_runs_past_the_timeout_while_the_real_client_retries_a_429(
201 - self, monkeypatch
200 + @pytest.mark.parametrize(
201 + "timeout,retry_after,elapsed", [(1.0, "3", 6.0), (25.0, "10", 25.0)]
202 + )
203 + def test_does_not_restart_client_retries_after_timeout(
204 + self, monkeypatch, timeout, retry_after, elapsed
205 ):
206 clock = self._clock(monkeypatch)
207 requests = []
@@ -206,7 +209,9 @@ class TestConnectionRequestPolling:
209 def handler(request):
210 requests.append(request)
211 return httpx.Response(
209 - 429, headers={"retry-after": "3"}, json={"error": {"message": "x"}}
212 + 429,
213 + headers={"retry-after": retry_after},
214 + json={"error": {"message": "x"}},
215 )
216
217 client = HttpClient(
@@ -217,9 +222,9 @@ class TestConnectionRequestPolling:
222 )
223
224 with pytest.raises(exceptions.ComposioSDKTimeoutError):
220 - self._request(client).wait_for_connection(timeout=1.0)
225 + self._request(client).wait_for_connection(timeout=timeout)
226
222 - assert clock["now"] == 6.0
227 + assert clock["now"] == elapsed
228 assert len(requests) == 3
229
230 def test_waiting_by_id_survives_a_429_on_its_first_request(self, monkeypatch):
@@ -259,16 +264,19 @@ class TestConnectionRequestPolling:
264 assert clock["now"] == 0.0
265 assert client.connected_accounts.retrieve.call_count == 1
266
262 - def test_stops_its_own_delay_at_the_timeout_after_a_429(self, monkeypatch):
267 + @pytest.mark.parametrize("retry_after", ["5", "60"])
268 + def test_does_not_repoll_at_the_deadline_after_a_429(
269 + self, monkeypatch, retry_after
270 + ):
271 clock = self._clock(monkeypatch)
272 client = Mock()
265 - client.connected_accounts.retrieve.side_effect = self._rate_limited("60")
273 + client.connected_accounts.retrieve.side_effect = self._rate_limited(retry_after)
274
275 with pytest.raises(exceptions.ComposioSDKTimeoutError):
276 self._request(client).wait_for_connection(timeout=5.0)
277
278 assert clock["now"] == 5.0
271 - assert client.connected_accounts.retrieve.call_count == 2
279 + assert client.connected_accounts.retrieve.call_count == 1
280
281 def test_reads_a_date_format_retry_after(self, monkeypatch):
282 clock = self._clock(monkeypatch)
ts/packages/core/src/models/ConnectionRequest.ts
+12 -4
@@ -64,8 +64,9 @@ export function createConnectionRequest(
64 * either becomes active, enters a terminal error state, or times out. The delay between polls
65 * grows from 1 second to 5 seconds, so a connection can be up to 5 seconds old when this returns.
66 * A rate-limited poll doesn't fail the wait: once the client's own retries run out, it waits as
67 - * long as the response asks, up to the timeout, and polls again. The client's retries can carry
68 - * the wait past the timeout, because they aren't bound by it.
67 + * long as the response asks before polling again. If that delay reaches the timeout, the wait
68 + * times out without another poll. The client's retries can carry the wait past the timeout,
69 + * because they aren't bound by it.
70 *
71 * @param {number} [timeout=60000] - Maximum time to wait in milliseconds before timing out (default: 60 seconds)
72 * @returns {Promise<ConnectedAccountRetrieveResponse>} The final connected account response when successful
@@ -118,6 +119,7 @@ export function createConnectionRequest(
119
120 while (true) {
121 let delay = pollDelay;
122 + let rateLimited = false;
123 try {
124 const response = await client.connectedAccounts.retrieve(request.id);
125 request.status = response.status;
@@ -139,18 +141,24 @@ export function createConnectionRequest(
141 if (!isRateLimitResponse(error)) {
142 throw error;
143 }
144 + rateLimited = true;
145 delay = Math.max(delay, (getRetryAfterSeconds(error) ?? 0) * 1000);
146 }
147
148 const remaining = deadline - Date.now();
149 // Written so a NaN timeout times out rather than polling forever.
150 if (!(remaining > 0)) {
148 - throw new ConnectionRequestTimeoutError(`Connection request timed out for ${request.id}`);
151 + break;
152 }
150 - // This delay stops at the deadline, so the last poll happens as the time runs out.
153 await new Promise(resolve => setTimeout(resolve, Math.min(delay, remaining)));
154 + // A rate-limited poll must not start a fresh client retry cycle at the deadline.
155 + if (rateLimited && Date.now() >= deadline) {
156 + break;
157 + }
158 pollDelay = Math.min(pollDelay * POLL_DELAY_GROWTH, MAX_POLL_DELAY_MS);
159 }
160 +
161 + throw new ConnectionRequestTimeoutError(`Connection request timed out for ${request.id}`);
162 }
163
164 return request;
ts/packages/core/test/connectedAccounts/connectionRequest.test.ts
+47 -36
@@ -382,15 +382,20 @@ describe('ConnectionRequest', () => {
382 expect(await result).toHaveProperty('status', ConnectedAccountStatuses.ACTIVE);
383 });
384
385 - it('stops its own delay at the timeout after a 429', async () => {
386 - mockClient.connectedAccounts.retrieve.mockRejectedValue(rateLimited({ 'retry-after': '60' }));
385 + it.each(['5', '60'])(
386 + 'does not repoll at the deadline after Retry-After: %s',
387 + async retryAfter => {
388 + mockClient.connectedAccounts.retrieve.mockRejectedValue(
389 + rateLimited({ 'retry-after': retryAfter })
390 + );
391
388 - const result = start(5000);
389 - await vi.advanceTimersByTimeAsync(5000);
392 + const result = start(5000);
393 + await vi.advanceTimersByTimeAsync(5000);
394
391 - expect(await result).toBeInstanceOf(ConnectionRequestTimeoutError);
392 - expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(2);
393 - });
395 + expect(await result).toBeInstanceOf(ConnectionRequestTimeoutError);
396 + expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(1);
397 + }
398 + );
399
400 it('times out after one poll on a NaN timeout', async () => {
401 mockClient.connectedAccounts.retrieve.mockResolvedValue(pending);
@@ -428,35 +433,41 @@ describe('ConnectionRequest', () => {
433 await result;
434 });
435
431 - it('runs past the timeout while the real client retries a 429', async () => {
432 - vi.useFakeTimers();
433 - const fetch = vi.fn<typeof globalThis.fetch>(
434 - async () =>
435 - new Response(JSON.stringify({ error: { message: 'slow down' } }), {
436 - status: 429,
437 - headers: { 'content-type': 'application/json', 'retry-after': '3' },
438 - })
439 - );
440 - const client = new ComposioClient({
441 - auth: Auth.project('test-api-key'),
442 - baseURL: 'https://backend.invalid',
443 - transport: createFetchTransport({ fetch }),
444 - });
445 - let settled = false;
446 - const result = createConnectionRequest(client, connectedAccountId)
447 - .waitForConnection(1000)
448 - .catch(error => error)
449 - .finally(() => {
450 - settled = true;
436 + it.each([
437 + { timeout: 1000, retryAfter: '3', elapsed: 6000 },
438 + { timeout: 25000, retryAfter: '10', elapsed: 25000 },
439 + ])(
440 + 'does not restart client retries after a $timeout ms timeout',
441 + async ({ timeout, retryAfter, elapsed }) => {
442 + vi.useFakeTimers();
443 + const fetch = vi.fn<typeof globalThis.fetch>(
444 + async () =>
445 + new Response(JSON.stringify({ error: { message: 'slow down' } }), {
446 + status: 429,
447 + headers: { 'content-type': 'application/json', 'retry-after': retryAfter },
448 + })
449 + );
450 + const client = new ComposioClient({
451 + auth: Auth.project('test-api-key'),
452 + baseURL: 'https://backend.invalid',
453 + transport: createFetchTransport({ fetch }),
454 });
452 -
453 - await vi.advanceTimersByTimeAsync(5999);
454 - expect(settled).toBe(false);
455 -
456 - await vi.advanceTimersByTimeAsync(1);
457 - expect(await result).toBeInstanceOf(ConnectionRequestTimeoutError);
458 - expect(fetch).toHaveBeenCalledTimes(3);
459 - });
455 + let settled = false;
456 + const result = createConnectionRequest(client, connectedAccountId)
457 + .waitForConnection(timeout)
458 + .catch(error => error)
459 + .finally(() => {
460 + settled = true;
461 + });
462 +
463 + await vi.advanceTimersByTimeAsync(elapsed - 1);
464 + expect(settled).toBe(false);
465 +
466 + await vi.advanceTimersByTimeAsync(1);
467 + expect(await result).toBeInstanceOf(ConnectionRequestTimeoutError);
468 + expect(fetch).toHaveBeenCalledTimes(3);
469 + }
470 + );
471
472 it('keeps its usual delay when a 429 has no usable Retry-After', async () => {
473 mockClient.connectedAccounts.retrieve.mockRejectedValue(
@@ -467,7 +478,7 @@ describe('ConnectionRequest', () => {
478 await vi.advanceTimersByTimeAsync(1000);
479
480 expect(await result).toBeInstanceOf(ConnectionRequestTimeoutError);
470 - expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(2);
481 + expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(1);
482 });
483
484 it('still fails on other API errors', async () => {