fix: atomic token cache swap to eliminate race condition

Replaced _token_cache.clear() + _token_cache.update(new_map) with an
atomic reference swap (_token_cache = dict(new_map)). The two-step
mutate approach had a window where the dict was empty — any request
hitting the reader at line 428 during that window would see zero
candidates and return 401.

Python's GIL makes the reference assignment atomic: readers always see
either the old fully-populated dict or the new one, never an empty state.
This commit is contained in:
isharak7m
2026-09-12 10:30:40 +05:30
parent 9d5c031914
commit 984337b35b
+3 -2
View File
@@ -313,6 +313,7 @@ if AUTH_ENABLED:
def _refresh_token_cache():
"""Rebuild the prefix→[(id,hash)] map from the DB."""
global _token_cache
from collections import defaultdict
new_map = defaultdict(list)
db = SessionLocal()
@@ -331,8 +332,8 @@ if AUTH_ENABLED:
new_map[r.token_prefix].append((r.id, r.token_hash, owner_key, scopes))
finally:
db.close()
_token_cache.clear()
_token_cache.update(new_map)
_token_cache = dict(new_map)
app.state._token_cache = _token_cache
app.state._token_cache_dirty = False
# Headers that prove a request was forwarded by a proxy/tunnel (cloudflared,