Skip to content

Commit 27b9b5e

Browse files
committed
Don't wait forever for subprocess
1 parent 33bf0c4 commit 27b9b5e

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::sync::atomic;
45
use std::{fs, net, thread, time};
@@ -333,7 +334,7 @@ fn server() {
333334
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
334335
curl(port);
335336

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

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

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

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

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

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

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

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

545566
impl Drop for KillOnDrop {

0 commit comments

Comments
 (0)