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 #18 from lyondhill/master
Browse files Browse the repository at this point in the history
fix: add locking around timers to avoid race
  • Loading branch information
benbjohnson authored Nov 8, 2019
2 parents 6228dc3 + 1c54274 commit 1c0be5e
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 1c0be5e

Please sign in to comment.