System: treat zombie-only process groups as dead

is_process_group_alive() relied on kill(-pgid, 0), which also succeeds
when every remaining member is a zombie. A supervising ancestor that is
a child subreaper (a build system running tests) can hold those zombies
until it reaps them, so Child::terminate() timed out after SIGKILL even
though the group was dead. On Linux, scan /proc and require a non-zombie
member.

Regression test system_process_group_alive_ignores_unreaped_zombies.
Full vbs test suite passes (VoidTests).
This commit is contained in:
faumaray committed 2026-12-05 12:05:10 +03:00
1 parent 128829bc42
commit c085822686
2 files changed
+87

No files matched your search

+50
View File
@@ -3,6 +3,7 @@
#include <algorithm>
#include <array>
#include <cctype>
#include <cerrno>
#include <chrono>
#include <csignal>
@@ -11,8 +12,10 @@
#include <expected>
#include <filesystem>
#include <format>
#include <fstream>
#include <limits>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <thread>
@@ -347,6 +350,49 @@ void write_child_failure(int err_fd, int phase, int err) noexcept
return st;
}
#if defined(__linux__)
// kill(-pgid, 0) succeeds while any member exists, including zombies. A
// supervising ancestor that is a child subreaper can hold zombie descendants
// until it reaps them, which would otherwise keep a dead group "alive"
// forever. Scan /proc for a member that is not a zombie.
[[nodiscard]] bool process_group_has_live_member(pid_t pgid) noexcept
{
std::error_code ec;
for (std::filesystem::directory_iterator entry{"/proc", ec};
!ec && entry != std::filesystem::directory_iterator{};
entry.increment(ec)) {
const std::string name = entry->path().filename().string();
if (name.empty() ||
!std::all_of(name.begin(), name.end(), [](unsigned char c) {
return std::isdigit(c) != 0;
})) {
continue;
}
std::ifstream stat{entry->path() / "stat"};
std::string line;
if (!std::getline(stat, line)) {
continue;
}
// "pid (comm) state ppid pgrp ..." - comm may contain ')' or spaces.
const std::size_t close = line.rfind(')');
if (close == std::string::npos) {
continue;
}
std::istringstream fields{line.substr(close + 1)};
char state = 0;
long parent = 0;
long group = 0;
if (!(fields >> state >> parent >> group)) {
continue;
}
if (group == static_cast<long>(pgid) && state != 'Z' && state != 'X') {
return true;
}
}
return false;
}
#endif
[[nodiscard]] bool is_process_group_alive(pid_t pgid) noexcept
{
if (pgid <= 0) {
@@ -354,7 +400,11 @@ void write_child_failure(int err_fd, int phase, int err) noexcept
}
for (;;) {
if (::kill(-pgid, 0) == 0) {
#if defined(__linux__)
return process_group_has_live_member(pgid);
#else
return true;
#endif
}
if (errno == EINTR) {
continue;