diff options
| author | Chris Denton <chris@chrisdenton.dev> | 2023-06-13 20:32:31 +0100 |
|---|---|---|
| committer | Chris Denton <chris@chrisdenton.dev> | 2023-07-01 01:38:39 +0100 |
| commit | e7fda447e7d05b6ca431fc8fe8f489b1fda810bc (patch) | |
| tree | bb9f25205187a9c742f4514b34a410e23d7d2e92 /library/std/src/sys/windows | |
| parent | 2ca8d358e55bc56755b597ea96b557232ef8bc86 (diff) | |
| download | rust-e7fda447e7d05b6ca431fc8fe8f489b1fda810bc.tar.gz rust-e7fda447e7d05b6ca431fc8fe8f489b1fda810bc.zip | |
Return `Ok` on kill if process has already exited
Diffstat (limited to 'library/std/src/sys/windows')
| -rw-r--r-- | library/std/src/sys/windows/process.rs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/library/std/src/sys/windows/process.rs b/library/std/src/sys/windows/process.rs index a573a05c39c..e3493cbb850 100644 --- a/library/std/src/sys/windows/process.rs +++ b/library/std/src/sys/windows/process.rs @@ -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(()) } |
