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:
1 parent
128829bc42
commit
c085822686
2 files changed
+87
No files matched your search
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cctype>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
@@ -11,8 +12,10 @@
|
|||||||
#include <expected>
|
#include <expected>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <fstream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -347,6 +350,49 @@ void write_child_failure(int err_fd, int phase, int err) noexcept
|
|||||||
return st;
|
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
|
[[nodiscard]] bool is_process_group_alive(pid_t pgid) noexcept
|
||||||
{
|
{
|
||||||
if (pgid <= 0) {
|
if (pgid <= 0) {
|
||||||
@@ -354,7 +400,11 @@ void write_child_failure(int err_fd, int phase, int err) noexcept
|
|||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (::kill(-pgid, 0) == 0) {
|
if (::kill(-pgid, 0) == 0) {
|
||||||
|
#if defined(__linux__)
|
||||||
|
return process_group_has_live_member(pgid);
|
||||||
|
#else
|
||||||
return true;
|
return true;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -296,6 +296,43 @@ Void::Testing::Test system_replace_file_temp_ownership_and_metrics()
|
|||||||
expect(sample.ratio >= 0.0);
|
expect(sample.ratio >= 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test system_process_group_alive_ignores_unreaped_zombies()
|
||||||
|
{
|
||||||
|
// A group whose only members are zombies is dead, even though
|
||||||
|
// kill(-pgid, 0) still succeeds. This happens whenever a supervising
|
||||||
|
// ancestor is a child subreaper (e.g. a build system running the tests).
|
||||||
|
Void::Process::SpawnOptions opts;
|
||||||
|
opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||||
|
opts.arguments = {"exit", "0"};
|
||||||
|
opts.new_process_group = true;
|
||||||
|
opts.stdin_pipe = false;
|
||||||
|
opts.stdout_pipe = false;
|
||||||
|
opts.stderr_pipe = false;
|
||||||
|
auto spawned = Void::Process::spawn(opts);
|
||||||
|
require(spawned.has_value());
|
||||||
|
|
||||||
|
const auto pid = static_cast<pid_t>(spawned->pid());
|
||||||
|
bool zombie = false;
|
||||||
|
for (int attempt = 0; attempt < 400; ++attempt) {
|
||||||
|
std::ifstream stat{"/proc/" + std::to_string(pid) + "/stat"};
|
||||||
|
std::string line;
|
||||||
|
if (std::getline(stat, line)) {
|
||||||
|
const std::size_t close = line.rfind(')');
|
||||||
|
if (close != std::string::npos && close + 2 < line.size() &&
|
||||||
|
line[close + 2] == 'Z') {
|
||||||
|
zombie = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||||
|
}
|
||||||
|
require(zombie);
|
||||||
|
expect(!spawned->process_group_alive());
|
||||||
|
|
||||||
|
// terminate() reaps the zombie leader and cleans up.
|
||||||
|
expect(spawned->terminate(std::chrono::milliseconds{50}).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
Void::Testing::Test system_terminate_reaps_entire_process_group_after_leader_exits()
|
Void::Testing::Test system_terminate_reaps_entire_process_group_after_leader_exits()
|
||||||
{
|
{
|
||||||
Void::FileSystem::TempDir tmp{"void_sys_pgid_"};
|
Void::FileSystem::TempDir tmp{"void_sys_pgid_"};
|
||||||
|
|||||||
Reference in new issue
Block a user