about summary refs log tree commit diff
path: root/library/std/src/sys/windows
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-07-05 11:04:17 +0000
committerbors <bors@rust-lang.org>2023-07-05 11:04:17 +0000
commitdfe0683138de0959b6ab6a039b54d9347f6a6355 (patch)
treef115d4a7de5b7a02151d745f44674adc31cb7ebd /library/std/src/sys/windows
parent99f7d368c0ed753db797ee82e89b5a2b7e49509a (diff)
parent9e5f61fcdd29936938d401c0f0055b9af32caf5c (diff)
downloadrust-dfe0683138de0959b6ab6a039b54d9347f6a6355.tar.gz
rust-dfe0683138de0959b6ab6a039b54d9347f6a6355.zip
Auto merge of #112594 - ChrisDenton:process=-kill, r=Amanieu
Return `Ok` on kill if process has already exited

This will require an FCP from `@rust-lang/libs-api.`

Fixes #112423. See that issue for more details.
Diffstat (limited to 'library/std/src/sys/windows')
-rw-r--r--library/std/src/sys/windows/process.rs11
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(())
     }