mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
process: make Child::kill async (#2823)
* This changes the `Child::kill` to be an async method which awaits the child after sending a kill signal. This avoids leaving zombie processes on Unix platforms if the caller forgets to await the child after the kill completes * A `start_kill` method was also added on `Child` which only sends the kill signal to the child process. This allows for kill signals to be sent even outside of async contexts.
This commit is contained in:
+23
-13
@@ -774,6 +774,23 @@ impl Child {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempts to force the child to exit, but does not wait for the request
|
||||||
|
/// to take effect.
|
||||||
|
///
|
||||||
|
/// On Unix platforms, this is the equivalent to sending a SIGKILL. Note
|
||||||
|
/// that on Unix platforms it is possible for a zombie process to remain
|
||||||
|
/// after a kill is sent; to avoid this, the caller should ensure that either
|
||||||
|
/// `child.wait().await` or `child.try_wait()` is invoked successfully.
|
||||||
|
pub fn start_kill(&mut self) -> io::Result<()> {
|
||||||
|
match &mut self.child {
|
||||||
|
FusedChild::Child(child) => child.kill(),
|
||||||
|
FusedChild::Done(_) => Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"invalid argument: can't kill an exited process",
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Forces the child to exit.
|
/// Forces the child to exit.
|
||||||
///
|
///
|
||||||
/// This is equivalent to sending a SIGKILL on unix platforms.
|
/// This is equivalent to sending a SIGKILL on unix platforms.
|
||||||
@@ -795,21 +812,14 @@ impl Child {
|
|||||||
/// tokio::spawn(async move { send.send(()) });
|
/// tokio::spawn(async move { send.send(()) });
|
||||||
/// tokio::select! {
|
/// tokio::select! {
|
||||||
/// _ = child.wait() => {}
|
/// _ = child.wait() => {}
|
||||||
/// _ = recv => {
|
/// _ = recv => child.kill().await.expect("kill failed"),
|
||||||
/// child.kill().expect("kill failed");
|
|
||||||
/// // NB: await the child here to avoid a zombie process on Unix platforms
|
|
||||||
/// child.wait().await.unwrap();
|
|
||||||
/// }
|
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
pub fn kill(&mut self) -> io::Result<()> {
|
/// ```
|
||||||
match &mut self.child {
|
pub async fn kill(&mut self) -> io::Result<()> {
|
||||||
FusedChild::Child(child) => child.kill(),
|
self.start_kill()?;
|
||||||
FusedChild::Done(_) => Err(io::Error::new(
|
self.wait().await?;
|
||||||
io::ErrorKind::InvalidInput,
|
Ok(())
|
||||||
"invalid argument: can't kill an exited process",
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Waits for the child to exit completely, returning the status that it
|
/// Waits for the child to exit completely, returning the status that it
|
||||||
|
|||||||
@@ -39,8 +39,7 @@ async fn issue_2174() {
|
|||||||
time::delay_for(Duration::from_secs(1)).await;
|
time::delay_for(Duration::from_secs(1)).await;
|
||||||
|
|
||||||
// Kill the child process.
|
// Kill the child process.
|
||||||
child.kill().unwrap();
|
child.kill().await.unwrap();
|
||||||
let _ = child.wait().await;
|
|
||||||
|
|
||||||
assert_err!(handle.await);
|
assert_err!(handle.await);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user