storage: filesystem, fix concurrent access to index map#2139
Open
AriehSchneier wants to merge 1 commit into
Open
storage: filesystem, fix concurrent access to index map#2139AriehSchneier wants to merge 1 commit into
AriehSchneier wants to merge 1 commit into
Conversation
Fix race conditions in ObjectStorage where s.index map was accessed without proper mutex protection in multiple locations, leading to concurrent read/write races. Changes: - Document muI and muP mutex fields - Reindex(): Add mutex protection around s.index = nil - PackfileWriter(): Add mutex protection in Notify callback when writing to s.index[h] - EncodedObject(): Add read lock when checking if s.index != nil - HashesWithPrefix(): Copy index values to slice while holding lock before iteration to avoid races - buildPackfileIters(): Add read lock when accessing s.index[h] in closure Add TestConcurrentIndexAccess to reproduce the race condition. The test fails without mutex fixes and passes with them. Run with -race to detect data races. Assisted-by: Claude Sonnet 4.5 <[email protected]> Signed-off-by: Arieh Schneier <[email protected]>
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.
Fix race conditions in ObjectStorage where the
s.indexmap was accessed without proper mutex protection in multiple locations.This PR extracts and focuses on just the index map race fixes from PR #2094. The other fixes in that PR have been superseded by:
Changes
storage/filesystem/object.go:
muIandmuPmutex fields to clarify what they protectReindex(): Add mutex protection arounds.index = nilPackfileWriter(): Add mutex protection in Notify callback when writing tos.index[h]EncodedObject(): Add read lock when checking ifs.index != nilHashesWithPrefix(): Copy index values to slice while holding lock before iterationbuildPackfileIters(): Add read lock when accessings.index[h]in closurestorage/filesystem/object_race_test.go (new):
TestConcurrentIndexAccessto reproduce the index map race conditionsgo test -race -run TestConcurrentIndexAccess ./storage/filesystem/Note on Test Output
The test may still report race conditions from external dependencies (e.g., memfs package). This is expected - those are separate issues outside the scope of this PR. The s.index races that this PR fixes are now resolved.
Testing
Supersedes #2094