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
time.After Sometimes Skips #34889
Comments
If you change the channel There might be some imperfections in the runtime implementation, but your use of time.After is not recommended. There will be more and more goroutines hanging for ever. We should use it like https://play.golang.org/p/ljRAQnREgLk |
Ah, there is a mistake in my description in the last comment. There will be not more and more goroutines hanging for ever. |
I think the difference is normal. For the select-block loop goroutine has a little more initialization overhead than the other loop at each step. |
You are in effect measuring the overhead of the I'm going to close this issue because it is not a bug. If you want to discuss it, I encourage you to use a forum rather than the issue tracker. See https://golang.org/wiki/Questions. |
Okay. But I think you might have missed the second part that I mentioned. Even if I set the time.After and the goroutine timeouts to be 1 second instead of 1 millisecond (in which case the
which I think any makes the use of time.After unreliable |
If you want to discuss this, I encourage you to use a forum rather than the issue tracker. See https://golang.org/wiki/Questions. The |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
From my understanding, time.After is equivalent to running a goroutine that sleeps for the given time period and then writes the time to the channel. It also takes care of channel cleanup for you. So I would expect something like this:
to behave similarly to the following code snippet (+ automatically close the channel for you):
however, from my experimentation, this is not the case. In my sample https://play.golang.org/p/FasIldsACpD, it would produce the same # for both runs but if I try this on any environment (I ran this on both Linux + Mac), I would run into discrepancies. These changes were more noticeable if I ran this on a multi-CPU system and if I ran it for a longer period of time. Running it for 600 seconds would result in something like this on a multi-CPU system:
Even if I set the interval to 1 second (replace time.Millisecond with time.Second), I would still run into discrepancies. After running it for a minute, I got
What did you expect to see?
They should both be the same or a difference of one at most
What did you see instead?
See above
The text was updated successfully, but these errors were encountered: