go: runtime: handling of CTRL_CLOSE_EVENT seems broken

What version of Go are you using (go version)?

go version go1.15.2 windows/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

set GO111MODULE= set GOARCH=amd64 set GOBIN=D:\Apps\Go\work\bin set GOCACHE=C:\Users\ncruc\AppData\Local\go-build set GOENV=C:\Users\ncruc\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=D:\Apps\Go\work\pkg\mod set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=D:\Apps\Go\work set GOPRIVATE= set GOPROXY=https://proxy.golang.org,direct set GOROOT=D:\Apps\Go\root set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=D:\Apps\Go\root\pkg\tool\windows_amd64 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD= set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ncruc\AppData\Local\Temp\go-build526790066=/tmp/go-build -gno-record-gcc-switches

What did you do?

I have a MCVE on https://gist.github.com/ncruces/20dbdc73d0da6e211ee56b68c2240bae

Open a new console window, build and run the example. Then close the console window and check the log file it creates next to the executable.

go build -o sig.exe gist.github.com/ncruces/20dbdc73d0da6e211ee56b68c2240bae.git
sig.exe

What did you expect to see?

Given cl/187739 has been merged, I expected to see a SIGTERM being received by signal.Notify, and having 5 to 20s to handle cleanup.

Specifically I want to see these log lines:

2020/10/09 13:36:31 Cleaning up...
2020/10/09 13:36:32 Exited

What did you see instead?

Either SIGTERM isn’t being received at all, or we’re not given any time to cleanup.


The MCVE also includes code to call SetConsoleCtrlHandler (which shouldn’t be needed given cl/187739).

You can activate it by running either of:

sig.exe -handle
sig.exe -handle -block

The MCVE works consistently if the handler blocks giving the rest of the program time to gracefully terminate.

This is consistent with the documentation:

So, I’m assuming that’s what needs to be changed, this return needs to become a select {} or similar:

https://github.com/golang/go/blob/c0dded04f7ded5048b44200078a1f723f5e1bcc1/src/runtime/os_windows.go#L1010-L1012

I could easily do a PR, but I’m not well versed with building go from source and testing the change.

Maybe commenters from #7479 can help: @alexbrainman, @tianon?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (13 by maintainers)

Most upvoted comments

@alexbrainman @dmitshur @bradfitz @ianlancetaylor - This has a working/reviewed fix now – https://go-review.googlesource.com/c/go/+/261057 – and it fixes a bug. Is this something we can consider submitting for 1.16?