-
Notifications
You must be signed in to change notification settings - Fork 13k
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-c causing reads of stdin to return empty on Windows. #89433
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
Can you add an ui test for this? (see this for how to add one yourself) Ideally we would want to make sure this behavior is consistent across all the platforms that we support. It would probably look like this file, although I am not sure how to programmatically send Ctrl-C to a process... (and there might be platforms that do not care about Ctrl-Cs) |
I think a test will be difficult -- maybe a run-make test with a I'm going to r? @dtolnay since this seems like it'll require a T-libs-api (or so) FCP, as a breaking change. |
Yes, adding a test is rather difficult. Windows doesn't really have a The closest thing seems to be setting up separate process group for a subprocess, then calling the Windows |
This PR closes an inconsistency between how the standard library implements reading from stdin on Windows vs other platforms. #89177 (comment) is the clearest example of the discrepancy: ctrl+C during a read would previously cause the read to return on Windows, but not other platforms, while the ctrl+C handler runs. I don't believe we want to keep this as a platform-specific inconsistency. As for which behavior to go with, I would biasedly say the non-Windows behavior seems better to me, which this PR makes the Windows implementation consistent with. @rust-lang/libs-api |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
This seems okay to me from a consistency perspective, but I wonder how other ecosystems handle this? Has anyone done a survey? |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I tested a few different languages on Windows 10 with the program:
The behavior of the readline call after pressing ctrl-c are a mixture on Windows:
All test programs have the same behavior on Linux (keep blocking, no error). For consistency, converging on "keep blocking, no error" (as this PR does) makes the most sense to me. The alternative of returning an error in Rust can cause problems if no signal handler is set up, since Windows kills the thread after a few milliseconds, sometimes causing a half-printed error message. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@bors r+ |
📌 Commit 273e522 has been approved by |
Fix ctrl-c causing reads of stdin to return empty on Windows. Pressing ctrl+c (or ctrl+break) on Windows caused a blocking read of stdin to unblock and return empty, unlike other platforms which continue to block. On ctrl-c, `ReadConsoleW` will return success, but also set `LastError` to `ERROR_OPERATION_ABORTED`. This change detects this case, and re-tries the call to `ReadConsoleW`. Fixes rust-lang#89177. See issue for further details. Tested on Windows 7 and Windows 10 with both MSVC and GNU toolchains
…askrgr Rollup of 6 pull requests Successful merges: - rust-lang#89390 (Fix incorrect Box::pin suggestion) - rust-lang#89433 (Fix ctrl-c causing reads of stdin to return empty on Windows.) - rust-lang#89823 (Switch order of terms to prevent overflow) - rust-lang#89865 (Allow static linking LLVM with ThinLTO) - rust-lang#89873 (Add missing word to `FromStr` trait documentation) - rust-lang#89878 (Fix missing remaining compiler specific cfg information) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Pressing ctrl+c (or ctrl+break) on Windows caused a blocking read of stdin to unblock and return empty, unlike other platforms which continue to block.
On ctrl-c,
ReadConsoleW
will return success, but also setLastError
toERROR_OPERATION_ABORTED
.This change detects this case, and re-tries the call to
ReadConsoleW
.Fixes #89177. See issue for further details.
Tested on Windows 7 and Windows 10 with both MSVC and GNU toolchains