Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Generators/include/Generators/GeneratorFileOrCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ struct GeneratorFileOrCmd {
* Terminates the background command using PID of the child
* process generated by fork.
*
* @param graceMillis How long to wait for the command to exit on its
* own before killing its process group. A command started
* through an intermediate shell (a wrapper script, say) only
* contributes the CPU time of the actual generator to this
* process' RUSAGE_CHILDREN once that shell has reaped it, which
* it can no longer do once we have killed it. Zero preserves
* the previous kill-immediately behaviour.
* @param graceMillis How long to wait for the command's process group
* to exit on its own before killing it. On Linux the CPU time
* of the whole group reaches this process' RUSAGE_CHILDREN
* either way. On other systems, killing loses the CPU time of
* a generator started through a wrapper script. Zero kills
* immediately.
*
* @return true if the process was terminated successfully
*/
Expand Down
66 changes: 47 additions & 19 deletions Generators/src/GeneratorFileOrCmd.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <sys/stat.h> // POSIX only
#include <csignal>
#include <sys/wait.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <cstdio>
// For filesystem operations
#include <filesystem>
Expand Down Expand Up @@ -120,6 +123,13 @@ std::string GeneratorFileOrCmd::makeCmdLine() const
bool GeneratorFileOrCmd::executeCmdLine(const std::string& cmd)
{
LOG(info) << "Command line to execute: \"" << cmd << "\"";
#ifdef __linux__
// Adopt what the command orphans, so that terminateCmd can reap it and its
// CPU time reaches RUSAGE_CHILDREN
if (prctl(PR_SET_CHILD_SUBREAPER, 1) == -1) {
LOG(warn) << "Failed to become the reaper of the command's processes: " << std::strerror(errno);
}
#endif
// Fork a new process
pid_t pid = fork();
if (pid == -1) {
Expand Down Expand Up @@ -149,35 +159,53 @@ bool GeneratorFileOrCmd::terminateCmd(unsigned int graceMillis)
return false;
}

// Let the command finish by itself if it is about to: killing the process
// group first would deprive an intermediate shell of the chance to reap the
// actual generator, and with it this process of the generator's CPU time,
// which only reaches RUSAGE_CHILDREN through that reap.
constexpr unsigned int pollMillis = 10;
for (unsigned int waited = 0; waited < graceMillis; waited += pollMillis) {
// Reaps the group's exited processes, returns whether any remain
bool cmdReaped = false;
auto reapExited = [this, &cmdReaped]() {
int status;
pid_t reaped = waitpid(mCmdPid, &status, WNOHANG);
if (reaped == mCmdPid) {
LOG(info) << "Command with process ID " << mCmdPid << " exited by itself";
mCmdPid = -1;
return true;
pid_t reaped;
while ((reaped = waitpid(-mCmdPid, &status, WNOHANG)) > 0) {
cmdReaped |= (reaped == mCmdPid);
}
return reaped == 0;
};
// Returns whether the whole group was reaped in time
auto reapFor = [&reapExited](unsigned int millis) {
constexpr unsigned int pollMillis = 10;
for (unsigned int waited = 0; reapExited(); waited += pollMillis) {
if (waited >= millis) {
return false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(pollMillis));
}
if (reaped == -1) {
break; // not our child (any more): let the kill path report it
return true;
};

// Let the command finish by itself if it is about to: where the generator
// cannot be adopted, its CPU time is lost once an intermediate shell is killed
if (reapFor(graceMillis)) {
if (not cmdReaped) {
LOG(fatal) << "Failed to wait for process termination: " << std::strerror(ECHILD);
return false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(pollMillis));
LOG(info) << "Command with process ID " << mCmdPid << " exited by itself";
mCmdPid = -1;
return true;
}

LOG(info) << "Terminating process ID group " << mCmdPid;
if (kill(-mCmdPid, SIGKILL) == -1) {
if (kill(-mCmdPid, SIGKILL) == -1 and errno != ESRCH) {
LOG(fatal) << "Failed to kill process: " << std::strerror(errno);
return false;
}

// Wait for the process to terminate
int status;
if (waitpid(mCmdPid, &status, 0) == -1) {
LOG(fatal) << "Failed to wait for process termination: " << std::strerror(errno);
// A process stuck in the kernel outlasts SIGKILL: do not wait forever
constexpr unsigned int killWaitMillis = 30000;
if (not reapFor(killWaitMillis)) {
LOG(warn) << "Process group " << mCmdPid << " still has processes " << killWaitMillis
<< " ms after being killed, not waiting for them any longer";
} else if (not cmdReaped) {
LOG(fatal) << "Failed to wait for process termination: " << std::strerror(ECHILD);
return false;
}

Expand Down