feat(supervisor): process workers via make_process_work with group kill and parent-death cleanup
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
// O7-2: OS-process workers: lifecycle, exit codes, restart, no orphans.
|
||||
#include <signal.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "supervisor/common/clock.hpp"
|
||||
#include "supervisor/common/observer.hpp"
|
||||
#include "supervisor/supervisor/process_work.hpp"
|
||||
#include "supervisor/supervisor/supervisor.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
int failures = 0;
|
||||
|
||||
#define CHECK(cond, msg) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cout << "FAIL " << __LINE__ << ": " << (msg) << std::endl; \
|
||||
++failures; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
using molo::supervisor::ProcessSpec;
|
||||
using molo::supervisor::make_process_work;
|
||||
|
||||
bool wait_for(const std::function<bool()>& pred, long timeout_ms) {
|
||||
const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
while (std::chrono::steady_clock::now() < end) {
|
||||
if (pred()) return true;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
return pred();
|
||||
}
|
||||
|
||||
std::string read_file(const std::string& path) {
|
||||
std::ifstream in(path);
|
||||
std::string s((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
return s;
|
||||
}
|
||||
|
||||
bool pid_alive(long pid) { return pid > 0 && kill(static_cast<pid_t>(pid), 0) == 0; }
|
||||
|
||||
void test_exit_codes() {
|
||||
std::atomic<bool> stop{false};
|
||||
CHECK(make_process_work({{"sh", "-c", "exit 0"}})(stop) == 0, "clean exit");
|
||||
CHECK(make_process_work({{"sh", "-c", "exit 3"}})(stop) == 3, "exit code passthrough");
|
||||
CHECK(make_process_work({{"sh", "-c", "kill -9 $$"}})(stop) == 128 + 9, "signal exit");
|
||||
CHECK(make_process_work({{"/nonexistent/prog"}})(stop) == 127, "exec failure");
|
||||
CHECK(make_process_work({{}})(stop) == 1, "empty argv rejected");
|
||||
}
|
||||
|
||||
void test_stop_kills_process_group() {
|
||||
const std::string pidfile = "/tmp/molo_pw_group.pid";
|
||||
std::remove(pidfile.c_str());
|
||||
std::atomic<bool> stop{false};
|
||||
ProcessSpec spec{{"sh", "-c", "sleep 300 & echo $! > " + pidfile + "; wait"}, 500};
|
||||
int code = -1;
|
||||
std::thread t([&] { code = make_process_work(spec)(stop); });
|
||||
CHECK(wait_for([&] { return !read_file(pidfile).empty(); }, 3000), "grandchild started");
|
||||
const long gpid = std::atol(read_file(pidfile).c_str());
|
||||
CHECK(pid_alive(gpid), "grandchild alive");
|
||||
stop = true;
|
||||
t.join();
|
||||
CHECK(code == 0, "requested stop returns 0");
|
||||
CHECK(wait_for([&] { return !pid_alive(gpid); }, 2000), "grandchild killed with group");
|
||||
}
|
||||
|
||||
void test_sigterm_ignored_gets_sigkill() {
|
||||
std::atomic<bool> stop{false};
|
||||
ProcessSpec spec{{"sh", "-c", "trap '' TERM; while true; do sleep 1; done"}, 300};
|
||||
std::thread t([&] { make_process_work(spec)(stop); });
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
stop = true;
|
||||
t.join();
|
||||
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - t0).count();
|
||||
CHECK(ms < 3000, "stubborn child force-killed after grace");
|
||||
}
|
||||
|
||||
void test_supervisor_restarts_and_shutdown_reaps() {
|
||||
const std::string pidfile = "/tmp/molo_pw_sup.pid";
|
||||
std::remove(pidfile.c_str());
|
||||
molo::common::SystemClock clock;
|
||||
auto obs = std::make_shared<molo::common::NullObserver>();
|
||||
long pid1 = 0;
|
||||
{
|
||||
molo::supervisor::Supervisor sup(clock, obs);
|
||||
molo::supervisor::GroupSpec spec;
|
||||
spec.name = "proc";
|
||||
spec.backoff_min_ms = 50;
|
||||
spec.backoff_max_ms = 100;
|
||||
sup.add_group(spec, make_process_work({{"sh", "-c", "echo $$ > " + pidfile + "; exec sleep 300"}}));
|
||||
sup.start_all();
|
||||
CHECK(wait_for([&] { return !read_file(pidfile).empty(); }, 3000), "child started");
|
||||
pid1 = std::atol(read_file(pidfile).c_str());
|
||||
std::remove(pidfile.c_str());
|
||||
kill(static_cast<pid_t>(pid1), SIGKILL); // crash it
|
||||
CHECK(wait_for([&] { return !read_file(pidfile).empty(); }, 5000), "restarted after crash");
|
||||
const long pid2 = std::atol(read_file(pidfile).c_str());
|
||||
CHECK(pid2 != pid1 && pid_alive(pid2), "new child alive");
|
||||
sup.shutdown(0);
|
||||
CHECK(wait_for([&] { return !pid_alive(pid2); }, 3000), "shutdown reaps child");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
test_exit_codes();
|
||||
test_stop_kills_process_group();
|
||||
test_sigterm_ignored_gets_sigkill();
|
||||
test_supervisor_restarts_and_shutdown_reaps();
|
||||
if (failures == 0) std::cout << "process_work: all passed" << std::endl;
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user