Repository navigation
fix(memory): prevent race condition in MemoryBlock._ensure_loaded() (#102) - #109
Open
chiragjangiir wants to merge 1 commit into
Open
chiragjangiir wants to merge 1 commit into
chiragjangiir wants to merge 1 commit into
Conversation
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.
Closes #102
Root Cause
MemoryBlock._ensure_loaded()insession/memory.pyused an unprotected check-then-act pattern:When multiple worker threads call
get(),all(), oras_prompt_block()concurrently on the sameMemoryBlockinstance (such as in multi-worker sessions sharing memory blocks), both threads could enter the branch beforeself._loadedwas set toTrue. This resulted in duplicate SQLite queries, duplicateProjectMemory.load()calls, and redundant upserts.Changes
self._load_lock = threading.Lock()inMemoryBlock.__init__._ensure_loaded()using double-checked locking:self._loadedis already True (zero lock contention on subsequent reads).self._load_lockand verifynot self._loadedbefore loading and populatingself._cache.test_ensure_loaded_concurrent_single_loadintests/test_phase12.pyto verify that concurrent calls to_ensure_loaded()across multiple threads only callstore.get_memoryandpm.loadexactly once.Verification