feat(SystemAsync): add async IO ProcessManager/communicate and shared WatcherService with WatchSubscription (M-SYSTEM-03, M-SYSTEM-04)
This commit is contained in:
1 parent
fcdbc063b2
commit
9a3c5d86ef
9 files changed
+2062
No files matched your search
@@ -0,0 +1,357 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/System/AsyncProcess.hpp>
|
||||
#include <Void/System/AsyncWatcher.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
void pump_loop(Void::IO::Driver& driver,
|
||||
Void::Async::ManualExecutor& exec,
|
||||
Void::Async::TaskScope& scope)
|
||||
{
|
||||
for (int iter = 0; iter < 5000 && scope.active_count() > 0; ++iter) {
|
||||
(void)exec.drain();
|
||||
(void)driver.poll_once(std::chrono::milliseconds{1});
|
||||
(void)exec.drain();
|
||||
}
|
||||
if (scope.active_count() > 0) {
|
||||
(void)scope.join_pumping(exec);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<void> run_communicate_task(
|
||||
Void::SystemAsync::ProcessManager* mgr,
|
||||
Void::SystemAsync::ManagedChild* child,
|
||||
std::string stdin_data,
|
||||
Void::Process::CaptureLimits limits,
|
||||
Void::Async::OperationOptions opts,
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>*
|
||||
out)
|
||||
{
|
||||
*out = co_await mgr->communicate(
|
||||
*child, std::move(stdin_data), limits, std::move(opts));
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<void> run_close_join_task(
|
||||
Void::SystemAsync::ProcessManager* mgr,
|
||||
Void::Result::Result<void, Void::Result::Error>* out)
|
||||
{
|
||||
*out = co_await mgr->close_and_join();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test system_async_process_manager_duplex_overflow_escalation_and_close()
|
||||
{
|
||||
Void::Async::ManualExecutor exec;
|
||||
auto drv_res = Void::IO::Driver::create(exec);
|
||||
require(drv_res.has_value());
|
||||
auto& driver = **drv_res;
|
||||
|
||||
Void::SystemAsync::ProcessManager mgr{
|
||||
driver,
|
||||
exec,
|
||||
Void::SystemAsync::ProcessManagerOptions{
|
||||
.max_active_children = 2,
|
||||
.default_kill_grace = std::chrono::milliseconds{40},
|
||||
}};
|
||||
|
||||
// 1. max_active_children slot cap enforced cleanly.
|
||||
{
|
||||
Void::Process::SpawnOptions sleep_opts;
|
||||
sleep_opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||
sleep_opts.arguments = {"ignore-sigterm", "2000"};
|
||||
sleep_opts.kill_grace = std::chrono::milliseconds{20};
|
||||
auto c1 = mgr.spawn(sleep_opts);
|
||||
auto c2 = mgr.spawn(sleep_opts);
|
||||
require(c1.has_value() && c2.has_value());
|
||||
auto c3 = mgr.spawn(sleep_opts);
|
||||
require(!c3.has_value());
|
||||
expect_eq(c3.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
Void::System::SystemErrorCode::limit_exceeded));
|
||||
expect_eq(mgr.active_children(), std::size_t{2});
|
||||
}
|
||||
expect_eq(mgr.active_children(), std::size_t{0});
|
||||
|
||||
// 2. Full-duplex 256 KiB async communicate via Void::IO without deadlock.
|
||||
{
|
||||
const std::size_t payload_bytes = 256ULL * 1024ULL;
|
||||
Void::Process::SpawnOptions sim_opts;
|
||||
sim_opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||
sim_opts.arguments = {"simultaneous-io", std::to_string(payload_bytes)};
|
||||
auto ch = mgr.spawn(sim_opts);
|
||||
require(ch.has_value());
|
||||
|
||||
Void::Process::CaptureLimits lim;
|
||||
lim.max_line_bytes = 512ULL * 1024ULL;
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>
|
||||
out{std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::internal, "unset")}};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
require(scope
|
||||
.spawn(run_communicate_task(
|
||||
&mgr,
|
||||
&*ch,
|
||||
std::string(payload_bytes, 'i'),
|
||||
lim,
|
||||
{},
|
||||
&out))
|
||||
.has_value());
|
||||
pump_loop(driver, exec, scope);
|
||||
require(out.has_value());
|
||||
expect(!out->timed_out);
|
||||
expect_eq(out->status.code, 0);
|
||||
expect_eq(out->out.size(), payload_bytes);
|
||||
expect_eq(out->err.size(), payload_bytes);
|
||||
}
|
||||
|
||||
// 3. Overflow limit terminates child and returns output_limit.
|
||||
{
|
||||
Void::Process::SpawnOptions flood_opts;
|
||||
flood_opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||
flood_opts.arguments = {"flood", "4096", "0"};
|
||||
auto ch = mgr.spawn(flood_opts);
|
||||
require(ch.has_value());
|
||||
|
||||
Void::Process::CaptureLimits lim;
|
||||
lim.max_stdout_bytes = 256;
|
||||
lim.overflow_policy = Void::Process::OverflowPolicy::fail_and_terminate;
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>
|
||||
out{std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::internal, "unset")}};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
require(scope
|
||||
.spawn(run_communicate_task(&mgr, &*ch, {}, lim, {}, &out))
|
||||
.has_value());
|
||||
pump_loop(driver, exec, scope);
|
||||
require(!out.has_value());
|
||||
expect_eq(out.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
Void::System::SystemErrorCode::output_limit));
|
||||
}
|
||||
|
||||
// 4. SIGTERM-ignoring child escalates on OperationOptions deadline -> SIGKILL.
|
||||
{
|
||||
Void::Process::SpawnOptions sig_opts;
|
||||
sig_opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||
sig_opts.arguments = {"ignore-sigterm", "10000"};
|
||||
sig_opts.kill_grace = std::chrono::milliseconds{30};
|
||||
auto ch = mgr.spawn(sig_opts);
|
||||
require(ch.has_value());
|
||||
|
||||
Void::Async::OperationOptions opts{
|
||||
.deadline = Void::Time::SystemMonotonicClock::instance().now() +
|
||||
std::chrono::milliseconds{60}};
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>
|
||||
out{std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::internal, "unset")}};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
require(scope
|
||||
.spawn(run_communicate_task(
|
||||
&mgr, &*ch, {}, Void::Process::CaptureLimits{}, opts, &out))
|
||||
.has_value());
|
||||
pump_loop(driver, exec, scope);
|
||||
require(out.has_value());
|
||||
expect(out->timed_out);
|
||||
expect_eq(out->status.signal, SIGKILL);
|
||||
}
|
||||
|
||||
// 5. close_and_join closes admission and drains cleanly.
|
||||
{
|
||||
Void::Result::Result<void, Void::Result::Error> close_res;
|
||||
Void::Async::TaskScope scope{exec};
|
||||
require(scope.spawn(run_close_join_task(&mgr, &close_res)).has_value());
|
||||
pump_loop(driver, exec, scope);
|
||||
require(close_res.has_value());
|
||||
expect(mgr.closed());
|
||||
|
||||
Void::Process::SpawnOptions blocked_opts;
|
||||
blocked_opts.executable = VOID_PROCESS_HELPER_PATH;
|
||||
blocked_opts.arguments = {"exit", "0"};
|
||||
auto late = mgr.spawn(blocked_opts);
|
||||
require(!late.has_value());
|
||||
expect_eq(late.error().code().value,
|
||||
static_cast<std::int32_t>(Void::System::SystemErrorCode::busy));
|
||||
}
|
||||
|
||||
(void)driver.shutdown_and_join();
|
||||
}
|
||||
|
||||
Void::Testing::Test system_async_watcher_deterministic_events_overflow_and_join()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_w6a_watch_"};
|
||||
Void::FileSystem::TempDir outside{"void_w6a_out_"};
|
||||
Void::Time::ManualClock clock;
|
||||
Void::Async::ManualExecutor exec;
|
||||
|
||||
std::vector<std::string> errors;
|
||||
Void::SystemAsync::WatcherService svc{
|
||||
exec,
|
||||
Void::SystemAsync::WatcherLimits{
|
||||
.max_watched_paths = 32,
|
||||
.max_queued_events = 2,
|
||||
.debounce_polls = 1,
|
||||
},
|
||||
clock,
|
||||
[&](const Void::Result::Error& err) {
|
||||
errors.push_back(err.message());
|
||||
},
|
||||
/*start_background_thread=*/false};
|
||||
|
||||
// 1. Rejects path outside allowed_root and rejects forbidden symlinks.
|
||||
const auto outside_file = outside / "secret.txt";
|
||||
require(Void::FileSystem::write_file(outside_file, "x").has_value());
|
||||
{
|
||||
auto esc = svc.watch(
|
||||
tmp.path() / ".." / outside.path().filename() / "secret.txt",
|
||||
[](const Void::SystemAsync::WatchEvent&) {},
|
||||
Void::SystemAsync::WatchPathOptions{.allowed_root = tmp.path()});
|
||||
expect(!esc.has_value());
|
||||
expect_eq(esc.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
Void::System::SystemErrorCode::permission_denied));
|
||||
}
|
||||
|
||||
const auto sym_path = tmp / "forbidden_link";
|
||||
std::filesystem::create_symlink(outside_file, sym_path);
|
||||
{
|
||||
auto sym_rej = svc.watch(
|
||||
sym_path,
|
||||
[](const Void::SystemAsync::WatchEvent&) {},
|
||||
Void::SystemAsync::WatchPathOptions{.allow_symlink = false});
|
||||
expect(!sym_rej.has_value());
|
||||
expect_eq(sym_rej.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
Void::System::SystemErrorCode::permission_denied));
|
||||
}
|
||||
|
||||
// 2. Detects created -> replaced_inode -> deleted -> self_join -> close_and_join.
|
||||
const auto target = tmp / "watched.cfg";
|
||||
std::vector<Void::SystemAsync::WatchEventKind> kinds;
|
||||
Void::SystemAsync::WatchSubscription* sub_ptr = nullptr;
|
||||
std::optional<Void::Result::Error> self_join_err;
|
||||
bool throw_on_next = false;
|
||||
|
||||
auto sub_res = svc.watch(
|
||||
target,
|
||||
[&](const Void::SystemAsync::WatchEvent& ev) {
|
||||
kinds.push_back(ev.kind);
|
||||
if (throw_on_next) {
|
||||
throw std::runtime_error("intentional watcher callback throw");
|
||||
}
|
||||
if (sub_ptr != nullptr &&
|
||||
ev.kind == Void::SystemAsync::WatchEventKind::deleted) {
|
||||
auto sj = sub_ptr->close_and_join();
|
||||
if (!sj.has_value()) {
|
||||
self_join_err = sj.error();
|
||||
}
|
||||
}
|
||||
},
|
||||
Void::SystemAsync::WatchPathOptions{.allowed_root = tmp.path()});
|
||||
require(sub_res.has_value());
|
||||
sub_ptr = &*sub_res;
|
||||
|
||||
require(Void::FileSystem::replace_file(target, "v1").has_value());
|
||||
svc.poll_once();
|
||||
(void)exec.drain();
|
||||
require(kinds.size() == 1);
|
||||
expect(kinds[0] == Void::SystemAsync::WatchEventKind::created);
|
||||
|
||||
require(Void::FileSystem::replace_file(target, "v2_longer").has_value());
|
||||
svc.poll_once();
|
||||
(void)exec.drain();
|
||||
require(kinds.size() == 2);
|
||||
expect(kinds[1] == Void::SystemAsync::WatchEventKind::replaced_inode);
|
||||
|
||||
std::filesystem::remove(target);
|
||||
svc.poll_once();
|
||||
(void)exec.drain();
|
||||
require(kinds.size() == 3);
|
||||
expect(kinds[2] == Void::SystemAsync::WatchEventKind::deleted);
|
||||
require(self_join_err.has_value());
|
||||
expect_eq(self_join_err->code().value,
|
||||
static_cast<std::int32_t>(
|
||||
Void::System::SystemErrorCode::self_join));
|
||||
|
||||
// Callback exceptions are caught and routed to ErrorSink without crashing.
|
||||
throw_on_next = true;
|
||||
require(Void::FileSystem::replace_file(target, "v3").has_value());
|
||||
svc.poll_once();
|
||||
(void)exec.drain();
|
||||
expect_eq(errors.size(), std::size_t{1});
|
||||
throw_on_next = false;
|
||||
|
||||
sub_ptr = nullptr;
|
||||
expect(sub_res->close_and_join().has_value());
|
||||
expect_eq(svc.watch_count(), std::size_t{0});
|
||||
|
||||
// 3. Allowed symlink target change detected + queue overflow rescan_required.
|
||||
std::vector<Void::SystemAsync::WatchEventKind> sym_kinds;
|
||||
const auto sym_ok = tmp / "allowed_link";
|
||||
std::filesystem::create_symlink("target_a", sym_ok);
|
||||
auto sym_sub = svc.watch(
|
||||
sym_ok,
|
||||
[&](const Void::SystemAsync::WatchEvent& ev) {
|
||||
sym_kinds.push_back(ev.kind);
|
||||
},
|
||||
Void::SystemAsync::WatchPathOptions{.allow_symlink = true});
|
||||
require(sym_sub.has_value());
|
||||
|
||||
std::filesystem::remove(sym_ok);
|
||||
std::filesystem::create_symlink("target_b", sym_ok);
|
||||
svc.poll_once();
|
||||
(void)exec.drain();
|
||||
require(sym_kinds.size() == 1);
|
||||
expect(sym_kinds[0] == Void::SystemAsync::WatchEventKind::symlink_changed);
|
||||
|
||||
// Queue overflow across 3 watched paths when max_queued_events == 2 triggers rescan_required.
|
||||
std::vector<Void::SystemAsync::WatchEventKind> ov_kinds;
|
||||
const auto f1 = tmp / "ov1";
|
||||
const auto f2 = tmp / "ov2";
|
||||
const auto f3 = tmp / "ov3";
|
||||
auto s1 = svc.watch(f1, [&](const auto& ev) { ov_kinds.push_back(ev.kind); });
|
||||
auto s2 = svc.watch(f2, [&](const auto& ev) { ov_kinds.push_back(ev.kind); });
|
||||
auto s3 = svc.watch(f3, [&](const auto& ev) { ov_kinds.push_back(ev.kind); });
|
||||
require(s1.has_value() && s2.has_value() && s3.has_value());
|
||||
|
||||
require(Void::FileSystem::write_file(f1, "1").has_value());
|
||||
require(Void::FileSystem::write_file(f2, "2").has_value());
|
||||
require(Void::FileSystem::write_file(f3, "3").has_value());
|
||||
svc.poll_once();
|
||||
(void)exec.drain();
|
||||
bool saw_rescan = false;
|
||||
for (auto k : ov_kinds) {
|
||||
if (k == Void::SystemAsync::WatchEventKind::rescan_required) {
|
||||
saw_rescan = true;
|
||||
}
|
||||
}
|
||||
expect(saw_rescan);
|
||||
|
||||
expect(svc.shutdown().has_value());
|
||||
}
|
||||
Reference in new issue
Block a user