Keep the JWK Set cache when a refresh fetch fails#1159
Open
Nadav0077 wants to merge 1 commit intojpadilla:masterfrom
Open
Keep the JWK Set cache when a refresh fetch fails#1159Nadav0077 wants to merge 1 commit intojpadilla:masterfrom
Nadav0077 wants to merge 1 commit intojpadilla:masterfrom
Conversation
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.
Contributor
There was a problem hiding this comment.
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 tojwk_set_cacheonly 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).