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

Consider different way of inheriting handles in child processes on Windows #38227

Open
Tracked by #3
retep998 opened this issue Dec 7, 2016 · 5 comments
Open
Tracked by #3
Labels
A-process Area: std::process and std::env C-feature-request Category: A feature request, i.e: not implemented / a PR. O-windows Operating system: Windows T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@retep998
Copy link
Member

retep998 commented Dec 7, 2016

Currently to avoid handles being inherited incorrectly by child processes due to race conditions, Rust wraps the whole blob of code in a mutex to synchronize it. However, it will still accidentally inherit any handles created elsewhere that are inheritable, can cause race conditions with people creating processes using libraries other than std, and isn't very efficient.

By switching from STARTUPINFO to STARTUPINFOEX, we can pass a value for lpAttributeList and that attribute list can specify an explicit list of handles to inherit. For more information on this, see https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873

Unfortunately this does require vista, so XP users will lose out.

@alexcrichton alexcrichton added the O-windows Operating system: Windows label Dec 9, 2016
@alexcrichton
Copy link
Member

This seems like a worthwhile implementation to me. We've got lots of fallbacks all over the place to support older OSes, but supporting XP isn't required so I'd also be fine with a patch to just switch to this.

This would also bring Windows in line with Unix, which essentially is already using the equivalent of this.

@ikostia
Copy link

ikostia commented Nov 14, 2018

What is the API vision here? Does std::process::Command::spawn decide which handles to pass down by itself or can an engineer ask for a handle to be inherited explicitly?

@retep998
Copy link
Member Author

Ideally it would know which of the stdio handles it would have to pass down by itself, but would also provide an API for the programmer to ask for additional handles to be inherited.

@ChrisDenton
Copy link
Member

This will be enabled by #88193 but it would be good if this had a dedicated API. It'd be nice to do this by default too but that may be too breaking.

@rustbot label +T-libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 9, 2022
facebook-github-bot pushed a commit to facebook/buck2 that referenced this issue May 26, 2022
Summary:
When test runner waits for `buck2` process it hangs because buckd process is still running.

This happens because buckd inherits handles from parent process and wait on the parent process awaits for these handles to be closed.

Example: https://stackoverflow.com/questions/13243807/popen-waiting-for-child-process-even-when-the-immediate-child-has-terminated

`Popen` in Python has `close_fds` argument to solve this:

> On Windows, if close_fds is true then no handles will be inherited by the child process unless explicitly passed in the handle_list element of STARTUPINFO.lpAttributeList, or by standard handle redirection.
https://docs.python.org/3/library/subprocess.html

Rust sets `bInheritHandles` to `TRUE` unconditionally: https://github.com/rust-lang/rust/blob/acb5c16fa8acf7fd3b48fc218881f006577bab1a/library/std/src/sys/windows/process.rs#L327

There are several open issues related to this:
- rust-lang/rust#54760
- rust-lang/rust#38227

Because of that we have to call `CreateProcessW` ourselves.

Implementation is inspired by:
- std library: https://github.com/rust-lang/rust/blob/master/library/std/src/sys/windows/process.rs
- subprocess library: https://github.com/hniksic/rust-subprocess/blob/master/src/win32.rs
- docs for `CreateProcessW`: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw

Reviewed By: stepancheg

Differential Revision: D36663797

fbshipit-source-id: 14431002a763042f9c160b8a7ff7f8a62dd6befa
@matu3ba
Copy link

matu3ba commented Jan 7, 2023

This would also bring Windows in line with Unix, which essentially is already using the equivalent of this.

No, Unixoid semantics are worse:
Before clone() and until parent closes the to be inherited descriptor there is plenty of time to leak.
This would make the mutex obsolte on Windows, but still necessary on Linux.

At least joshtripplet hinted on some Linux Kernel experiments he is doing related to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-process Area: std::process and std::env C-feature-request Category: A feature request, i.e: not implemented / a PR. O-windows Operating system: Windows T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants