fix(cache): lazy-init TTL cache locks to survive event-loop turnover

A module-level asyncio.Lock binds to the loop it was first awaited on.
Under pytest-anyio (and xdist) each test spins up a new loop; any later
test that hit /health or /config would wait on a lock owned by a dead
loop and the whole worker would hang.

Create the lock on first use and drop it in the test-reset helpers so a
fresh loop always gets a fresh lock.
This commit is contained in:
2026-04-17 16:23:00 -04:00
parent 4ea1c2ff4f
commit 45039bd621
2 changed files with 11 additions and 3 deletions

View File

@@ -24,6 +24,9 @@ _state_locks: dict[str, asyncio.Lock] = {}
def _reset_state_cache() -> None:
"""Reset cached config state — used by tests."""
_state_cache.clear()
# Drop any locks bound to the previous event loop — reusing one from
# a dead loop deadlocks the next test.
_state_locks.clear()
async def _get_state_cached(name: str) -> Optional[dict[str, Any]]: