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

Merged
merged 8 commits into from
Feb 19, 2025

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.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, just some comments on tests.

@Darksonn
Copy link
Contributor

Darksonn commented Feb 5, 2025

cc @ipetkov please review

Copy link
Member

@ipetkov ipetkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the means to test this myself, but since others have commented this works for them I'll trust that its helping!

@Astlaan
Copy link

Astlaan commented Feb 8, 2025

I can test it tomorrow if needed

@Finomnis
Copy link
Contributor Author

Finomnis commented Feb 8, 2025

I can test it tomorrow if needed

Yes, please.

@Astlaan
Copy link

Astlaan commented Feb 11, 2025

Maybe I'm just too tired right now, but shouldn't this work?:
https://github.com/Astlaan/test-tokio-ctrl-close/blob/main/src/main.rs

@dsherret
Copy link

What's the actual behaviour? (I'm AFK atm. My guess is it creates a single file and exits, which is fine I think?)

@Finomnis
Copy link
Contributor Author

Maybe I'm just too tired right now, but shouldn't this work?: https://github.com/Astlaan/test-tokio-ctrl-close/blob/main/src/main.rs

Yes, it should create 4-5 files and then be killed.

I'm not sure why, but in my own tests it seemed to work in cmd, but not in powershell.

@Finomnis
Copy link
Contributor Author

@Astlaan is it the same behavior I described here? #7039 (comment)

@Finomnis
Copy link
Contributor Author

@dsherret You said it works, right? I'd merge anyway if @Astlaan does not respond

@dsherret
Copy link

Yes, it works for me.

By the way, that example correctly creates 4-5 files on both cmd and powershell from windows terminal and windows console host. Also, I see now that 4-5 files is the expected behaviour because there's a 5000ms timeout before windows kills the process.

@Astlaan
Copy link

Astlaan commented Feb 16, 2025

Yes, it works for me.

By the way, that example correctly creates 4-5 files on both cmd and powershell from windows terminal and windows console host. Also, I see now that 4-5 files is the expected behaviour because there's a 5000ms timeout before windows kills the process.

Are you using my code? Because for me I see no files being created at all.

@dsherret
Copy link

Yup. What terminal are you using?

@Astlaan
Copy link

Astlaan commented Feb 16, 2025

Yup. What terminal are you using?

I tried with both cmd and powershell.
I opened them in the repo, then ran "cargo run", click the close button in the powershell, but no files were created.

@dsherret
Copy link

Try cargo build and launch the executable directly. I think maybe cargo run is preventing it from receiving the signal. I’m not at my computer right now, but I ran the executable directly.

Also, is cmd and powershell being launched with windows console host? Does it work for you in windows terminal with cmd and powershell? I think windows console host/windows terminal is what sends the event.

@Astlaan
Copy link

Astlaan commented Feb 16, 2025

Try cargo build and launch the executable directly. I think maybe cargo run is preventing it from receiving the signal. I’m not at my computer right now, but I ran the executable directly.

This was the issue indeed. It runs fine if I run it directly.

@Finomnis
Copy link
Contributor Author

Finomnis commented Feb 19, 2025

In that case, ready for merging, @Darksonn.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@Darksonn Darksonn merged commit a27575f into tokio-rs:master Feb 19, 2025
82 checks passed
@Finomnis Finomnis deleted the fix_windows_signals branch February 19, 2025 15:00
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?
6 participants