-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Calling time.Tick causes a Ticker to leak because there is no interface to Stop() it.
Here is a demonstration which starts 100,000 go routines each of which calls time.Tick then closes the go routines down again in an orderly fashion
http://play.golang.org/p/zy5X4hAq0I
It sleeps at the end so you can run top or equivalent and see that it will be using 100% of a CPU core keeping the Tickers up to date even though the user no longer has access to them.
This is unfortunate but it was decided in #8001 that it was too difficicult to Stop the Tickers when they are garbage collected.
The documentation for NewTicker got changed after this, but not the documentation for Tick.
It currently reads
Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. Useful for clients that have no need to shut down the ticker.
It doesn't say why you might want to shut the Ticker down.
My suggestion for a re-word is this
Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. Note that this leaks associated resources; use NewTicker and Stop to avoid that.
That is possibly too alarmist, but I wanted the docs to note clearly that using time.Tick is leaking something which can't be recovered.