Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Two unresolved moderate issues affect lock contention and regression-test reliability.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR fixes a TOCTOU race in MemoryRuntimeManager by serializing runtime lifecycle operations and adding concurrency coverage.
Changes:
- Added locking around acquisition, swapping, and release.
- Added a concurrent close-count regression test.
File summaries
| File | Summary |
|---|---|
memind-server/src/main/java/com/openmemind/ai/memory/server/runtime/MemoryRuntimeManager.java |
Serializes runtime lifecycle operations. Moderate issue: Memory.close() runs while the lock is held, potentially blocking operations (2 votes). |
memind-server/src/test/java/com/openmemind/ai/memory/server/runtime/MemoryRuntimeManagerTest.java |
Adds concurrent swap/acquire coverage. Moderate issue: the test does not reliably force the required interleaving (3 votes). |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+71
to
+75
| lock.lock(); | ||
| try { | ||
| handle.inFlightRequests().decrementAndGet(); | ||
| tryClose(handle); | ||
| } |
Comment on lines
+106
to
+109
| start.countDown(); | ||
| for (int version = 2; version <= 20; version++) { | ||
| manager.swap(new TestMemory(), MemoryBuildOptions.defaults(), version); | ||
| } |
This branch has not been deployed
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.
What changed
Serialized
acquire(),swap(), andrelease()inMemoryRuntimeManagerwith aReentrantLock, closing the TOCTOU window between thedrainingcheck and theinFlightRequestsincrement inacquire().Why
acquire()previously did check-then-act across two separate atomics (drainingandinFlightRequests). A thread could observedraining == false, then another thread'sswap()could setdrainingand close the underlying memory (when in-flight was 0) before the first thread incremented. The first thread then releases a lease on an already-closed handle, andrelease()→tryClose()closes it a second time. With the lock, the increment and the drain/close are mutually exclusive, so a handle is closed at most once and no lease is handed out for a draining handle.The previous
while (true)+ re-check loop inacquire()became unnecessary:swap()atomically removes the old handle fromcurrent(viagetAndSet) before settingdraining, socurrentcan never reference a draining handle.How verified
MemoryRuntimeManagerTeststill cover the lease lifecycle and swap ordering.concurrentAcquireDuringSwapClosesOldRuntimeExactlyOnce, which runs concurrentacquire()loops against repeatedswap()s and asserts the old runtime'sclose()is invoked exactly once (the buggy path could invoke it twice).Note: the CI workflow runs
mvn clean install -Dmaven.test.skip=trueplus a license-header check, so this test is not exercised by CI — it runs withmvn -pl memind-server testlocally.Related
Fixes #84