From 2aebc131207abdccb34c75fa96b83090c3974d6f Mon Sep 17 00:00:00 2001 From: Joseph Birr-Pixton Date: Sun, 5 May 2024 10:35:20 +0100 Subject: [PATCH] Don't wait forever for subprocess --- rustls-libssl/tests/runner.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/rustls-libssl/tests/runner.rs b/rustls-libssl/tests/runner.rs index 9203795..f0da201 100644 --- a/rustls-libssl/tests/runner.rs +++ b/rustls-libssl/tests/runner.rs @@ -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}; @@ -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") @@ -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); } @@ -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") @@ -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); } @@ -541,6 +542,26 @@ 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"); + } + if let Some(_) = child.try_wait().expect("subprocess broken") { + return child.wait_with_output().unwrap(); + }; + thread::sleep(time::Duration::from_millis(500)); + } + } } impl Drop for KillOnDrop {