[cargo run] Should proxy signals to child process #2343
Description
From watchexec/cargo-watch#25, around this comment.
Also referring this short IRC conversation.
Background: Cargo Watch has improved behaviour when watching a Cargo project and running cargo run
: on changes, it kills the child cargo run
process and starts a new one. This is highly helpful when developing web servers and other applications which never return. For prior art, see nodemon.
Cargo Watch currently uses the stdlib's Child#kill()
method on Windows, and libc::kill()
on all other platforms (Unix), with a signal of 15 (SIGTERM).
Problem: cargo run
creates a new child process. Killing the cargo run
child does not kill the grand-child. Cargo Watch then proceeds to start a new cargo-run
process, which attempts to start a new grand-child. We now have two of the target process, which is obviously unexpected and wrong. In the case of webservers, the first target process is still bound to its TCP port, preventing latter target processes from functioning, producing EADDRINUSE errors.
Proposal: To have cargo run
proxy signals to its own child process, allowing Cargo Watch (and other applications opening cargo
child commands) to send a SIGTERM (on Unix) or taskkill (on Windows, through the stdlib's implementation) directly to the target process. The target would then be responsible for its own shutdown.
Cargo Watch would not attempt to send further signals, instead waiting for the cargo run
process to return normally.
cargo-run
may want to SIGKILL its child if it does not seem to be responding to SIGTERM, but I will leave that up to Cargo maintainers.
I am also not familiar with the behaviours on Windows, so cannot comment on that aspect.