Avoid race in diode.Close with waiter (#481)
This commit is contained in:
parent
e218d18951
commit
315967f32d
|
@ -30,6 +30,14 @@ func TestNewWriter(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
buf := bytes.Buffer{}
|
||||
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {})
|
||||
log := zerolog.New(w)
|
||||
log.Print("test")
|
||||
w.Close()
|
||||
}
|
||||
|
||||
func Benchmark(b *testing.B) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
|
|
|
@ -39,7 +39,12 @@ func NewWaiter(d Diode, opts ...WaiterConfigOption) *Waiter {
|
|||
|
||||
go func() {
|
||||
<-w.ctx.Done()
|
||||
|
||||
// Mutex is strictly necessary here to avoid a race in Next() (between
|
||||
// w.isDone() and w.c.Wait()) and w.c.Broadcast() here.
|
||||
w.mu.Lock()
|
||||
w.c.Broadcast()
|
||||
w.mu.Unlock()
|
||||
}()
|
||||
|
||||
return w
|
||||
|
|
Loading…
Reference in New Issue