mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 21:21:37 +00:00
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:
@@ -64,15 +64,16 @@ class LinePrinter
|
|||||||
namespace fair::mq::tools
|
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)
|
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;
|
execute_result result;
|
||||||
stringstream out;
|
stringstream out;
|
||||||
@@ -104,6 +105,25 @@ execute_result execute(const string& cmd, const string& prefix, const string& in
|
|||||||
int pid = c.id();
|
int pid = c.id();
|
||||||
p.Print(ToString("fair::mq::tools::execute: pid: ", pid));
|
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
|
// handle std_in with a delay
|
||||||
if (!input.empty()) {
|
if (!input.empty()) {
|
||||||
inputTimer.async_wait([&](const bs::error_code& ec1) {
|
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);
|
ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut);
|
||||||
} else {
|
} else {
|
||||||
if (ec == ba::error::eof) {
|
if (ec != ba::error::eof) {
|
||||||
// outputPipe.async_close();
|
|
||||||
} else {
|
|
||||||
p.Print(ec.message());
|
p.Print(ec.message());
|
||||||
}
|
}
|
||||||
|
onPipeClosed();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut);
|
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);
|
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);
|
||||||
} else {
|
} else {
|
||||||
if (ec == ba::error::eof) {
|
if (ec != ba::error::eof) {
|
||||||
// errorPipe.async_close();
|
|
||||||
} else {
|
|
||||||
p.Print(ec.message());
|
p.Print(ec.message());
|
||||||
}
|
}
|
||||||
|
onPipeClosed();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);
|
ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#ifndef FAIR_MQ_TOOLS_PROCESS_H
|
#ifndef FAIR_MQ_TOOLS_PROCESS_H
|
||||||
#define FAIR_MQ_TOOLS_PROCESS_H
|
#define FAIR_MQ_TOOLS_PROCESS_H
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace fair::mq::tools
|
namespace fair::mq::tools
|
||||||
@@ -37,6 +38,16 @@ execute_result execute(const std::string& cmd,
|
|||||||
const std::string& input = "",
|
const std::string& input = "",
|
||||||
int sig = -1);
|
int sig = -1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as above, but kill the child process (SIGKILL) if it runs longer than
|
||||||
|
* timeout. A timeout of zero means no limit.
|
||||||
|
*/
|
||||||
|
execute_result execute(const std::string& cmd,
|
||||||
|
const std::string& prefix,
|
||||||
|
const std::string& input,
|
||||||
|
int sig,
|
||||||
|
std::chrono::milliseconds timeout);
|
||||||
|
|
||||||
} // namespace fair::mq::tools
|
} // namespace fair::mq::tools
|
||||||
|
|
||||||
#endif /* FAIR_MQ_TOOLS_PROCESS_H */
|
#endif /* FAIR_MQ_TOOLS_PROCESS_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user