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

Commit

Permalink
Merge pull request #24 from danielnelson/fix-race-clock-reset
Browse files Browse the repository at this point in the history
Lock clock state to fix race between clock.Add and timer.Reset
  • Loading branch information
danielnelson authored Jun 10, 2020
2 parents 206a2de + 6b1ff0d commit d30813f
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ func (m *Mock) Timer(d time.Duration) *Timer {
}

func (m *Mock) removeClockTimer(t clockTimer) {
m.mu.Lock()
defer m.mu.Unlock()
for i, timer := range m.timers {
if timer == t {
copy(m.timers[i:], m.timers[i+1:])
Expand Down Expand Up @@ -257,12 +255,9 @@ func (t *Timer) Stop() bool {

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
t.mock.mu.Unlock()
return registered
}

Expand All @@ -289,13 +284,13 @@ type internalTimer Timer

func (t *internalTimer) Next() time.Time { return t.next }
func (t *internalTimer) Tick(now time.Time) {
t.mock.mu.Lock()
if t.fn != nil {
t.fn()
} else {
t.c <- now
}
t.mock.removeClockTimer((*internalTimer)(t))
t.mock.mu.Lock()
t.stopped = true
t.mock.mu.Unlock()
gosched()
Expand All @@ -316,7 +311,9 @@ func (t *Ticker) Stop() {
if t.ticker != nil {
t.ticker.Stop()
} else {
t.mock.mu.Lock()
t.mock.removeClockTimer((*internalTicker)(t))
t.mock.mu.Unlock()
}
}

Expand Down

0 comments on commit d30813f

Please sign in to comment.