Ensure store server serializes access per thread - #175
Conversation
There was a problem hiding this comment.
Pull Request Overview
Adds an event-loop–aware async lock to the in‑memory store and serializes server delegate access with a server-level lock, updating API routes to funnel through wrapped methods.
- Introduces _LoopAwareAsyncLock to allow reusing the in‑memory store across different event loops (thread-unsafe by design).
- Wraps LightningStoreServer delegate calls with a new locking layer and routes FastAPI endpoints through the server methods.
- Adds a test to verify store usability after switching to a new event loop.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/store/test_memory.py | Adds test ensuring the in-memory store lock rebinds correctly when used from a new event loop. |
| agentlightning/store/memory.py | Implements _LoopAwareAsyncLock and replaces prior asyncio.Lock usage in the store. |
| agentlightning/store/client_server.py | Adds per-server lock and routes HTTP handlers through serialized delegate methods. |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| # wait_for_rollouts can block for a long time; avoid holding the lock | ||
| # so other requests can make progress while we wait. | ||
| return await method(*args, **kwargs) | ||
| with self._lock: |
There was a problem hiding this comment.
Using a blocking threading.Lock inside an async code path will block the entire event loop when contention occurs (another request waiting to acquire the lock will hard-block the loop), defeating concurrency and risking stalls. Replace threading.Lock with an asyncio.Lock (initialized as self._lock = asyncio.Lock()) and use async with self._lock: so awaiting the method does not block other tasks from running while they wait for the lock.
| with self._lock: | |
| async with self._lock: |
There was a problem hiding this comment.
This change is intentional to make _call_store_method thread-safe.
Summary
Testing
https://chatgpt.com/codex/tasks/task_e_68f592757158832ea4368388b2b64c79