Skip to content

Keep the JWK Set cache when a refresh fetch fails#1159

Open
Nadav0077 wants to merge 1 commit intojpadilla:masterfrom
Nadav0077:preserve-jwks-cache-on-fetch-failure
Open

Keep the JWK Set cache when a refresh fetch fails#1159
Nadav0077 wants to merge 1 commit intojpadilla:masterfrom
Nadav0077:preserve-jwks-cache-on-fetch-failure

Conversation

@Nadav0077
Copy link
Copy Markdown

@Nadav0077 Nadav0077 commented Apr 19, 2026

PyJWKClient.fetch_data currently stores the parsed JWK Set inside a finally clause. When the request raises (URLError, TimeoutError, or a json.load ValueError on a bad response), the local jwk_set is still None, so the finally branch calls jwk_set_cache.put(None) and clears the cache. Any brief outage on the JWKS endpoint wipes a perfectly
valid cached set, forces the next call back onto the network, and turns a single transient failure into a cascading one while the endpoint is recovering.

The fix is to only write to the cache after the parse succeeds. On failure the previously cached set is left untouched and the next non-refresh call is still served from memory.

While looking into this I noticed the existing test, test_get_jwt_set_failed_request_should_clear_cache, wasn't actually
pinning anything. Its only assertion sits inside the pytest.raises block, after a line that always raises, so control leaves the block before the assert executes. The attribute it checks (jwks_client.jwk_set_cache) is the JWKSetCache wrapper, which is
never None once cache_jwk_set is enabled, so even if it did run it wasn't looking at the right thing. I replaced it with
test_get_jwt_set_failed_request_preserves_cache, which primes the cache, triggers a failed refresh, then checks the cached payload is still there and that a follow-up get_jwk_set() doesn't make a new request.

Full test suite passes locally (344 passed, 4 skipped without cryptography installed).

fetch_data writes the parsed JWKS into the cache from inside a finally
block, so any URLError, TimeoutError, or malformed JSON response ends
up calling put(None), which wipes whatever was cached before. A brief
network blip during a refresh leaves the client with nothing, every
subsequent call has to hit the network, and if the endpoint is still
down the failures cascade until it recovers.

Move the cache write out of finally so it only happens after a
successful parse. A failed fetch now leaves the previously cached set
alone.

The existing test meant to pin this area
(test_get_jwt_set_failed_request_should_clear_cache) put its only
assertion inside the pytest.raises block, after a line that always
raises, so the assertion never ran. It also checked the JWKSetCache
wrapper rather than its contents. Replaced it with one that actually
exercises the invariant: after a failed refresh the cached data is
still there and the next non-refresh call is served without another
network request.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents transient JWKS endpoint failures from wiping an otherwise valid in-memory JWK Set cache in PyJWKClient, reducing cascading network retries during brief outages.

Changes:

  • Update PyJWKClient.fetch_data() to write to jwk_set_cache only after a successful fetch + JSON parse.
  • Replace a non-effective test with a new test that primes the cache, triggers a failed refresh, and asserts the cache remains intact and prevents a follow-up network call.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
jwt/jwks_client.py Stops clearing the JWK Set cache on fetch/parsing failures by caching only after success.
tests/test_jwks_client.py Replaces the prior failing-to-assert test with one that verifies cache preservation and no extra network hit.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants