fix(wave11): resolve adversarial review findings in EventBus, IO, and Runtime
This commit is contained in:
1 parent
7bea7df46d
commit
c6de7842f7
10 files changed
+871
-134
No files matched your search
@@ -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});
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user