Skip to content
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

Return Ok on kill if process has already exited #112594

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,8 +1904,8 @@ impl FromInner<imp::ExitCode> for ExitCode {
}

impl Child {
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
/// error is returned.
/// Forces the child process to exit. If the child has already exited, `Ok(())`
/// is returned.
ChrisDenton marked this conversation as resolved.
Show resolved Hide resolved
///
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function.
///
Expand All @@ -1920,7 +1920,7 @@ impl Child {
///
/// let mut command = Command::new("yes");
/// if let Ok(mut child) = command.spawn() {
/// child.kill().expect("command wasn't running");
/// child.kill().expect("command couldn't be killed");
/// } else {
/// println!("yes command didn't start");
/// }
Expand Down
15 changes: 15 additions & 0 deletions library/std/src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,18 @@ fn run_canonical_bat_script() {
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "Hello, fellow Rustaceans!");
}

#[test]
fn terminate_exited_process() {
let mut cmd = if cfg!(target_os = "android") {
let mut p = shell_cmd();
p.args(&["-c", "true"]);
p
} else {
known_command()
};
let mut p = cmd.stdout(Stdio::null()).spawn().unwrap();
p.wait().unwrap();
assert!(p.kill().is_ok());
assert!(p.kill().is_ok());
}
7 changes: 2 additions & 5 deletions library/std/src/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,12 +719,9 @@ impl Process {
pub fn kill(&mut self) -> io::Result<()> {
// If we've already waited on this process then the pid can be recycled
// and used for another process, and we probably shouldn't be killing
// random processes, so just return an error.
// random processes, so return Ok because the process has exited already.
if self.status.is_some() {
Err(io::const_io_error!(
ErrorKind::InvalidInput,
"invalid argument: can't kill an exited process",
))
Ok(())
} else {
cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
}
Expand Down
7 changes: 2 additions & 5 deletions library/std/src/sys/unix/process/process_vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,9 @@ impl Process {
pub fn kill(&mut self) -> io::Result<()> {
// If we've already waited on this process then the pid can be recycled
// and used for another process, and we probably shouldn't be killing
// random processes, so just return an error.
// random processes, so return Ok because the process has exited already.
if self.status.is_some() {
Err(io::const_io_error!(
ErrorKind::InvalidInput,
"invalid argument: can't kill an exited process",
))
Ok(())
} else {
cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
}
Expand Down
11 changes: 10 additions & 1 deletion library/std/src/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,16 @@ pub struct Process {

impl Process {
pub fn kill(&mut self) -> io::Result<()> {
cvt(unsafe { c::TerminateProcess(self.handle.as_raw_handle(), 1) })?;
let result = unsafe { c::TerminateProcess(self.handle.as_raw_handle(), 1) };
if result == c::FALSE {
let error = unsafe { c::GetLastError() };
// TerminateProcess returns ERROR_ACCESS_DENIED if the process has already been
// terminated (by us, or for any other reason). So check if the process was actually
// terminated, and if so, do not return an error.
if error != c::ERROR_ACCESS_DENIED || self.try_wait().is_err() {
return Err(crate::io::Error::from_raw_os_error(error as i32));
}
}
Ok(())
}

Expand Down