Skip to content

Commit

Permalink
Fix a data race
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 11, 2019
1 parent bc19d96 commit 80c37d2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/util/deadline/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

// PipeDeadline is an abstraction for handling timeouts.
type PipeDeadline struct {
mu sync.Mutex // Guards timer and cancel
timer *time.Timer
cancel chan struct{} // Must be non-nil
mu sync.Mutex // Guards timer and cancel
timer *time.Timer
cancelMu sync.RWMutex
cancel chan struct{} // Must be non-nil
}

func MakePipeDeadline() PipeDeadline {
Expand Down Expand Up @@ -44,7 +45,9 @@ func (d *PipeDeadline) Set(t time.Time) {
// Time in the future, setup a timer to cancel in the future.
if dur := time.Until(t); dur > 0 {
if closed {
d.cancelMu.Lock()
d.cancel = make(chan struct{})
d.cancelMu.Unlock()
}
d.timer = time.AfterFunc(dur, func() {
close(d.cancel)
Expand All @@ -66,6 +69,9 @@ func (d *PipeDeadline) Wait() chan struct{} {
}

func (d *PipeDeadline) Closed() bool {
d.cancelMu.RLock()
defer d.cancelMu.RUnlock()

select {
case <-d.cancel:
return true
Expand Down

0 comments on commit 80c37d2

Please sign in to comment.