-
Notifications
You must be signed in to change notification settings - Fork 18k
time: constructing your own time.Ticker used to work but now fails #21874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We should also document whether constructing your own timer manually is supported. In my own code, I have always done: t := time.NewTimer(0)
<-t.C since I was not sure whether manual construction was supported or not. |
Alright, I can confirm that this CL https://go-review.googlesource.com/34784 aka commit 76f4fd8 at 76f4fd8#diff-67606dc98d3216ed00b49367a70885ecL128 introduced this bug. /cc @valyala |
CL coming up in a few minutes. |
Change https://golang.org/cl/63970 mentions this issue: |
@odeke-em, with all due respect &time.Ticker{C: c} creates a ticker with a zero value timer, and the timer can't be started, correct? When Stop() is called, would it not be better to do nothing? In other words, return early if the timer is stopped, rather than lazily starting the timer only in order to stop it? |
@mdcnz that's polite of you. No worries, it's a great question, and that's the purpose of healthy discourse, thank you for the comment! In deed, it creates a zero value timer, and like yours, @ianlancetaylor also suggested the same on the review. My rationale for following the entire addtimer routine was to preserve an almost similar code path to the former code(since that's what it did before), and ensure that the runtimeBucket indexed by the goroutine's ID was being cleaned up. However, I'll be updating the CL to follow yours and @ianlancetaylor same suggestion. |
Thanks for your response @odeke-em, nice one. |
People sometimes build their own
time.Ticker
values in order to inject atime.Ticker
value into some other function. This lets them control when values are sent on the channel, in order to write tests that work more quickly.Whether we think this is a good strategy or not, it did work with 1.9. It fails on current tip. Sample program:
Running this program crashes:
It should be possible to fix this to avoid breaking people's programs unnecessarily.
CC @valyala
The text was updated successfully, but these errors were encountered: