Repository navigation
[Server] Add an opt-in per-session lock - #582
Closed
chr-hertel wants to merge 4 commits into
Closed
chr-hertel wants to merge 4 commits into
chr-hertel wants to merge 4 commits into
Conversation
Concurrent requests of one session no longer overwrite each other's changes to different keys.
Concurrent requests of one session take turns instead of losing each other's changes to the same key.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Concurrent saves, resumed fibers, session destruction, and lease expiry still leave serialization gaps.
4 open findings
What changed in this PR
Adds concurrency protections for session-backed protocol state through incremental saves and an opt-in per-session lock.
Changes:
- Merges changed session keys onto freshly loaded data.
- Adds configurable Symfony-based session locking.
- Adds concurrency tests and usage documentation.
| File | Description |
|---|---|
CHANGELOG.md |
Records concurrency improvements. |
composer.json |
Suggests and tests with symfony/lock. |
docs/run/sessions.md |
Documents concurrent requests and lock setup. |
src/Server/Builder.php |
Exposes session-lock configuration. |
src/Server/Protocol.php |
Applies locking around protocol session operations. |
src/Server/Session/Session.php |
Tracks and merges changed keys. |
src/Server/Session/SessionLockInterface.php |
Defines the locking contract. |
src/Server/Session/SymfonySessionLock.php |
Implements Symfony-backed locking. |
tests/Unit/Server/ProtocolSessionLockTest.php |
Tests protocol locking behavior. |
tests/Unit/Server/Session/Fixture/FiberSessionLock.php |
Simulates locking between fibers. |
tests/Unit/Server/Session/SessionTest.php |
Tests changed-key merge semantics. |
tests/Unit/Server/Session/SymfonySessionLockTest.php |
Tests lock acquisition and release. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| 'session_id' => $sessionId->toRfc4122(), | ||
| ]); | ||
| // Taken under the lock, from a fresh read, before the transport resumes the fiber with it. | ||
| $responseData = $this->locked($sessionId, fn (): mixed => $this->consumeResponse($requestId, $sessionId)); |
Comment on lines
+59
to
+63
| if (null !== $this->changes) { | ||
| $data = $this->load(); | ||
| foreach (array_keys($this->changes) as $key) { | ||
| $this->apply($data, (string) $key); | ||
| } |
Member
Author
|
Closed in favor of #584 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Stacked on #572 - that one keeps disjoint keys, but read-modify-write on the same key (a counter in a tool handler,
_mcp.pending_requests,_mcp.outgoing_queue) still loses updates under parallel requests. Follows the lock idea from #275.Builder::setSessionLock()with a smallSessionLockInterface, off by defaultSymfonySessionLockon top ofsymfony/lock(optional dependency), bounded wait =>TimeoutExceptionProtocolholds the lock from loading to saving the session; a suspended request saves and returns, so the lock is free while it waits on the client - the polling loop locks again only to take a response or queued messagesFixes chr-hertel#58 (together with #572)
🤖 Generated with Claude Code