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
sync.Cond.Signal() does not respect wait generations #1648
Labels
Comments
Owner changed to @rsc. Status changed to Accepted. |
Here is the order of events for getting both locked up: A) Thread 0 executes, stops in cv.wait() B) Thread 1 executes, sets state to 1, calls cv.signal() C) Thread 2 executes, stops in cv.wait() D) Thread 2 unblocks, due to signal from (B) E) Thread 2 loops, blocks again on cv.wait() Deadlock, with two goroutines dead. In this case, not even Broadcast will help. There's actually an improvement we can make to avoid (C) happening when Signal() takes the number of goroutines from 1 to 0, but in the end it still boils down to the expectation of which goroutine Signal() is awakening. That said, please post the snippet you have with real code. I'll have a look at it. Owner changed to @niemeyer. Status changed to Thinking. |
Here is the repro. Please put it into src/sync/cond_test.go after fixing the issue. import ( . "sync" "testing" "runtime" ) func TestCondWaitGenerations(t *testing.T) { procs := runtime.GOMAXPROCS(4) for i := 0; i < 1000; i ++ { var m Mutex c := NewCond(&m) state := 0 go func() { m.Lock(); for state == 0 { c.Wait() } state = 2 c.Signal() m.Unlock() }() go func() { m.Lock() state = 1 c.Signal() m.Unlock() }() go func() { for true { m.Lock() if (state != 0) { for state != 2 { c.Wait() } } else { break } m.Unlock() } }() } runtime.GOMAXPROCS(procs) } |
The fix is up for review, with a simpler test case: http://golang.org/cl/4524083 Note that your example above has a bug: it's breaking out of the for loop with a mutex held. Status changed to Started. |
This issue was closed by revision 17bfa32. Status changed to Fixed. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: