fix(storage): acquire lock in InMemoryBackend.get/get_last (#145) - #155
Closed
itsnevu wants to merge 3 commits into
Closed
fix(storage): acquire lock in InMemoryBackend.get/get_last (#145)#155itsnevu wants to merge 3 commits into
itsnevu wants to merge 3 commits into
Conversation
Add regression test for lock acquisition in get() and get_last() methods to prevent TOCTOU race conditions.
Ensure backend lock is held during read operations to prevent race conditions.
Owner
lavneethora
pushed a commit
to lavneethora/provena
that referenced
this pull request
Aug 28, 2026
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.
Summary
InMemoryBackend.get()andget_last()readself._recordswithout holdingself._lock, unlike every other method on the backend (append,count,query,all_records,add_annotation,get_annotations). This is a TOCTOU race:get_last()'sif self._records/self._records[-1]can raiseIndexErrorunder a concurrentappend(), and on non-CPython runtimes the read can observe a partially-initialized dict.Change
Wrap both reads in
with self._lock:, exactly as the issue's Expected behavior describes.Testing
Added
TestInMemoryBackend::test_get_and_get_last_hold_lock, which wraps the real lock and asserts both methods enter it. Fails onmain, passes with the fix.ruff check src/ tests/passesruff format --check src/ tests/passesmypy src/provena/passespytestpasses with no failuresCloses #145