You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
The bool from Stop will tell you if the timer was stopped by the call, so in your example the second call of Stop will return false because the timer had already been stopped and your code will block forever (deadlock) because the channel was not closed (as documented) and the timer will not fire (as documented).
Thanks for the suggestion, but in general there is no need to close an unneeded channel. A channel only needs to be closed if there is some other goroutine waiting to read from the channel and checking whether it has been closed, as in a for/range statement. I don't think the comment in the time package needs to be updated.
What version of Go are you using (
go version
)?go version devel +116da1c Fri Jan 6 20:55:48 2017 +0000 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/nefthy/go-test/"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="x86_64-pc-linux-gnu-gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/home/nefthy/go-test/tmp/go-build490756288=/tmp/go-build -gno-record-gcc-switches"
CXX="x86_64-pc-linux-gnu-g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
What did you do?
full playground example
What did you expect to see?
//Output: OK
According to Spec _, ok := <-t.C will not block, but set ok to false. The Docs for Stop() also suggest using:
And mentions the case of the timer is stopped, so I expect this to work when the timer has already been stopped.
What did you see instead?
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.Brocken()
/tmp/sandbox409514762/main.go:42 +0x5a0
main.main()
/tmp/sandbox409514762/main.go:50 +0x40
The text was updated successfully, but these errors were encountered: