-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std::process::Child::kill should not fail if process is terminated on Windows #112423
Comments
It does make sense to return success if the process has already exited. That said...
I'm not sure this makes any sense when put in Windows terms. Wait is more like Also it's important to note that If we want to change this then my proposal would be to simply try to terminate and if that fails (with |
Store a flag in
Good point — I don't know WinAPI well, but seems like this is how it is supposed to be implemented, thanks! |
Perhaps. I admit I'm not terribly keen on trying to emulate the POSIX implementation exactly. This has lead to problems in the past, especially when it leads to inconsistent behaviour within the platform (for example, people using their own wait function(s) now have to know to also implement their own kill function). What is the benefit of erroring here? How would that error be handled? |
Simplicity of writing cross-platform code. Write something on one OS, and it will behave the same on the other OS. Writing cross-platform code is hard because such corner cases needs to be handled properly, and which are not very easy to test (and even figure out what scenarios needs to be tested). That said, I'd prefer to change API on Unix instead to not return error on |
Ok, I'm going to tag this for libs-api discussion. I'm personally uncertain here but I'd love to be convinced. I'll try to summarise below but also see the above discussion. DiscussionThe semantics of Windows and posix implementations are different. It is advantageous to smooth over those differences in the std. However, how to do that is the question. I think the current documentation is wrong here. It says:
The Windows implementation does not return |
This was discussed in a libs-api meeting. The feeling was that it makes sense to simply return |
Child::kill
callskill
on Unix, andTerminateProcess
on Windows.If process is terminated, but handle is alive yet,
kill
is successful on Unix, but fails on Windows.kill
is successful on Unix because the process is zombie until waited (it fails after processwaited
, that's probably OK)According to documentation of
TerminateProcess
So if someone else (not us) terminated the process,
kill
would fail.There are problems with this behavior:
kill
kill
failed, need totry_wait
. And iftry_wait
is successful, ignore the error ofkill
I could reproduce it. I don't have easy access to Windows machine to create completely isolated test case, but repro looks like this:
This fails with:
exactly as described in WinAPI docs.
I think proper
kill
implementation should look like this:TerminateProcess
TerminateProcess
failed, callGetExitCodeProcess
GetExitCodeProcess
is successful and exit code is notSTILL_ACTIVE
, considerkill
successfulThis is how
kill
is implemented in our project:https://github.com/facebook/buck2/blob/b6fb1364e1caf6dac821767b27bda65726e38895/app/buck2_wrapper_common/src/winapi_process.rs#L58-L72
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: