#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using Void::Runtime::Host; using Void::Runtime::HostState; using Void::Runtime::Options; using Void::Runtime::SignalGate; using Void::Runtime::StartOptions; using Void::Runtime::StartStep; using Void::Testing::expect; using Void::Testing::expect_eq; using Void::Testing::require; [[nodiscard]] std::size_t count_open_fds() { std::size_t count = 0; std::error_code ec; for (const auto& _ : std::filesystem::directory_iterator("/proc/self/fd", ec)) { (void)_; ++count; } return count; } [[nodiscard]] bool is_sig_blocked(int sig) noexcept { sigset_t cur{}; if (::pthread_sigmask(SIG_SETMASK, nullptr, &cur) != 0) { return false; } return ::sigismember(&cur, sig) == 1; } void drain_and_unblock_sigusr2() noexcept { sigset_t set{}; ::sigemptyset(&set); ::sigaddset(&set, SIGUSR2); // Drain any queued SIGUSR2 safely while blocked before unblocking. timespec zero{0, 0}; siginfo_t info{}; while (::sigtimedwait(&set, &info, &zero) == SIGUSR2) { } ::pthread_sigmask(SIG_UNBLOCK, &set, nullptr); } struct Sigusr2CleanupGuard { ~Sigusr2CleanupGuard() { drain_and_unblock_sigusr2(); } }; class ProcessSignalGate final : public SignalGate { public: std::atomic install_calls{0}; std::atomic uninstall_calls{0}; std::vector drained_signals{}; bool drain_before_uninstall{true}; Void::Result::Result install() override { install_calls.fetch_add(1, std::memory_order_relaxed); auto w = Void::Process::SignalWatcher::create({SIGUSR2}); if (!w.has_value()) { return std::unexpected{std::move(w.error())}; } watcher_ = std::move(*w); return {}; } [[nodiscard]] int drain_pending() { int count = 0; if (!watcher_.has_value()) { return count; } for (;;) { auto r = watcher_->poll(); if (!r.has_value() || !r->has_value()) { break; } drained_signals.push_back(**r); ++count; } return count; } void uninstall(bool on_owner_thread) noexcept override { (void)on_owner_thread; if (drain_before_uninstall && watcher_.has_value()) { (void)drain_pending(); } watcher_.reset(); uninstall_calls.fetch_add(1, std::memory_order_relaxed); } private: std::optional watcher_{}; }; } // namespace Void::Testing::Test runtime_system_gate_mask_inherited_by_workers() { Sigusr2CleanupGuard sig_guard; drain_and_unblock_sigusr2(); expect(!is_sig_blocked(SIGUSR2)); ProcessSignalGate gate; Options opts; opts.cpu_workers = 2; opts.file_workers = 2; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); std::atomic blocked_hooks{0}; std::atomic total_hooks{0}; StartOptions start_opts; start_opts.signal_gate = &gate; start_opts.before_thread_start = [&](StartStep, std::size_t) -> Void::Result::Result { total_hooks.fetch_add(1, std::memory_order_relaxed); if (is_sig_blocked(SIGUSR2)) { blocked_hooks.fetch_add(1, std::memory_order_relaxed); } return {}; }; require(host.start(start_opts).has_value()); expect(is_sig_blocked(SIGUSR2)); // Owner mask installed before any thread started, so every worker hook saw it. expect_eq(total_hooks.load(), 5); expect_eq(blocked_hooks.load(), 5); std::atomic worker_saw_blocked{false}; auto check_task = [](std::atomic* out) -> Void::Coro::Task { out->store(is_sig_blocked(SIGUSR2), std::memory_order_release); co_return; }; auto jh = host.spawn(check_task(&worker_saw_blocked)); require(jh.has_value()); require(jh->join_blocking().has_value()); expect(worker_saw_blocked.load(std::memory_order_acquire)); require(host.shutdown_blocking().has_value()); expect(!is_sig_blocked(SIGUSR2)); } Void::Testing::Test runtime_system_gate_mask_restored_on_owner_after_failed_start_and_teardown() { Sigusr2CleanupGuard sig_guard; drain_and_unblock_sigusr2(); expect(!is_sig_blocked(SIGUSR2)); // 1. Failed start restores SIGUSR2 mask on owner thread during reverse cleanup. { ProcessSignalGate gate; Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); StartOptions so; so.signal_gate = &gate; so.before_step = [](StartStep step) -> Void::Result::Result { if (step == StartStep::components) { return std::unexpected{Void::Runtime::make_runtime_error( Void::Runtime::RuntimeErrorCode::internal, "inject_comp_fail")}; } return {}; }; auto sr = host.start(so); require(!sr.has_value()); expect(host.state() == HostState::failed_start); expect(!is_sig_blocked(SIGUSR2)); expect_eq(gate.uninstall_calls.load(), 1); } // 2. Normal start -> teardown restores SIGUSR2 mask on owner thread. { ProcessSignalGate gate; Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start(StartOptions{.signal_gate = &gate}).has_value()); expect(is_sig_blocked(SIGUSR2)); require(host.shutdown_blocking().has_value()); expect(!is_sig_blocked(SIGUSR2)); expect_eq(gate.uninstall_calls.load(), 1); } } Void::Testing::Test runtime_signal_during_start_stays_pending_and_is_drained() { Sigusr2CleanupGuard sig_guard; drain_and_unblock_sigusr2(); ProcessSignalGate gate; Options opts; opts.cpu_workers = 2; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); StartOptions so; so.signal_gate = &gate; so.before_step = [](StartStep step) -> Void::Result::Result { if (step == StartStep::readiness) { // Send SIGUSR2 mid-start while workers exist; all block SIGUSR2. (void)::kill(::getpid(), SIGUSR2); } return {}; }; require(host.start(so).has_value()); expect(is_sig_blocked(SIGUSR2)); // Signal remained pending in signalfd without killing the process and is drained once. const int n = gate.drain_pending(); expect_eq(n, 1); require(gate.drained_signals.size() == 1); expect_eq(gate.drained_signals[0], SIGUSR2); expect_eq(gate.drain_pending(), 0); require(host.shutdown_blocking().has_value()); expect(!is_sig_blocked(SIGUSR2)); } Void::Testing::Test runtime_offowner_destroy_closes_gate_fd_mask_not_restored() { Sigusr2CleanupGuard sig_guard; drain_and_unblock_sigusr2(); expect(!is_sig_blocked(SIGUSR2)); const std::size_t fds_before = count_open_fds(); ProcessSignalGate gate; Options opts; opts.cpu_workers = 1; opts.file_workers = 1; opts.io.preference = Void::IO::BackendPreference::mock; auto host_res = Host::create(opts); require(host_res.has_value()); Host host = std::move(*host_res); require(host.start(StartOptions{.signal_gate = &gate}).has_value()); expect(is_sig_blocked(SIGUSR2)); expect(count_open_fds() > fds_before); // Move host to a non-owner thread and destroy it there. std::thread off_owner([moved = std::move(host)]() mutable {}); off_owner.join(); // Gate signalfd fd is closed, while owner thread mask remains blocked (not restored off-owner). expect_eq(gate.uninstall_calls.load(), 1); expect_eq(count_open_fds(), fds_before); expect(is_sig_blocked(SIGUSR2)); // P3-3: explicitly restore owner mask cleanly before leaving test. drain_and_unblock_sigusr2(); expect(!is_sig_blocked(SIGUSR2)); }