fix(wave11): resolve adversarial review findings in EventBus, IO, and Runtime

This commit is contained in:
faumaray committed 2026-12-01 22:03:35 +03:00
1 parent 7bea7df46d
commit c6de7842f7
10 files changed
+871 -134

No files matched your search

+169
View File
@@ -598,3 +598,172 @@ Void::Testing::Test bus_flush_blocking_self_join_and_manual_invalid()
Void::Testing::require(exec->join().has_value());
}
}
Void::Testing::Test bus_pumping_self_join_returns_self_join()
{
using namespace Void::EventBus;
Void::Async::ManualExecutor manual;
EventBus bus{manual};
std::optional<Void::Result::Result<void>> fp_res;
std::optional<Void::Result::Result<void>> cj_res;
auto sub = bus.try_subscribe<Ping>([&](const Ping&) {
fp_res = bus.flush_pumping(manual);
cj_res = bus.close_and_join_pumping(manual);
});
Void::Testing::require(sub.has_value());
Void::Testing::require(bus.try_publish(Ping{7}).has_value());
(void)manual.drain();
Void::Testing::require(fp_res.has_value() && !fp_res->has_value());
Void::Testing::expect_eq(
async_code(fp_res->error()),
static_cast<std::int32_t>(Void::Async::AsyncErrorCode::self_join));
Void::Testing::require(cj_res.has_value() && !cj_res->has_value());
Void::Testing::expect_eq(
async_code(cj_res->error()),
static_cast<std::int32_t>(Void::Async::AsyncErrorCode::self_join));
Void::Testing::require(bus.close_and_join_pumping(manual).has_value());
}
Void::Testing::Test bus_close_and_join_waits_for_slow_capture_destructor()
{
using namespace Void::EventBus;
struct SlowGuard {
explicit SlowGuard(std::atomic<bool>* p) : destroyed(p) {}
std::atomic<bool>* destroyed{nullptr};
~SlowGuard()
{
if (destroyed != nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds{15});
destroyed->store(true, std::memory_order_release);
}
}
};
// 1. ThreadExecutor subscription::close_and_join waits until slow capture dtor finishes.
{
auto exec_res = Void::Async::ThreadExecutor::create(
Void::Async::ThreadExecutorOptions{.workers = 2, .max_admitted = 16});
Void::Testing::require(exec_res.has_value());
auto exec = std::move(*exec_res);
Void::Testing::require(exec->start().has_value());
EventBus bus{*exec};
std::atomic<bool> destroyed{false};
std::latch entered{1};
std::promise<void> rel;
auto rel_fut = rel.get_future().share();
auto guard = std::make_shared<SlowGuard>(&destroyed);
auto sub = bus.try_subscribe<Ping>([&, guard](const Ping&) {
(void)guard;
entered.count_down();
rel_fut.wait();
});
Void::Testing::require(sub.has_value());
guard.reset();
Void::Testing::require(bus.try_publish(Ping{1}).has_value());
entered.wait();
std::thread releaser([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds{5});
rel.set_value();
});
Void::Testing::require(sub->close_and_join().has_value());
// Must ALREADY be destroyed when close_and_join returns!
Void::Testing::expect(destroyed.load(std::memory_order_acquire));
releaser.join();
Void::Testing::require(bus.close_and_join().has_value());
exec->close();
Void::Testing::require(exec->join().has_value());
}
// 2. Mode::Async EventBus::close_and_join waits until slow capture dtor finishes.
{
EventBus bus{Mode::Async};
std::atomic<bool> destroyed{false};
std::latch entered{1};
std::promise<void> rel;
auto rel_fut = rel.get_future().share();
auto guard = std::make_shared<SlowGuard>(&destroyed);
auto sub = bus.try_subscribe<Ping>([&, guard](const Ping&) {
(void)guard;
entered.count_down();
rel_fut.wait();
});
Void::Testing::require(sub.has_value());
guard.reset();
bus.publish(Ping{2});
entered.wait();
std::thread releaser([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds{5});
rel.set_value();
});
Void::Testing::require(bus.close_and_join().has_value());
Void::Testing::expect(destroyed.load(std::memory_order_acquire));
releaser.join();
}
}
Void::Testing::Test bus_legacy_async_purged_drop_outside_lock()
{
using namespace Void::EventBus;
struct MuLockingDtor {
EventBus* bus{nullptr};
std::atomic<bool>* dropped{nullptr};
std::atomic<int>* calls{nullptr};
~MuLockingDtor()
{
if (bus != nullptr) {
bus->unsubscribe(0); // Acquires BusCore::mu; would deadlock under lock.
}
if (dropped != nullptr) {
dropped->store(true, std::memory_order_release);
}
if (calls != nullptr) {
calls->fetch_add(1, std::memory_order_relaxed);
}
}
};
EventBus bus{Mode::Async};
std::atomic<bool> dropped{false};
std::atomic<int> dtor_calls{0};
std::atomic<int> b_ran{0};
std::optional<Void::Result::Result<void>> sub_b_join_res;
auto sub_b_ptr = std::make_shared<std::optional<EventBus::Subscription>>();
// Subscribe A before B so run_worker visits A first for the same queued item.
auto sub_a = bus.try_subscribe<Ping>([&](const Ping&) {
if (*sub_b_ptr) {
sub_b_join_res = (*sub_b_ptr)->close_and_join();
}
});
Void::Testing::require(sub_a.has_value());
{
auto guard = std::shared_ptr<MuLockingDtor>(
new MuLockingDtor{&bus, &dropped, &dtor_calls});
auto sb = bus.try_subscribe<Ping>([&, guard](const Ping&) {
(void)guard;
b_ran.fetch_add(1, std::memory_order_relaxed);
});
Void::Testing::require(sb.has_value());
*sub_b_ptr = std::move(*sb);
}
bus.publish(Ping{1});
bus.flush();
Void::Testing::require(sub_b_join_res.has_value());
Void::Testing::expect(sub_b_join_res->has_value());
Void::Testing::expect_eq(b_ran.load(), 0);
Void::Testing::expect(dropped.load(std::memory_order_acquire));
Void::Testing::expect_eq(dtor_calls.load(), 1);
Void::Testing::require(bus.close_and_join().has_value());
}
+24
View File
@@ -267,3 +267,27 @@ Void::Testing::Test io_pump_until_exits_after_shutdown_complete()
require(scope.join_pumping(exec).has_value());
}
}
Void::Testing::Test io_pump_until_mock_max_wait_paces_idle_turns()
{
ManualExecutor exec;
const auto& clock = Void::Time::SystemMonotonicClock::instance();
auto drv_res = Driver::create(
exec, BackendOptions{.preference = BackendPreference::mock}, clock);
require(drv_res.has_value());
Driver& drv = **drv_res;
auto res = pump_until(
drv,
exec,
[] { return false; },
PumpOptions{
.max_wait = 5ms,
.max_turns = 200,
.operation = OperationOptions{.deadline = clock.now() + 20ms},
});
require(!res.has_value());
expect_eq(res.error().code().value,
static_cast<std::int32_t>(IoErrorCode::timeout));
expect(drv.telemetry().turns < std::uint64_t{100});
}
+204
View File
@@ -530,3 +530,207 @@ Void::Testing::Test io_run_busy_and_mock_wait_does_not_spin()
expect(drv.telemetry().turns < 50);
}
}
Void::Testing::Test io_shutdown_epoll_idle_readable_fd_no_busy_spin()
{
ManualExecutor exec;
auto drv_res = Driver::create(
exec, BackendOptions{.preference = BackendPreference::epoll});
require(drv_res.has_value());
Driver& drv = **drv_res;
auto pair_res = drv.create_socket_pair();
require(pair_res.has_value());
ByteStream a = std::move(pair_res->first);
ByteStream b = std::move(pair_res->second);
// Push unread data into stream `a` while NO read operation is pending on `a`.
TaskScope scope{exec};
const std::array<std::byte, 4> msg{
std::byte{'p'}, std::byte{'i'}, std::byte{'n'}, std::byte{'g'}};
std::optional<Void::Result::Result<Transfer>> wres;
auto wtask = [](ByteStream* st,
std::span<const std::byte> sp,
std::optional<Void::Result::Result<Transfer>>* out)
-> Void::Coro::Task<void> { *out = co_await st->write_some(sp); };
require(scope.spawn(wtask(&b, msg, &wres)).has_value());
(void)exec.drain();
while (!wres.has_value()) {
(void)drv.poll_once(1ms);
(void)exec.drain();
}
require(wres->has_value());
expect_eq((*wres)->bytes, std::size_t{4});
// Running driver with unread data on `a` and no active reader must NOT busy-spin.
std::stop_source stop;
Void::Result::Result<std::size_t> run_res{std::size_t{0}};
std::thread runner([&]() { run_res = drv.run(stop.get_token(), 10ms); });
std::this_thread::sleep_for(40ms);
stop.request_stop();
runner.join();
require(run_res.has_value());
expect(drv.telemetry().turns <= std::uint64_t{15});
auto rep = drv.shutdown(OperationOptions{});
require(rep.has_value());
expect(rep->complete);
require(scope.join_pumping(exec).has_value());
(void)a;
}
Void::Testing::Test io_shutdown_mock_try_acquire_race_tsan()
{
// 1. try_acquire_read refuses once stream close or driver shutdown begins.
{
ManualExecutor exec;
auto drv_res = Driver::create(
exec, BackendOptions{.preference = BackendPreference::mock});
require(drv_res.has_value());
Driver& drv = **drv_res;
auto* ctrl = drv.mock_controller();
require(ctrl != nullptr);
auto s_res = drv.create_mock_stream();
require(s_res.has_value());
ByteStream s = std::move(*s_res);
std::array<std::byte, 8> buf{};
std::optional<Void::Result::Result<Transfer>> rres;
TaskScope scope{exec};
auto rtask = [](ByteStream* st,
std::span<std::byte> sp,
std::optional<Void::Result::Result<Transfer>>* out)
-> Void::Coro::Task<void> { *out = co_await st->read_some(sp); };
require(scope.spawn(rtask(&s, buf, &rres)).has_value());
(void)exec.drain();
std::optional<Void::Result::Result<void>> cres;
auto ctask = [](ByteStream* st,
std::optional<Void::Result::Result<void>>* out)
-> Void::Coro::Task<void> { *out = co_await st->close_and_join(); };
require(scope.spawn(ctask(&s, &cres)).has_value());
(void)exec.drain();
expect(!ctrl->try_acquire_read(s).has_value());
(void)drv.poll_once(0ns);
require(scope.join_pumping(exec).has_value());
auto rep = drv.shutdown(OperationOptions{});
require(rep.has_value());
expect(rep->complete);
}
// 2. Concurrent race between try_acquire_read and stop_requested cancellation (TSan).
{
ManualExecutor exec;
auto drv_res = Driver::create(
exec, BackendOptions{.preference = BackendPreference::mock});
require(drv_res.has_value());
Driver& drv = **drv_res;
auto* ctrl = drv.mock_controller();
require(ctrl != nullptr);
auto s_res = drv.create_mock_stream();
require(s_res.has_value());
ByteStream s = std::move(*s_res);
const std::array<std::byte, 2> payload{std::byte{'o'}, std::byte{'k'}};
for (int iter = 0; iter < 30; ++iter) {
std::array<std::byte, 8> buf{};
std::optional<Void::Result::Result<Transfer>> rres;
std::stop_source stop_src;
TaskScope scope{exec};
auto rtask = [](ByteStream* st,
std::span<std::byte> sp,
std::stop_token tok,
std::optional<Void::Result::Result<Transfer>>* out)
-> Void::Coro::Task<void> {
*out = co_await st->read_some(sp, OperationOptions{.stop = tok});
};
require(scope.spawn(rtask(&s, buf, stop_src.get_token(), &rres)).has_value());
(void)exec.drain();
std::atomic<bool> finish_ok{true};
std::thread finisher([&]() {
if (auto tok = ctrl->try_acquire_read(s)) {
finish_ok.store(
ctrl->finish_read_data(*tok, payload, false),
std::memory_order_release);
}
});
std::thread canceller([&]() {
stop_src.request_stop();
(void)drv.poll_once(0ns);
});
finisher.join();
canceller.join();
expect(finish_ok.load(std::memory_order_acquire));
(void)drv.poll_once(0ns);
require(scope.join_pumping(exec).has_value());
require(rres.has_value());
if (rres->has_value()) {
expect_eq((*rres)->bytes, std::size_t{2});
} else {
expect_eq(rres->error().code().value,
static_cast<std::int32_t>(IoErrorCode::cancelled));
}
}
auto rep = drv.shutdown(OperationOptions{});
require(rep.has_value());
expect(rep->complete);
}
// 3. Concurrent race between try_acquire_read and Driver::shutdown (TSan).
for (int iter = 0; iter < 15; ++iter) {
ManualExecutor exec;
auto drv_res = Driver::create(
exec, BackendOptions{.preference = BackendPreference::mock});
require(drv_res.has_value());
Driver& drv = **drv_res;
auto* ctrl = drv.mock_controller();
require(ctrl != nullptr);
auto s_res = drv.create_mock_stream();
require(s_res.has_value());
ByteStream s = std::move(*s_res);
std::array<std::byte, 8> buf{};
std::optional<Void::Result::Result<Transfer>> rres;
TaskScope scope{exec};
auto rtask = [](ByteStream* st,
std::span<std::byte> sp,
std::optional<Void::Result::Result<Transfer>>* out)
-> Void::Coro::Task<void> { *out = co_await st->read_some(sp); };
require(scope.spawn(rtask(&s, buf, &rres)).has_value());
(void)exec.drain();
const std::array<std::byte, 2> payload{std::byte{'o'}, std::byte{'k'}};
std::atomic<bool> finish_ok{true};
std::thread finisher([&]() {
if (auto tok = ctrl->try_acquire_read(s)) {
finish_ok.store(
ctrl->finish_read_data(*tok, payload, false),
std::memory_order_release);
}
});
std::optional<Void::Result::Result<DriverShutdownReport>> sd_rep;
std::thread shutdowner([&]() {
sd_rep = drv.shutdown(OperationOptions{});
});
finisher.join();
shutdowner.join();
expect(finish_ok.load(std::memory_order_acquire));
require(sd_rep.has_value() && sd_rep->has_value());
expect((*sd_rep)->complete);
require(scope.join_pumping(exec).has_value());
require(rres.has_value());
if (rres->has_value()) {
expect_eq((*rres)->bytes, std::size_t{2});
} else {
expect_eq(rres->error().code().value,
static_cast<std::int32_t>(IoErrorCode::closed));
}
}
}
@@ -1,5 +1,6 @@
#include <Void/Async/Executor.hpp>
#include <Void/Async/Task.hpp>
#include <Void/Async/ThreadExecutor.hpp>
#include <Void/Async/Timer.hpp>
#include <Void/IO/Driver.hpp>
#include <Void/IO/File.hpp>
@@ -939,3 +940,93 @@ Void::Testing::Test runtime_destructor_blocks_until_complete_after_incomplete_sh
expect(task_finished.load(std::memory_order_acquire));
expect_eq(wait_for_task_count(baseline), baseline);
}
Void::Testing::Test runtime_embedded_manual_poll_shutdown_completes_exporter()
{
ManualExecutor manual;
Void::Time::ManualClock clock;
auto drv_res = Void::IO::Driver::create(
manual,
Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock},
clock);
require(drv_res.has_value());
auto exp = std::make_shared<CompletionProbeComponent>();
HostBuilder builder;
builder.with_embedded(EmbeddedResources{
.executor = &manual,
.io = drv_res->get(),
.clock = &clock,
});
require(builder.add_component("exp", ResourceKind::exporter, exp).has_value());
auto host_res = builder.build();
require(host_res.has_value());
Host host = std::move(*host_res);
require(host.start().has_value());
std::optional<ShutdownReport> rep;
for (int i = 0; i < 10; ++i) {
auto pr = host.poll_shutdown();
require(pr.has_value());
rep = *pr;
if (rep->complete) {
break;
}
}
require(rep.has_value());
expect(rep->complete);
expect(rep->state == HostState::stopped);
expect(exp->stop_ran.load(std::memory_order_acquire));
}
Void::Testing::Test runtime_embedded_thread_executor_shutdown_blocking_succeeds()
{
const std::size_t baseline = settle_baseline_tasks();
auto exec_res = Void::Async::ThreadExecutor::create(
Void::Async::ThreadExecutorOptions{.workers = 2, .max_admitted = 32});
require(exec_res.has_value());
auto exec = std::move(*exec_res);
require(exec->start().has_value());
auto drv_res = Void::IO::Driver::create(
*exec,
Void::IO::BackendOptions{.preference = Void::IO::BackendPreference::mock});
require(drv_res.has_value());
auto svc = std::make_shared<CompletionProbeComponent>();
std::atomic<bool> task_done{false};
{
HostBuilder builder;
builder.with_embedded(EmbeddedResources{
.executor = exec.get(),
.io = drv_res->get(),
});
require(builder.add_component("svc", ResourceKind::service, svc).has_value());
auto host_res = builder.build();
require(host_res.has_value());
Host host = std::move(*host_res);
require(host.start().has_value());
auto jh = host.spawn(quick_done_task(&task_done));
require(jh.has_value());
auto rep = host.shutdown_blocking();
require(rep.has_value());
expect(rep->complete);
expect(rep->state == HostState::stopped);
}
expect(task_done.load(std::memory_order_acquire));
expect(svc->stop_ran.load(std::memory_order_acquire));
expect(!(*drv_res)->shutdown_complete());
expect(exec->try_reserve_slot().has_value());
exec->release_reserved();
auto drep = (*drv_res)->shutdown(OperationOptions{});
require(drep.has_value() && drep->complete);
exec->close();
require(exec->join().has_value());
expect_eq(wait_for_task_count(baseline), baseline);
}
+79
View File
@@ -14,6 +14,7 @@
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <latch>
#include <mutex>
#include <optional>
#include <string>
@@ -692,3 +693,81 @@ Void::Testing::Test runtime_moved_from_host_safe_defaults()
h1.begin_shutdown();
}
Void::Testing::Test runtime_begin_shutdown_during_start_prevents_ready()
{
const std::size_t baseline = settle_baseline_tasks();
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);
bool invoked = false;
auto sr = host.start(StartOptions{
.before_step = [&](StartStep step) -> Void::Result::Result<void> {
if (step == StartStep::components && !invoked) {
invoked = true;
std::thread t([&]() { host.begin_shutdown(); });
t.join();
}
return {};
},
});
require(sr.has_value());
expect(invoked);
expect(!host.is_ready());
expect(host.state() == HostState::stopping_admission);
std::atomic<bool> flag{false};
auto spw = host.spawn(set_flag_task(&flag));
expect(!spw.has_value());
expect(spw.error() == Void::Async::AdmissionError::stopped);
auto rep = host.shutdown_blocking();
require(rep.has_value());
expect(rep->complete);
expect(rep->state == HostState::stopped);
expect_eq(wait_for_task_count(baseline), baseline);
}
Void::Testing::Test runtime_concurrent_shutdown_waits_for_start_tsan()
{
const std::size_t baseline = settle_baseline_tasks();
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);
std::latch entered_components{1};
std::optional<Void::Result::Result<Void::Runtime::ShutdownReport>> sd_res;
std::thread stopper([&]() {
entered_components.wait();
sd_res = host.shutdown_blocking();
});
auto sr = host.start(StartOptions{
.before_step = [&](StartStep step) -> Void::Result::Result<void> {
if (step == StartStep::components) {
entered_components.count_down();
std::this_thread::sleep_for(10ms);
}
return {};
},
});
stopper.join();
require(sr.has_value());
require(sd_res.has_value() && sd_res->has_value());
expect((*sd_res)->complete);
expect(host.state() == HostState::stopped);
expect(!host.is_ready());
expect_eq(wait_for_task_count(baseline), baseline);
}