-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix deadlock on core state lock #10456
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// TestGrabLockOrStopped is a non-deterministic test to detect deadlocks in the | ||
// grabLockOrStopped function. This test starts a bunch of workers which | ||
// continually lock/unlock and rlock/runlock the same RWMutex. Each worker also | ||
// starts a goroutine which closes the stop channel 1/2 the time, which races | ||
// with acquisition of the lock. | ||
func TestGrabLockOrStop(t *testing.T) { | ||
// Stop the test early if we deadlock. | ||
const ( | ||
workers = 100 | ||
testDuration = time.Second | ||
testTimeout = 10*testDuration | ||
) | ||
done := make(chan struct{}) | ||
defer close(done) | ||
var lockCount int64 | ||
go func() { | ||
select{ | ||
case <-done: | ||
case <-time.After(testTimeout): | ||
panic(fmt.Sprintf("deadlock after %d lock count", | ||
atomic.LoadInt64(&lockCount))) | ||
} | ||
}() | ||
|
||
// lock is locked/unlocked and rlocked/runlocked concurrently. | ||
var lock sync.RWMutex | ||
start := time.Now() | ||
|
||
// workerWg is used to wait until all workers exit. | ||
var workerWg sync.WaitGroup | ||
workerWg.Add(workers) | ||
|
||
// Start a bunch of worker goroutines. | ||
for g := 0; g < workers; g++ { | ||
g := g | ||
go func() { | ||
defer workerWg.Done() | ||
for time.Now().Sub(start) < testDuration { | ||
stop := make(chan struct{}) | ||
|
||
// closerWg waits until the closer goroutine exits before we do | ||
// another iteration. This makes sure goroutines don't pile up. | ||
var closerWg sync.WaitGroup | ||
closerWg.Add(1) | ||
go func() { | ||
defer closerWg.Done() | ||
// Close the stop channel half the time. | ||
if time.Now().UnixNano() % 2 == 0 { | ||
close(stop) | ||
} | ||
}() | ||
|
||
// Half the goroutines lock/unlock and the other half rlock/runlock. | ||
if g % 2 == 0 { | ||
if !grabLockOrStop(lock.Lock, lock.Unlock, stop) { | ||
lock.Unlock() | ||
} | ||
} else { | ||
if !grabLockOrStop(lock.RLock, lock.RUnlock, stop) { | ||
lock.RUnlock() | ||
} | ||
} | ||
|
||
closerWg.Wait() | ||
|
||
// This lets us know how many lock/unlock and rlock/runlock have | ||
// happened if there's a deadlock. | ||
atomic.AddInt64(&lockCount, 1) | ||
} | ||
}() | ||
} | ||
workerWg.Wait() | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I tested this by adding an atomic to count how often the close path was taken, I found it was always taken. I suggest instead using something like
rand.Int31()
.