Skip to content

Commit

Permalink
Don't wait forever for subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed May 5, 2024
1 parent ce6858f commit 23d7a9c
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions rustls-libssl/tests/runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Read;
use std::ops::Add;
use std::process::{Child, Command, Output, Stdio};
use std::{fs, net, sync::atomic, thread, time};

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

let openssl_output = print_output(openssl_server.take_inner().wait_with_output().unwrap());
let openssl_output = print_output(openssl_server.wait_with_timeout());

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

let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
let rustls_output = print_output(rustls_server.wait_with_timeout());
assert_eq!(openssl_output, rustls_output);
}

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

let openssl_output = print_output(openssl_server.take_inner().wait_with_output().unwrap());
let openssl_output = print_output(openssl_server.wait_with_timeout());

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

let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
let rustls_output = print_output(rustls_server.wait_with_timeout());
assert_eq!(openssl_output, rustls_output);
}

Expand Down Expand Up @@ -541,6 +542,27 @@ impl KillOnDrop {
fn take_inner(&mut self) -> Child {
self.0.take().unwrap()
}

fn wait_with_timeout(&mut self) -> Output {
let mut child = self.take_inner();

// close stdin in case child is waiting for us
child.stdin.take();

let timeout_secs = 30;
let deadline = time::SystemTime::now().add(time::Duration::from_secs(timeout_secs));

loop {
if time::SystemTime::now() > deadline {
panic!("subprocess did not end within {timeout_secs} seconds");
}
match child.try_wait().expect("subprocess broken") {
Some(result) => return child.wait_with_output().unwrap(),
None => {}
};
thread::sleep(time::Duration::from_millis(500));
}
}
}

impl Drop for KillOnDrop {
Expand Down

0 comments on commit 23d7a9c

Please sign in to comment.