Skip to content

Commit 5d1a951

Browse files
tianonalexbrainman
authored andcommitted
runtime: treat CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT as SIGTERM on Windows
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 (moby/moby#25982 (comment)). 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: 9e05d63 GitHub-Pull-Request: #33311 Reviewed-on: https://go-review.googlesource.com/c/go/+/187739 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent d865b5c commit 5d1a951

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

src/os/signal/doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ will cause os.Interrupt to be sent on the channel, and the program will
211211
not exit. If Reset is called, or Stop is called on all channels passed
212212
to Notify, then the default behavior will be restored.
213213
214+
Additionally, if Notify is called, and Windows sends CTRL_CLOSE_EVENT,
215+
CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT to the process, Notify will
216+
return syscall.SIGTERM. Unlike Control-C and Control-Break, Notify does
217+
not change process behavior when either CTRL_CLOSE_EVENT,
218+
CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT is received - the process will
219+
still get terminated unless it exits. But receiving syscall.SIGTERM will
220+
give the process an opportunity to clean up before termination.
221+
214222
Plan 9
215223
216224
On Plan 9, signals have type syscall.Note, which is a string. Calling

src/runtime/defs_windows.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ const (
4141
DUPLICATE_SAME_ACCESS = C.DUPLICATE_SAME_ACCESS
4242
THREAD_PRIORITY_HIGHEST = C.THREAD_PRIORITY_HIGHEST
4343

44-
SIGINT = C.SIGINT
45-
CTRL_C_EVENT = C.CTRL_C_EVENT
46-
CTRL_BREAK_EVENT = C.CTRL_BREAK_EVENT
44+
SIGINT = C.SIGINT
45+
SIGTERM = C.SIGTERM
46+
CTRL_C_EVENT = C.CTRL_C_EVENT
47+
CTRL_BREAK_EVENT = C.CTRL_BREAK_EVENT
48+
CTRL_CLOSE_EVENT = C.CTRL_CLOSE_EVENT
49+
CTRL_LOGOFF_EVENT = C.CTRL_LOGOFF_EVENT
50+
CTRL_SHUTDOWN_EVENT = C.CTRL_SHUTDOWN_EVENT
4751

4852
CONTEXT_CONTROL = C.CONTEXT_CONTROL
4953
CONTEXT_FULL = C.CONTEXT_FULL

src/runtime/defs_windows_386.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ const (
1515
_DUPLICATE_SAME_ACCESS = 0x2
1616
_THREAD_PRIORITY_HIGHEST = 0x2
1717

18-
_SIGINT = 0x2
19-
_CTRL_C_EVENT = 0x0
20-
_CTRL_BREAK_EVENT = 0x1
18+
_SIGINT = 0x2
19+
_SIGTERM = 0xF
20+
_CTRL_C_EVENT = 0x0
21+
_CTRL_BREAK_EVENT = 0x1
22+
_CTRL_CLOSE_EVENT = 0x2
23+
_CTRL_LOGOFF_EVENT = 0x5
24+
_CTRL_SHUTDOWN_EVENT = 0x6
2125

2226
_CONTEXT_CONTROL = 0x10001
2327
_CONTEXT_FULL = 0x10007

src/runtime/defs_windows_amd64.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ const (
1515
_DUPLICATE_SAME_ACCESS = 0x2
1616
_THREAD_PRIORITY_HIGHEST = 0x2
1717

18-
_SIGINT = 0x2
19-
_CTRL_C_EVENT = 0x0
20-
_CTRL_BREAK_EVENT = 0x1
18+
_SIGINT = 0x2
19+
_SIGTERM = 0xF
20+
_CTRL_C_EVENT = 0x0
21+
_CTRL_BREAK_EVENT = 0x1
22+
_CTRL_CLOSE_EVENT = 0x2
23+
_CTRL_LOGOFF_EVENT = 0x5
24+
_CTRL_SHUTDOWN_EVENT = 0x6
2125

2226
_CONTEXT_CONTROL = 0x100001
2327
_CONTEXT_FULL = 0x10000b

src/runtime/defs_windows_arm.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ const (
1616
_DUPLICATE_SAME_ACCESS = 0x2
1717
_THREAD_PRIORITY_HIGHEST = 0x2
1818

19-
_SIGINT = 0x2
20-
_CTRL_C_EVENT = 0x0
21-
_CTRL_BREAK_EVENT = 0x1
19+
_SIGINT = 0x2
20+
_SIGTERM = 0xF
21+
_CTRL_C_EVENT = 0x0
22+
_CTRL_BREAK_EVENT = 0x1
23+
_CTRL_CLOSE_EVENT = 0x2
24+
_CTRL_LOGOFF_EVENT = 0x5
25+
_CTRL_SHUTDOWN_EVENT = 0x6
2226

2327
_CONTEXT_CONTROL = 0x10001
2428
_CONTEXT_FULL = 0x10007

src/runtime/os_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@ func ctrlhandler1(_type uint32) uint32 {
894894
switch _type {
895895
case _CTRL_C_EVENT, _CTRL_BREAK_EVENT:
896896
s = _SIGINT
897+
case _CTRL_CLOSE_EVENT, _CTRL_LOGOFF_EVENT, _CTRL_SHUTDOWN_EVENT:
898+
s = _SIGTERM
897899
default:
898900
return 0
899901
}

src/syscall/types_windows.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ const (
176176
FILE_MAP_READ = 0x04
177177
FILE_MAP_EXECUTE = 0x20
178178

179-
CTRL_C_EVENT = 0
180-
CTRL_BREAK_EVENT = 1
179+
CTRL_C_EVENT = 0
180+
CTRL_BREAK_EVENT = 1
181+
CTRL_CLOSE_EVENT = 2
182+
CTRL_LOGOFF_EVENT = 5
183+
CTRL_SHUTDOWN_EVENT = 6
181184
)
182185

183186
const (

0 commit comments

Comments
 (0)