Skip to content

Commit

Permalink
Add start_kill() and align kill() to Tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Nov 4, 2023
1 parent 7d92289 commit 25d1f62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/tokio/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ impl AsyncGroupChild {
self.imp.into_inner()
}

/// Forces the child process group to exit. If the group has already exited, an [`InvalidInput`]
/// error is returned.
/// Forces the child process group to exit.
///
/// This is equivalent to sending a SIGKILL on Unix platforms.
/// If the group has already exited, an [`InvalidInput`] error is returned.
///
/// **Unlike the Tokio implementation**, this method does not wait for the child process group,
/// and only sends the kill. You’ll need to call [`wait()`](Self::wait) yourself.
/// This is equivalent to sending a SIGKILL on Unix platforms.
///
/// See [the Tokio documentation](Child::kill) for more.
///
Expand All @@ -157,16 +155,33 @@ impl AsyncGroupChild {
///
/// let mut command = Command::new("yes");
/// if let Ok(mut child) = command.group_spawn() {
/// child.kill().expect("command wasn't running");
/// child.kill().await.expect("command wasn't running");
/// } else {
/// println!("yes command didn't start");
/// }
/// # }
/// ```
///
/// [`InvalidInput`]: std::io::ErrorKind::InvalidInput
pub fn kill(&mut self) -> Result<()> {
self.imp.kill()
pub async fn kill(&mut self) -> Result<()> {
self.start_kill()?;
self.wait().await?;
Ok(())
}

/// Attempts to force the child to exit, but does not wait for the request to take effect.
///
/// This is equivalent to sending a SIGKILL on Unix platforms.
///
/// 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.
///
/// See [the Tokio documentation](Child::start_kill) for more.
///
/// [`InvalidInput`]: std::io::ErrorKind::InvalidInput
pub fn start_kill(&mut self) -> Result<()> {
self.imp.start_kill()
}

/// Returns the OS-assigned process group identifier.
Expand Down
2 changes: 1 addition & 1 deletion src/tokio/child/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ChildImp {
killpg(self.pgid, sig).map_err(Error::from)
}

pub fn kill(&mut self) -> Result<()> {
pub fn start_kill(&mut self) -> Result<()> {
self.signal_imp(Signal::SIGKILL)
}

Expand Down
2 changes: 1 addition & 1 deletion src/tokio/child/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ChildImp {
self.inner
}

pub fn kill(&mut self) -> Result<()> {
pub fn start_kill(&mut self) -> Result<()> {
res_bool(unsafe { TerminateJobObject(self.handles.job, 1) })
}

Expand Down

0 comments on commit 25d1f62

Please sign in to comment.