-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
signal::windows::ctrl_close does not work as expected #6735
Comments
Thoughts @ipetkov ? |
I validated on windows that if you wish to do any closing work after receiving CTRL_CLOSE, you must do it in the handler before returning. The return value just stops calling handlers that are registered. BOOL myHook(DWORD event) {
// Say we handled it.
std::cout << "handled" << std::endl;
// If we don't sleep, process is killed immediately regardless of return value.
// E.g, you must do your work in this callback.
Sleep(5000);
return TRUE; // True means stop calling handlers, False calls the next handler.
}
int main()
{
SetConsoleCtrlHandler(myHook, TRUE);
while (true) {
std::cout << "Hello World!\n";
Sleep(1000);
}
} The reason why this differs from ctrl_c is that the ctrl_close, the window is closed before the handlers are called, so the process is about to exit. ctrl_c doesn't close anything, and since we ignore the default handler, it doesn't force close the process there. |
My understanding is/was that returning If that's not the case, then our implementation is incorrect |
Hmm. If this is the case, then I guess we'll need to deprecate |
any ideas about this? |
@dtzxporter Check my comment here: Finomnis/tokio-graceful-shutdown#94 (comment) A call to the ctrlhandler does not freeze the main thread, I tested that as explained in that comment. As such, the closing work can be done in the main thread (outside the handler). The only thing is that you need to prevent the handler's completion before the cleaning work is one (ex. with a synchronization flag, or just by sleeping). I mention 3 possible solutions here (not equally pretty) in the issue I created (before realizing this one existed): |
In the case of the
I mention this in my duplicated issue, along with possible solutions: |
Let's keep the conversation in #7039. I'll close this one as duplicate. I know this issue was first, but the other thread has more information now. Thanks a lot for reporting this! |
Version
v1.39.2
Platform
64-bit (Windows)
Description
Tried writing some code for cross platform graceful shutdown in my project but the windows specific callbacks seem to not work.
I tried this code:
I expected to see this happen:
When I closed the console application my
world.shutdown().await
function would execute which notifies players of the shutdown and saves the server to the database.Instead, this happened:
The process terminates before
world.shutdown().await
can run.The text was updated successfully, but these errors were encountered: