Avoid race in diode.Close with waiter (#481)

This commit is contained in:
Donald Nguyen 2022-09-15 01:15:42 -07:00 committed by GitHub
parent e218d18951
commit 315967f32d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -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) { func Benchmark(b *testing.B) {
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stderr) defer log.SetOutput(os.Stderr)

View File

@ -39,7 +39,12 @@ func NewWaiter(d Diode, opts ...WaiterConfigOption) *Waiter {
go func() { go func() {
<-w.ctx.Done() <-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.c.Broadcast()
w.mu.Unlock()
}() }()
return w return w