Skip to content

Commit 2872391

Browse files
committed
Don't wait forever for subprocess
1 parent ccb7681 commit 2872391

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

rustls-libssl/tests/runner.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::io::Read;
2+
use std::ops::Add;
23
use std::process::{Child, Command, Output, Stdio};
34
use std::{fs, net, sync::atomic, thread, time};
45

@@ -334,7 +335,7 @@ fn server() {
334335
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
335336
curl(port);
336337

337-
let openssl_output = print_output(openssl_server.take_inner().wait_with_output().unwrap());
338+
let openssl_output = print_output(openssl_server.wait_with_timeout());
338339

339340
let mut rustls_server = KillOnDrop(Some(
340341
Command::new("tests/maybe-valgrind.sh")
@@ -353,7 +354,7 @@ fn server() {
353354
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
354355
curl(port);
355356

356-
let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
357+
let rustls_output = print_output(rustls_server.wait_with_timeout());
357358
assert_eq!(openssl_output, rustls_output);
358359
}
359360

@@ -399,7 +400,7 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
399400
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
400401
connect(port, key_type, sig_algs, version_flag);
401402

402-
let openssl_output = print_output(openssl_server.take_inner().wait_with_output().unwrap());
403+
let openssl_output = print_output(openssl_server.wait_with_timeout());
403404

404405
let mut rustls_server = KillOnDrop(Some(
405406
Command::new("tests/maybe-valgrind.sh")
@@ -418,7 +419,7 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
418419
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
419420
connect(port, key_type, sig_algs, version_flag);
420421

421-
let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
422+
let rustls_output = print_output(rustls_server.wait_with_timeout());
422423
assert_eq!(openssl_output, rustls_output);
423424
}
424425

@@ -541,6 +542,26 @@ impl KillOnDrop {
541542
fn take_inner(&mut self) -> Child {
542543
self.0.take().unwrap()
543544
}
545+
546+
fn wait_with_timeout(&mut self) -> Output {
547+
let mut child = self.take_inner();
548+
549+
// close stdin in case child is waiting for us
550+
child.stdin.take();
551+
552+
let timeout_secs = 30;
553+
let deadline = time::SystemTime::now().add(time::Duration::from_secs(timeout_secs));
554+
555+
loop {
556+
if time::SystemTime::now() > deadline {
557+
panic!("subprocess did not end within {timeout_secs} seconds");
558+
}
559+
if child.try_wait().expect("subprocess broken").is_some() {
560+
return child.wait_with_output().unwrap();
561+
};
562+
thread::sleep(time::Duration::from_millis(500));
563+
}
564+
}
544565
}
545566

546567
impl Drop for KillOnDrop {

0 commit comments

Comments
 (0)