Skip to content

fix: propagate async JWKS fetch cancellation - #910

Open
pcbeingused33 wants to merge 2 commits into
auth0:masterfrom
pcbeingused33:fix/c2105-jwks-cancellation
Open

pcbeingused33 wants to merge 2 commits into
auth0:masterfrom
pcbeingused33:fix/c2105-jwks-cancellation

Conversation

@pcbeingused33

Copy link
Copy Markdown

Changes

AsyncJwksFetcher._fetch_jwks catches asyncio.CancelledError in a bare except:. Cancelling an in-flight key lookup, including through asyncio.wait_for, is swallowed: get_key then forces a second request and raises TokenValidationError ("RSA Public Key ... was not found").

As a result, a timeout on an async token verification reports a missing key instead of a timeout, and the caller keeps waiting for the extra request. With a mocked 1.5 s JWKS request and asyncio.wait_for(fetcher.get_key("k"), 0.2):

Result Requests Elapsed
Before TokenValidationError 2 1.70 s
After TimeoutError 1 0.20 s

The handler now catches Exception, so cancellation propagates while ordinary fetch errors keep their existing behavior. No public API changes.

References

No linked issue; found by reading the code and reproduced with the regression test below.

Testing

tests/authentication/test_async_token_verifier.py adds two network-free tests: cancelling a real task propagates CancelledError after a single request, and an ordinary request error still reports the missing key. The cancellation test fails on the unchanged implementation.

The tests/authentication suite passes (114 tests). Tests under tests/wire were not run because they need the WireMock container. Ruff check passes on both changed files; the formatting differences ruff format --check reports in the source file are on lines this change does not touch.

  • This change adds unit test coverage
  • This change adds integration test coverage
  • This change has been tested on the latest version of the platform/language or why not (Python 3.15)

Checklist

This contribution was generated with AI assistance.