Fix concurrent map write in db.sync() method #697
Merged
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
db.sync()
method by adding missing mutex locksSnapshotReader()
methodProblem 1: Concurrent Map Write (Critical)
The
db.maxLTXFileInfos.m
map was being accessed without mutex protection in two locations:delete(db.maxLTXFileInfos.m, 0)
db.maxLTXFileInfos.m[0]
This caused sporadic "fatal error: concurrent map writes" panics during tests.
Problem 2: PageSize Race (Minor)
The
db.pageSize
field was being read directly inSnapshotReader()
without synchronization while it could be written duringinit()
. This is unlikely to occur in practice since:pageSize
is only written once during initializationint
which is typically atomic on most architecturesHowever, it's technically a race according to Go's memory model, and the fix is trivial (use the existing thread-safe
PageSize()
method), so it's worth fixing for correctness.Solution
SnapshotReader()
to usedb.PageSize()
instead of direct field accessTest plan
TestDB_ConcurrentMapWrite
to reproduce the race conditionsgo test -race
🤖 Generated with Claude Code