Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
fix: add locking around timers to avoid race
Browse files Browse the repository at this point in the history
add locking around access and updates to the stopped field.
  • Loading branch information
lyondhill authored Nov 8, 2019
1 parent 6228dc3 commit 1c54274
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,13 @@ func (t *Timer) Stop() bool {
return t.timer.Stop()
}

t.mock.mu.Lock()
registered := !t.stopped
t.mock.mu.Unlock()

t.mock.removeClockTimer((*internalTimer)(t))
t.mock.mu.Lock()
defer t.mock.mu.Unlock()
t.stopped = true
return registered
}
Expand All @@ -268,12 +273,14 @@ func (t *Timer) Reset(d time.Duration) bool {
}

t.next = t.mock.now.Add(d)
t.mock.mu.Lock()
defer t.mock.mu.Unlock()

registered := !t.stopped
if t.stopped {
t.mock.mu.Lock()
t.mock.timers = append(t.mock.timers, (*internalTimer)(t))
t.mock.mu.Unlock()
}

t.stopped = false
return registered
}
Expand All @@ -288,6 +295,9 @@ func (t *internalTimer) Tick(now time.Time) {
t.c <- now
}
t.mock.removeClockTimer((*internalTimer)(t))
t.mock.mu.Lock()
defer t.mock.mu.Unlock()

t.stopped = true
gosched()
}
Expand Down

0 comments on commit 1c54274

Please sign in to comment.