Skip to content

Commit

Permalink
Avoid race in diode.Close with waiter (rs#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddn0 authored and pablitoc committed Apr 7, 2023
1 parent a0f0556 commit 88a20ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions diode/diode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions diode/internal/diodes/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 88a20ce

Please sign in to comment.