-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
cargo run
kills child processes upon exit
#5598
Comments
It's currently intended that we kill all child processes. That was originally intended, however, so that ctrl-c on Cargo would behave the same way on Windows as it does on Unix by default (kill the process tree). I'm actually not sure why everything is killed on exit though. I think that's mainly because if a subprocess failed we want to be sure to clean up after it, but if everything was successful then leaking seems fine |
This breaks RustGnuplot on Windows. It spawns a persistent gnuplot process showing all the plots that is meant to outlive the spawning Rust program, but since cargo appears to kill everything, it closes all the plots. This works fine on Linux. |
Ok thinking about this for a bit and looking through the history, the main motivation for adding the job objects in the first place was emulating the same behavior on Unix and Windows for ctrl-c. That doesn't have to do with successful execution, however! I think that instead what we want to do is to when control flow of the program reaches the destructor for the job object we instead just remove all processes from the job object or otherwise configure the job object to not terminate everything on closure. I believe this'll emulate the same behavior on Unix and Windows (and what I'd expect as the "naive intuitive behavior" as well). That way if you ctrl-c on both platforms you'll kill everything, and otherwise everything will remain running that's otherwise running. The fix here needs to go in two locations: Both those locations will need to be tweaked to, in the destructor, reconfigure the job object to not kill all processes or otherwise remove all processes from the job object. |
Reading the linked code for the job object in Cargo, I see that in the job object destructor, processes are killed manually with the If we want all running processes to keep running when cargo exits successfully, then all we would need to do is remove the call to the |
@Seeker14491 job objects were originally added to make ctrl-c work across platforms. On Unix ctrl-c kills the process group, not a single process, which means everything gets taken down. On Windows however only one process killed, and by using job objects we also guaranteed unix-like behavior of the whole group of processes being killed. The initial implementation simply let the job object kill everything on Windows, even on success. This was a bug at the time and didn't really come up till much later (now). When implementing leaking processes it simply preserved the previous behavior, killing everything on all exits. All that to say, no, there shouldn't be anything that Cargo should kill on successful exits. It should continue to kill everything on abnormal exits though! |
@alexcrichton,
The correct behavior would be:
Does this sound correct? |
@Seeker14491 that sounds about right yeah! I don't know how we can configure Cargo to not kill |
Don't kill child processes on normal exit on Windows Fix for #5598
Could you also not kill processes on ctrl+c when |
@hlavaatch idealy we would yeah! I unfortunately don't know how to personally do that myself though :( |
But I do! :) What happens is Windows console sends ctrl-c to all processes associated with the given window. There is a crate for handling ctrl-c called ctrlc
With this hack cargo run no longer interferes with the ctrl-c handling of the crate that is being run :) |
@hlavaatch could you create a new issue and link to it here? |
Is this expected behaviour?
Running this with
cargo run
openscmd.exe
for only one second, but running directly viatarget/release/project.exe
opens it forever.The text was updated successfully, but these errors were encountered: