mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-09-14 04:02:22 +02:00
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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user