Skip to content

Self-hosted AI workspace.

Python

fix: atomic token cache swap to eliminate race condition

isharak7m984337b35bdc
diff --git a/app.py b/app.py
index bb4f51ffb..80eb83c85 100644
--- a/app.py
+++ b/app.py
@@ -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,