[structural] Module-level mutable globals in llm_core.py create thread-safety issues #709
Labels
No labels
area:chat
area:core
area:llm
area:routes
area:tools
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
refactor
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
sleepy/odysseus#709
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
File:
src/llm_core.pyThe following module-level mutable dicts have no thread-safety protection:
_response_cache(line 42) — read/written by sync and async calls_dead_hosts(line 57) — read/written by concurrent stream handlers_host_fails(line 58) — read/written by concurrent stream handlers_model_activity(line 59) — read/written by concurrent requestsIn production with
asyncio+ThreadPoolExecutor(used inmodel_routes._refresh_caches_bg), these dicts can be mutated concurrently. CPython's GIL protects against corruption but not against race conditions in multi-step operations like the cache eviction (lines 130–134: check length → collect keys → delete).Fix: Use
threading.Lockfor the mutable dicts, or replace with thread-safe alternatives:_response_cache→cachetools.TTLCache(thread-safe)_dead_hosts/_host_fails→ protect with a lock_model_activity→ protect with a lockThis is related to issue #671 (module-level global mutable state) but specific to the LLM core layer.