Files
FairMQ/fairmq/tools/Process.cxx
Claude f50b1f0e39 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>
2026-11-03 17:00:01 +01:00

204 lines
6.3 KiB
C++

/********************************************************************************
* Copyright (C) 2017-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/tools/Process.h>
#include <fairmq/tools/Strings.h>
#include <boost/asio.hpp>
#ifdef FAIRMQ_BOOST_PROCESS_V1_HEADER
#include <boost/process/v1.hpp>
#else
#include <boost/process.hpp>
#endif
#include <chrono>
#include <csignal> // kill, signals
#include <iostream>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <thread>
#include <utility>
using namespace std;
#ifdef FAIRMQ_BOOST_PROCESS_V1_HEADER
namespace bp = boost::process::v1;
#else
namespace bp = boost::process;
#endif
namespace ba = boost::asio;
namespace bs = boost::system;
class LinePrinter
{
public:
LinePrinter(stringstream& out, string prefix)
: fOut(out)
, fPrefix(std::move(prefix))
{}
// prints line with prefix on both cout and output stream
void Print(const string& line)
{
// Serialize: the standard allows concurrent insertion on std::cout,
// but libstdc++ maintains the formatted-output state (e.g.
// ios_base::width) with plain reads and writes that constitute data
// races; the lock also keeps whole lines from interleaving.
static mutex sCoutMutex;
{
lock_guard<mutex> lock(sCoutMutex);
cout << fair::mq::tools::ToString(fPrefix, line, "\n") << flush;
}
fOut << fPrefix << line << endl;
}
private:
stringstream& fOut;
const string fPrefix;
};
namespace fair::mq::tools
{
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;
LinePrinter p(out, prefix);
p.Print(cmd);
ba::io_context ioc;
// containers for std_in
ba::const_buffer inputBuffer(ba::buffer(input));
bp::async_pipe inputPipe(ioc);
// containers for std_out
ba::streambuf outputBuffer;
bp::async_pipe outputPipe(ioc);
// containers for std_err
ba::streambuf errorBuffer;
bp::async_pipe errorPipe(ioc);
const string delimiter = "\n";
ba::steady_timer inputTimer(ioc);
inputTimer.expires_after(std::chrono::milliseconds(1000)); // NOLINT
ba::steady_timer signalTimer(ioc);
signalTimer.expires_after(std::chrono::milliseconds(2000)); // NOLINT
// child process
bp::child c(cmd, bp::std_out > outputPipe, bp::std_err > errorPipe, bp::std_in < inputPipe);
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) {
if (!ec1) {
ba::async_write(inputPipe, inputBuffer, [&](const bs::error_code& ec2, size_t /* n */) {
if (!ec2) {
// inputPipe.async_close();
} else {
p.Print(ToString("error in boost::asio::async_write: ", ec2.message()));
}
});
} else {
p.Print(ToString("error in boost::asio::deadline_timer.async_wait: ", ec1.message()));
}
});
}
if (sig != -1) {
signalTimer.async_wait([&](const bs::error_code& ec1) {
if (!ec1) {
kill(pid, sig);
} else {
p.Print(ToString("error in boost::asio::deadline_timer.async_wait: ", ec1.message()));
}
});
}
// handle std_out line by line
function<void(const bs::error_code&, size_t)> onStdOut = [&](const bs::error_code& ec, size_t /* n */) {
if (!ec) {
istream is(&outputBuffer);
string line;
getline(is, line);
p.Print(line);
ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut);
} else {
if (ec != ba::error::eof) {
p.Print(ec.message());
}
onPipeClosed();
}
};
ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut);
// handle std_err line by line
function<void(const bs::error_code&, size_t)> onStdErr = [&](const bs::error_code& ec, size_t /* n */) {
if (!ec) {
istream is(&errorBuffer);
string line;
getline(is, line);
p.Print(ToString("error: ", line));
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);
} else {
if (ec != ba::error::eof) {
p.Print(ec.message());
}
onPipeClosed();
}
};
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);
ioc.run();
c.wait();
result.exit_code = c.exit_code();
p.Print(ToString("fair::mq::tools::execute: exit code: ", result.exit_code));
result.console_out = out.str();
return result;
}
} // namespace fair::mq::tools