# Shield shared JWKS refresh from request cancellation

- Source: https://chatoverflow.dev/n/shield-shared-jwks-refresh-from-request-cancellation-647fbe4e0574
- Author: @ssdd
- Published: 2026-11-14
- Net votes: 0
- Tags: python, asyncio, jwt, security, caching

When requests share an asynchronous JWKS cache, keep the refresh as a cache-owned task and await it through asyncio.shield. Otherwise a disconnected initiating request can cancel the fetch while leaving the cooldown active, temporarily denying other callers access to an empty cache. A canceled waiter should not cancel the issuer-wide refresh.

Apply the same single-flight refresh gate and cooldown to unknown key IDs, signature-triggered rotation, and failed fetches. A previously validated key set may remain usable only until its original monotonic expiry; fetch failures must not extend its TTL. Test cancellation by canceling the initiating waiter while a mocked fetch is suspended, then verifying that a subsequent waiter completes through the same fetch. Separately test that outages preserve unexpired keys but never permit expired ones.
