Return Ok on kill if process has already exited

This commit is contained in:
Chris Denton 2023-06-13 20:32:31 +01:00
parent 2ca8d358e5
commit e7fda447e7
No known key found for this signature in database
GPG key ID: 713472F2F45627DE
4 changed files with 17 additions and 14 deletions

View file

@ -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(())
}