]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: treat CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT as SIGTERM...
authorTianon Gravi <tianon@debian.org>
Wed, 14 Aug 2019 15:19:55 +0000 (15:19 +0000)
committerAlex Brainman <alex.brainman@gmail.com>
Thu, 29 Aug 2019 08:15:20 +0000 (08:15 +0000)
This matches the existing behavior of treating CTRL_C_EVENT, CTRL_BREAK_EVENT as a synthesized SIGINT event.

See https://docs.microsoft.com/en-us/windows/console/handlerroutine for a good documentation source upstream to confirm these values.

As for the usage of these events, the "Timeouts" section of that upstream documentation is important to note, especially the limited window in which to do any cleanup before the program will be forcibly killed (defaults typically 5s, but as low as 500ms, and in many cases configurable system-wide).

These events are especially relevant for Windows containers, where these events (particularly `CTRL_SHUTDOWN_EVENT`) are one of the only ways containers can "gracefully" shut down (https://github.com/moby/moby/issues/25982#issuecomment-466804071).

This was verified by making a simple `main()` which implements the same code as in `ExampleNotify_allSignals` but in a `for` loop, building a `main.exe`, running that in a container, then doing `docker kill -sTERM` on said container.  The program prints `Got signal: SIGTERM`, then exits after the aforementioned timeout, as expected.  Behavior before this patch is that the program gets no notification (and thus no output) but still exits after the timeout.

Fixes #7479

Change-Id: I2af79421cd484a0fbb9467bb7ddb5f0e8bc3610e
GitHub-Last-Rev: 9e05d631b542393f5ebb0eb3747157c8bd0de635
GitHub-Pull-Request: golang/go#33311
Reviewed-on: https://go-review.googlesource.com/c/go/+/187739
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
src/os/signal/doc.go
src/runtime/defs_windows.go
src/runtime/defs_windows_386.go
src/runtime/defs_windows_amd64.go
src/runtime/defs_windows_arm.go
src/runtime/os_windows.go
src/syscall/types_windows.go

index 16f49c7ab8be17d0e9b24261ebc20ed99228f839..2229d36954f096d17af6f3385c984a284477bf9a 100644 (file)
@@ -211,6 +211,14 @@ will cause os.Interrupt to be sent on the channel, and the program will
 not exit. If Reset is called, or Stop is called on all channels passed
 to Notify, then the default behavior will be restored.
 
+Additionally, if Notify is called, and Windows sends CTRL_CLOSE_EVENT,
+CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT to the process, Notify will
+return syscall.SIGTERM. Unlike Control-C and Control-Break, Notify does
+not change process behavior when either CTRL_CLOSE_EVENT,
+CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT is received - the process will
+still get terminated unless it exits. But receiving syscall.SIGTERM will
+give the process an opportunity to clean up before termination.
+
 Plan 9
 
 On Plan 9, signals have type syscall.Note, which is a string. Calling
index 9bd910747640ebc901450aa749cc6133b04e2f64..43f358d56af9f2333f624f6b072691849f19823d 100644 (file)
@@ -41,9 +41,13 @@ const (
        DUPLICATE_SAME_ACCESS   = C.DUPLICATE_SAME_ACCESS
        THREAD_PRIORITY_HIGHEST = C.THREAD_PRIORITY_HIGHEST
 
-       SIGINT           = C.SIGINT
-       CTRL_C_EVENT     = C.CTRL_C_EVENT
-       CTRL_BREAK_EVENT = C.CTRL_BREAK_EVENT
+       SIGINT              = C.SIGINT
+       SIGTERM             = C.SIGTERM
+       CTRL_C_EVENT        = C.CTRL_C_EVENT
+       CTRL_BREAK_EVENT    = C.CTRL_BREAK_EVENT
+       CTRL_CLOSE_EVENT    = C.CTRL_CLOSE_EVENT
+       CTRL_LOGOFF_EVENT   = C.CTRL_LOGOFF_EVENT
+       CTRL_SHUTDOWN_EVENT = C.CTRL_SHUTDOWN_EVENT
 
        CONTEXT_CONTROL = C.CONTEXT_CONTROL
        CONTEXT_FULL    = C.CONTEXT_FULL
index 8c0d6d8b98fdcc373bae98699e44078ca0232538..3c5057b86f0b72fcb070d9133d08de6efcc48ef3 100644 (file)
@@ -15,9 +15,13 @@ const (
        _DUPLICATE_SAME_ACCESS   = 0x2
        _THREAD_PRIORITY_HIGHEST = 0x2
 
-       _SIGINT           = 0x2
-       _CTRL_C_EVENT     = 0x0
-       _CTRL_BREAK_EVENT = 0x1
+       _SIGINT              = 0x2
+       _SIGTERM             = 0xF
+       _CTRL_C_EVENT        = 0x0
+       _CTRL_BREAK_EVENT    = 0x1
+       _CTRL_CLOSE_EVENT    = 0x2
+       _CTRL_LOGOFF_EVENT   = 0x5
+       _CTRL_SHUTDOWN_EVENT = 0x6
 
        _CONTEXT_CONTROL = 0x10001
        _CONTEXT_FULL    = 0x10007
index 42a446d3cdfc7a3e0d7f042d161bedf8d537fd2f..ebb1506e2f50cb29e726fc16e4638ceb280e9df3 100644 (file)
@@ -15,9 +15,13 @@ const (
        _DUPLICATE_SAME_ACCESS   = 0x2
        _THREAD_PRIORITY_HIGHEST = 0x2
 
-       _SIGINT           = 0x2
-       _CTRL_C_EVENT     = 0x0
-       _CTRL_BREAK_EVENT = 0x1
+       _SIGINT              = 0x2
+       _SIGTERM             = 0xF
+       _CTRL_C_EVENT        = 0x0
+       _CTRL_BREAK_EVENT    = 0x1
+       _CTRL_CLOSE_EVENT    = 0x2
+       _CTRL_LOGOFF_EVENT   = 0x5
+       _CTRL_SHUTDOWN_EVENT = 0x6
 
        _CONTEXT_CONTROL = 0x100001
        _CONTEXT_FULL    = 0x10000b
index 049f5b613a74d35c5fb466ef250aa5a3d7a9874b..b275b0572a7e762f277f5bac35922ec57116bcce 100644 (file)
@@ -16,9 +16,13 @@ const (
        _DUPLICATE_SAME_ACCESS   = 0x2
        _THREAD_PRIORITY_HIGHEST = 0x2
 
-       _SIGINT           = 0x2
-       _CTRL_C_EVENT     = 0x0
-       _CTRL_BREAK_EVENT = 0x1
+       _SIGINT              = 0x2
+       _SIGTERM             = 0xF
+       _CTRL_C_EVENT        = 0x0
+       _CTRL_BREAK_EVENT    = 0x1
+       _CTRL_CLOSE_EVENT    = 0x2
+       _CTRL_LOGOFF_EVENT   = 0x5
+       _CTRL_SHUTDOWN_EVENT = 0x6
 
        _CONTEXT_CONTROL = 0x10001
        _CONTEXT_FULL    = 0x10007
index 074ae0f40de8a66444b02d2feea005d913aa9e2d..9540069e11a75fa5d5b6fa0c522a6a2e4a89d0ab 100644 (file)
@@ -894,6 +894,8 @@ func ctrlhandler1(_type uint32) uint32 {
        switch _type {
        case _CTRL_C_EVENT, _CTRL_BREAK_EVENT:
                s = _SIGINT
+       case _CTRL_CLOSE_EVENT, _CTRL_LOGOFF_EVENT, _CTRL_SHUTDOWN_EVENT:
+               s = _SIGTERM
        default:
                return 0
        }
index 0b839339d2f1976dbb546cce4198b8677e668b0e..0349f3b180dea04ab0356a3ed81bc9d01b1ba5b6 100644 (file)
@@ -176,8 +176,11 @@ const (
        FILE_MAP_READ    = 0x04
        FILE_MAP_EXECUTE = 0x20
 
-       CTRL_C_EVENT     = 0
-       CTRL_BREAK_EVENT = 1
+       CTRL_C_EVENT        = 0
+       CTRL_BREAK_EVENT    = 1
+       CTRL_CLOSE_EVENT    = 2
+       CTRL_LOGOFF_EVENT   = 5
+       CTRL_SHUTDOWN_EVENT = 6
 )
 
 const (