Skip to content
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

Fix CTRL_CLOSE, CTRL_LOGOFF and CTRL_SHUTDOWN on windows #7122

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Finomnis
Copy link
Contributor

Fixes #7039.

Motivation

According to https://learn.microsoft.com/en-us/windows/console/handlerroutine#remarks, the windows signals CTRL_CLOSE, CTRL_LOGOFF and CTRL_SHUTDOWN immediately kill the process upon being handled.

The intention is that the handler of those signals performs cleanup tasks, and once completed, it returns and the process is killed.

This behavior is sadly not really compatible with an async system that is based on signals.

Solution

There are multiple possible ways to solve this; the easiest one being to simply never return from the handler thread. This works because Rust calls ExitProcess after its main (to my knowledge). I tested it on a project of mine, and it seems to work indeed.

@Finomnis Finomnis force-pushed the fix_windows_signals branch from 77cf12e to 6e60886 Compare January 25, 2025 05:22
@Finomnis Finomnis force-pushed the fix_windows_signals branch from 6e60886 to 44a84f2 Compare January 25, 2025 05:25
@maminrayej maminrayej added A-tokio Area: The main tokio crate M-signal Module: tokio/signal labels Jan 25, 2025
@Finomnis Finomnis requested a review from Darksonn January 27, 2025 21:33
@dsherret
Copy link

Thanks for doing this. I tested this with CTRL_CLOSE and it works for me. FWIW, this is the same solution that libuv does: https://github.com/libuv/libuv/blob/23632e9104b6506d5bee0d85b807c2351617d226/src/win/signal.c#L130

@Finomnis
Copy link
Contributor Author

@dsherret Actually, it doesn't. It only sleeps if someone handles the event, we should do that as well.

@Finomnis
Copy link
Contributor Author

Finomnis commented Jan 30, 2025

@dsherret would you mind testing the PR again now?

@dsherret
Copy link

Yup, still works.

@Finomnis
Copy link
Contributor Author

Finomnis commented Feb 1, 2025

@Darksonn Ready for another review

@Darksonn
Copy link
Contributor

Darksonn commented Feb 1, 2025

Why do we handle all three this way when libuv only does it for one of them?

@Finomnis
Copy link
Contributor Author

Finomnis commented Feb 1, 2025

Why do we handle all three this way when libuv only does it for one of them?

Why does libuv only handle one?
They say something about services, do we have a separate windows service interface?

@Finomnis
Copy link
Contributor Author

Finomnis commented Feb 1, 2025

@Darksonn Either way, the other two signals are completely pointless for Tokio if we don't handle them like this, according to the windows api docs. They also result in an immediate kill after returning from the handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-signal Module: tokio/signal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tokio::signals::windows - are windows signals correctly handled?
4 participants