feat(tools): add an execute() overload with a deadline

- a child process that hangs blocked execute() until the caller was
  killed (e.g. by the ctest timeout); the child then ran on as an
  orphan, with its shm segments and ipc files
- the new overload kills the child with SIGKILL after the timeout and
  logs the timeout in the captured output
- keep the old overload (no limit) for compatibility

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-03 14:17:36 +00:00
committed by Dennis Klein
parent 3c752937a4
commit f50b1f0e39
2 changed files with 43 additions and 14 deletions

View File

@@ -64,15 +64,16 @@ class LinePrinter
namespace fair::mq::tools
{
/**
* Execute given command in forked process and capture stdout output
* and exit code.
*
* @param[in] cmd Command to execute
* @param[in] log_prefix How to prefix each captured output line with
* @return Captured stdout output and exit code
*/
execute_result execute(const string& cmd, const string& prefix, const string& input, int sig)
{
return execute(cmd, prefix, input, sig, chrono::milliseconds::zero());
}
execute_result execute(const string& cmd,
const string& prefix,
const string& input,
int sig,
chrono::milliseconds timeout)
{
execute_result result;
stringstream out;
@@ -104,6 +105,25 @@ execute_result execute(const string& cmd, const string& prefix, const string& in
int pid = c.id();
p.Print(ToString("fair::mq::tools::execute: pid: ", pid));
// kill the child when the deadline expires; cancel the deadline when both
// output pipes are closed (the child has exited)
ba::steady_timer deadlineTimer(ioc);
int openPipes = 2;
auto onPipeClosed = [&]() {
if (--openPipes == 0) {
deadlineTimer.cancel();
}
};
if (timeout > chrono::milliseconds::zero()) {
deadlineTimer.expires_after(timeout);
deadlineTimer.async_wait([&](const bs::error_code& ec) {
if (!ec) {
p.Print(ToString("fair::mq::tools::execute: timeout after ", timeout.count(), " ms, killing pid ", pid));
kill(pid, SIGKILL);
}
});
}
// handle std_in with a delay
if (!input.empty()) {
inputTimer.async_wait([&](const bs::error_code& ec1) {
@@ -142,11 +162,10 @@ execute_result execute(const string& cmd, const string& prefix, const string& in
ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut);
} else {
if (ec == ba::error::eof) {
// outputPipe.async_close();
} else {
if (ec != ba::error::eof) {
p.Print(ec.message());
}
onPipeClosed();
}
};
ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut);
@@ -162,11 +181,10 @@ execute_result execute(const string& cmd, const string& prefix, const string& in
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);
} else {
if (ec == ba::error::eof) {
// errorPipe.async_close();
} else {
if (ec != ba::error::eof) {
p.Print(ec.message());
}
onPipeClosed();
}
};
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);