Repository navigation
Conversation
request_device_authorization, poll_device_token, and each poll of wait_for_device_token run the credential's client authentication and the request under one asyncio.wait_for, so the timeout bounds the whole round trip, as Go does with one context.WithTimeout. httpx's timeout alone applies to each phase of the request, and the credential call had no bound. Running out of time raises NetworkError, so poll until done stops with DevicePollError and a doubled interval. An exception the credential raises propagates unchanged (unit row 27). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Findings marked 🟡 are optional suggestions and need no follow-up push.
Additional findings (outside the current diff — GitHub can't attach inline comments there):
-
🟡
packages/oauth/src/keycardai/oauth/client.py— nit: The wait_for_device_token docstring now misdescribes timeouts. It still callstimeoutonly "the httpx timeout of each poll". It also says any credential failure "propagates as is", but a credential hang now raises DevicePollError. Fix: say thattimeoutbounds the credential call plus the request for each poll. Also add a credential timeout to theDevicePollErrorentry, and limit theExceptionentry to errors the credential itself raises.Why this was flagged
The docstring at client.py:1469-1472 says
timeoutis the httpx per-phase timeout of each poll. Args at 1488 says "Timeout for each request". Raises at 1509-1510 says the credential's failure "propagates as is, withoutinterval". After this change,_authenticate_and_sendruns the credential insideasyncio.wait_for(run(), limit)(client.py:1294). When the credential outlasts the limit, the loop gets NetworkError and theexcept NetworkErrorbranch at client.py:1558 raisesDevicePollError(interval * 2, e). The PR updated the sibling docstrings for request_device_authorization and poll_device_token but not this one. Callers reading it would expect a slow credential to propagate unchanged, but they get DevicePollError instead.Verification: Lines 1469-1472 still say "
timeoutis the httpx timeout of each poll (connect, read, write, and pool)". But_authenticate_and_sendruns the credential call and the send insideasyncio.wait_for(run(), limit)and turns a timeout into NetworkError, which line 1558 re-raises as DevicePollError. So a hanging credential raises DevicePollError, which the Raises entries at 1505-1510 leave out.
| try: | ||
| return await asyncio.wait_for(run(), limit) | ||
| except asyncio.TimeoutError as e: |
There was a problem hiding this comment.
🔴 On Python 3.10 and 3.11, a cancellation that lands just as a device request finishes is silently lost. The polling loop then keeps calling the credential and sending polls. asyncio.wait_for on those versions returns the inner result when the inner task is already done at cancel time, which swallows the CancelledError. Fix: bound the call so outer cancellation always propagates, for example with asyncio.timeout on 3.11+ and a cancel-aware fallback on 3.10. This covers request_device_authorization, poll_device_token and each wait_for_device_token poll.
Why this was flagged
pyproject.toml sets requires-python = ">=3.10". In CPython 3.10 and 3.11, asyncio.wait_for catches CancelledError and, when fut.done(), does return fut.result() without re-raising. That code was rewritten only in 3.12. _authenticate_and_send (client.py:1294-1296) now wraps every device request in asyncio.wait_for. Suppose a caller cancels wait_for_device_token, for example through asyncio.timeout or a TaskGroup, in the same loop iteration the poll response completes with authorization_pending. The cancellation is consumed, the loop takes the continue path, and it sleeps, calls the credential and polls again. The docstring at client.py:1467-1469 promises it will not do that. The caller's deadline is missed until the device code expires or the user approves. On the base branch the credential and _send_device_request were awaited directly, so the CancelledError always reached the caller. The await asyncio.sleep(0) guard at client.py:1548 does not help, because the cancellation was already swallowed.
Verification: On 3.10 and 3.11, wait_for runs fut as a separate task. Its except CancelledError: if fut.done(): return fut.result() path returns the result without re-raising, which drops the cancellation (bpo-42130 / gh-86296). The diff adds return await asyncio.wait_for(run(), limit) at client.py:1295. This is a regression. The base had no wait_for anywhere in packages/oauth/src.
| limit = timeout or self.config.timeout | ||
|
|
||
| async def run() -> "HttpResponse | _ClientAuthenticationFailure": | ||
| try: | ||
| client_auth = await authenticator.client_authentication( | ||
| issuer=issuer, endpoint=endpoint | ||
| ) | ||
| except Exception as e: | ||
| return _ClientAuthenticationFailure(e) | ||
| return await self._send_device_request(build(client_auth), limit, operation) | ||
|
|
||
| try: | ||
| return await asyncio.wait_for(run(), limit) |
There was a problem hiding this comment.
🟡 (optional) Callers whose timeout is an httpx.Timeout object, not a float, now get a TypeError from every device grant call. On the base branch those calls work. limit = timeout or self.config.timeout now goes to asyncio.wait_for, which compares it with 0, while the base passed it only to httpx, which accepts httpx.Timeout. Fix: reduce limit to a number before wait_for, for example by taking the largest phase of an httpx.Timeout or by validating the value up front with a ConfigError, so the httpx transport still gets the original value.
Why this was flagged
I could not list who sets the timeout. ClientConfig (types/models.py:491) is a plain dataclass with timeout: float = 30.0 and does no validation. HttpxAsyncTransport passes the value straight to httpx at _transports.py:143 and :182, and httpx accepts httpx.Timeout(10, connect=2). So a config that sets one works for every other AsyncClient operation (for example client.py:863 and :1089). After this change, _authenticate_and_send (client.py:1283-1295) calls asyncio.wait_for(run(), limit). On 3.12+ that evaluates timeout <= 0, and on 3.10/3.11 it adds the timeout to loop.time(). Both raise TypeError before any credential call or request, and leave run() un-awaited, which emits a RuntimeWarning. request_device_authorization and poll_device_token raise that bare TypeError. wait_for_device_token raises it too, not DevicePollError. The float annotation does not prevent this, because nothing checks it at runtime.
Verification: Trigger: a caller passes a value the public API does not declare, an httpx.Timeout object, as ClientConfig.timeout or as the per-call timeout. client.py:1294 calls await asyncio.wait_for(run(), limit). With an httpx.Timeout, wait_for raises TypeError. The except asyncio.TimeoutError at 1295 does not catch that TypeError. On the base, httpx accepts httpx.Timeout.
Release previewkeycardai-oauth 0.35.2 → 0.36.0 (minor)
No other package releases. This comment was automatically generated by the release preview workflow. |
The device grant's
timeoutnow bounds the credential's client authentication and the request together. Linear: ECO-524. Follows the review of keycard-sdk-spec #109.request_device_authorization,poll_device_token, and each poll ofwait_for_device_tokenrun both steps under oneasyncio.wait_for. Go does the same with onecontext.WithTimeout. Before this change, thetimeoutwent only to httpx, which applies it to each phase of a request, and the credential call had no bound.timeoutNetworkError.wait_for_device_tokenstops withDevicePollErrorand a doubledinterval.TimeoutErrororNetworkErrorTests
TimeoutErrororNetworkErrorcomes back unchanged from poll once, the device authorization request, and the loop.DevicePollErrorwithinterval10.httpx.ReadTimeoutorasyncio.TimeoutErroras the cause, because both timeouts are 0.2 seconds.🤖 Generated with Claude Code