-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Child process can be spawned even when tokio::process::Command::spawn returns an error #6797
Comments
@ipetkov Thoughts? |
We mirror the behavior of the standard library here: (example of the same behavior with using only
If you want to ensure that a child process is killed whenever its handle is dropped (even during panic unwinding), you can use |
Oh looks like |
The child should probably be wrapped with kill-on-drop as soon as it's spawned, and if everything is successful just unwrap it, so that the If it's ok, I can try implementing it, and making a pull request. |
There's a chance that between spawning of the `std Child`, and wrapping it into a tokio impl an error can occurs, which would leave the child alive, with no way to access it from the user side and not being part of the tokio runtime. This commit fixes that issue by wrapping the `std Child` right after spawning with a struct that implements Drop, where the child would be killed if it's dropped. Fixes: tokio-rs#6797
Version
Tokio v1.39.2
rustc 1.76.0
Platform
WSL2 Ubuntu 20.04.6 LTS
Description
For both unix and windows imp for
tokio::process::Command::spawn
, spawning thestd::process::Child
is the first call. That could leave the child alive even if subsequent function calls fail.In my case a panic happened, because I used the Tokio Child outside the Tokio runtime, which isn't the same as the
spawn
returning an error. But it's still seems possible for an error to happen in the function, which would leave the Child process alive, without the user being able to kill it, or access it in any way.I could be wrong about it though, as I don't know if those functions in that order would never fail. Maybe they could only fail if the child got killed, and the panic is the only way for this to happen. Which would be fine, as the child would then not end up unreachable, but still running. I have tried looking deeper into the source code to check if that's the case, but it gets much deeper than my understanding of tokio/async runtimes.
Minimum code recreation
Calling tokio::process::Command::spawn outside the tokio runtime.
This will cause a panic, but the
ls
command will still output the results to the terminal.How to maybe handle this
I thought of two ways to handle this. Either:
std::process::Child
as part of the error, so the user can handle it how they wantSIGKILL
to the child, to make sure that it's dead when erroringThe text was updated successfully, but these errors were encountered: