Skip to content

Commit b86c601

Browse files
authored
Rollup merge of #146030 - ChrisDenton:wait-timeout, r=tgross35
Fix `sys::process::windows::tests::test_thread_handle` spurious failure Instead of sleeping, wait for the process to finish so that we can be sure it's done. We use a timeout because otherwise this test can be stuck indefinitely if it fails (unfortunately std doesn't currently have a way to wait with a timeout so a manual OS API call is necessary). I also changed the test to run `whoami` and pipe the output to null so that it doesn't clutter up the test output. Fixes #146024
2 parents 9489339 + 3516e25 commit b86c601

File tree

1 file changed

+18
-6
lines changed
  • library/std/src/sys/process/windows

1 file changed

+18
-6
lines changed

library/std/src/sys/process/windows/tests.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::{Arg, make_command_line};
22
use crate::env;
33
use crate::ffi::{OsStr, OsString};
4-
use crate::process::Command;
4+
use crate::os::windows::io::AsHandle;
5+
use crate::process::{Command, Stdio};
56

67
#[test]
78
fn test_raw_args() {
@@ -29,19 +30,30 @@ fn test_thread_handle() {
2930
use crate::os::windows::process::{ChildExt, CommandExt};
3031
const CREATE_SUSPENDED: u32 = 0x00000004;
3132

32-
let p = Command::new("cmd").args(&["/C", "exit 0"]).creation_flags(CREATE_SUSPENDED).spawn();
33+
let p = Command::new("whoami").stdout(Stdio::null()).creation_flags(CREATE_SUSPENDED).spawn();
3334
assert!(p.is_ok());
34-
let mut p = p.unwrap();
35+
36+
// Ensure the process is killed in the event something goes wrong.
37+
struct DropGuard(crate::process::Child);
38+
impl Drop for DropGuard {
39+
fn drop(&mut self) {
40+
let _ = self.0.kill();
41+
}
42+
}
43+
let mut p = DropGuard(p.unwrap());
44+
let p = &mut p.0;
3545

3646
unsafe extern "system" {
37-
fn ResumeThread(_: BorrowedHandle<'_>) -> u32;
47+
unsafe fn ResumeThread(hHandle: BorrowedHandle<'_>) -> u32;
48+
unsafe fn WaitForSingleObject(hHandle: BorrowedHandle<'_>, dwMilliseconds: u32) -> u32;
3849
}
3950
unsafe {
4051
ResumeThread(p.main_thread_handle());
52+
// Wait until the process exits or 1 minute passes.
53+
// We don't bother checking the result here as that's done below using `try_wait`.
54+
WaitForSingleObject(p.as_handle(), 1000 * 60);
4155
}
4256

43-
crate::thread::sleep(crate::time::Duration::from_millis(100));
44-
4557
let res = p.try_wait();
4658
assert!(res.is_ok());
4759
assert!(res.unwrap().is_some());

0 commit comments

Comments
 (0)