fix: race condition in Tree.FindEntry() and Tree.entry()#1922
Open
majiayu000 wants to merge 2 commits intogo-git:mainfrom
Open
fix: race condition in Tree.FindEntry() and Tree.entry()#1922majiayu000 wants to merge 2 commits intogo-git:mainfrom
majiayu000 wants to merge 2 commits intogo-git:mainfrom
Conversation
Add sync.Once for the entry name map (t.m) in entry() and sync.Mutex for the tree path cache (t.t) in FindEntry() to make concurrent access to the same Tree instance thread-safe. Fixes go-git#1917 Signed-off-by: majiayu000 <1835304752@qq.com>
Remove pre-warming that populated caches before the concurrent phase, making the race detector ineffective. Use top-level entry names and a start barrier (sync.WaitGroup gate) so all goroutines begin simultaneously, exercising concurrent t.m and t.t cache initialization. Signed-off-by: majiayu000 <1835304752@qq.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.
Fixes #1917
Summary
Three data races exist on the
Treestruct whenFindEntry()is called concurrently:FindEntry()readt.t == niland writet.t = make(...)without synchronizationentry()race on the nil-check andbuildMap()callt.mwhile another is still populating it viabuildMap()Approach
sync.Oncefort.m(Races 2 & 3): Replace the nil-check-then-buildMap pattern inentry()witht.mOnce.Do(t.buildMap). buildMap is a one-time lazy init from immutableEntries, sosync.Onceis the idiomatic solution with zero overhead after first call. Reset inDecode()wheret.mis set to nil.sync.Mutexfort.t(Race 1): Protect accesses to the tree path cache with fine-grained locking. I/O operations (tree.dir()) happen outside the lock to preserve concurrency.Both caches are preserved — no performance regression (ref: #1238 was rejected for removing caches, causing 24x regression).
Test Plan
TestFindEntryConcurrent— spawns 10 goroutines callingFindEntryconcurrently on the sameTreeinstance, verifying both same-entry and different-entry access patterns-raceflag