-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
add support for detecting window close events (on Windows) #6808
Conversation
Allows for a potentially more graceful shutdown compared to immediate termination
case windows.CTRL_CLOSE_EVENT: | ||
//NOTE: the OS will terminate our process after receiving this event | ||
// either after `SPI_GETHUNGAPPTIMEOUT` or immediately after we return non-0 | ||
// (whichever is first) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is 5AM influenza English, look at again later.
Edit: applies to all comments
Edit2: and the code too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks!
// call it so it can inject signals from the OS | ||
if nativeNotifier != nil { | ||
nativeNotifier(ih, notify, sigs...) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably go below the ih.wg.Add
call (or maybe just move that goroutine to the top of the function?). Otherwise, we could:
- Receive a window close event before we call
ih.wg.Add
- Call
ih.wg.Wait()
- Succeed.
- Either the process gets killed or we then call
ih.wg.Add(1)
and panic (because we've already called wait on the waitgroup.
if !goIsHandlingIntterupts { | ||
sigChan <- os.Interrupt | ||
} | ||
return processed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If go is handling interrupts, will this cause problems? I wonder if we should just always use this function on windows (skipping the signal.Notify
call).
Note: Will need to see how this behaves in 1.14 and hide this patch behind a build constant for <=1.13. |
1.14 is out |
Since Go 1.14 handles this natively we can just drop this hack. |
WIP fix for #1897
We add a shim
nativeNotifier
who's responsibility is to notify a channel of events/signals not covered by thesignal
pkg.In this case, we implement this to add support for Windows' window events.
When the window receives a
CTRL+C
,CTRL+BREAK
, orWM_CLOSE
event, we will notify the interrupt channel and tell the OS we proccessed the event successfully.This in turn will cancel the node's context for the first event and exit outright upon subsequent interrupts.
Current problem:
When processing
CTRL_C_EVENT
andCTRL_BREAK_EVENT
events, nothing special has to be considered. ButWM_CLOSE
breaks down to aCTRL_CLOSE_EVENT
which has special implications in this context.https://docs.microsoft.com/en-us/windows/console/handlerroutine
When we receive this message, the system will terminate us in either 5 seconds, or when we return from our event handler (whichever is first).
Meaning we can initiate graceful shutdown from any signal, but need a way to wait for shutdown to be completed before returning from the handler, specifically when handling the
CTRL_CLOSE_EVENT
event.i.e. the node has to...
Commands like
shutdown
have direct access to the node and can just call itsClose
method, blocking until the node is finished shutting down.We're a background thread that gets called at some arbitrary point by the user/OS, and don't have access to the node object so we can't just do the same.
This needs to be taken care of before this can progress out of a draft.
Edit:
video showing the signal getting caught