fix: add read lock to WAL segment Flush to prevent data race with Close#893
Merged
mattisonchao merged 1 commit intomainfrom Feb 23, 2026
Merged
fix: add read lock to WAL segment Flush to prevent data race with Close#893mattisonchao merged 1 commit intomainfrom
mattisonchao merged 1 commit intomainfrom
Conversation
The runSync goroutine calls segment.Flush() without holding any lock, while Close() concurrently calls Unmap() under the write lock. This adds RLock/RUnlock to Flush() so the two operations are properly serialized, and guards against a nil txnMappedFile after Unmap. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Motivation
TestCoordinatorE2Eis flaky due to a data race between the WALrunSyncgoroutine callingsegment.Flush()(which reads the mmap) andwal.Close()callingsegment.Close()→MMap.Unmap()(which writes/invalidates the mmap). The race was latent in the WAL code and surfaced after the follower controller lifecycle refactor in #887 changed shutdown timing.CI failure: https://github.com/oxia-db/oxia/actions/runs/22312993235/job/64549930899
Modification
RLock/RUnlocktoreadWriteSegment.Flush()to synchronize against the write lock already held byClose(). This ensures eitherFlush()completes beforeUnmap()begins, orUnmap()completes first andFlush()seestxnMappedFile == niland returns safely.