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
+64
-61
@@ -189,6 +189,7 @@ struct DescriptorSlot {
|
||||
bool close_enqueued{false};
|
||||
bool eof_seen{false};
|
||||
bool epoll_registered{false};
|
||||
bool epoll_in_armed{false};
|
||||
bool epoll_out_armed{false};
|
||||
// S5: a File occupies a slot with fd == -1; socket paths skip it and the
|
||||
// file descriptor lives only in FileState.
|
||||
@@ -731,6 +732,8 @@ protected:
|
||||
virtual std::size_t drain_socket_queues(std::vector<Async::ResumeTicket>& tickets) = 0;
|
||||
// Stopping: the slot's interest is dropped (poll owner only, mu_ held).
|
||||
virtual void on_slot_abort_locked(DescriptorSlot&) noexcept {}
|
||||
// Called under mu_ right after an op is cleared from a slot.
|
||||
virtual void on_op_retired_locked(DescriptorSlot&) noexcept {}
|
||||
// Shutdown step 5 for socket slots (mu_ held): close the native fd.
|
||||
[[nodiscard]] virtual bool closes_native_fds() const noexcept { return false; }
|
||||
virtual void on_slot_final_close_locked(DescriptorSlot&) noexcept {}
|
||||
@@ -800,6 +803,7 @@ protected:
|
||||
s.close_enqueued = false;
|
||||
s.eof_seen = false;
|
||||
s.epoll_registered = false;
|
||||
s.epoll_in_armed = false;
|
||||
s.epoll_out_armed = false;
|
||||
s.is_file = false;
|
||||
s.file.reset();
|
||||
@@ -830,6 +834,7 @@ protected:
|
||||
s.close_enqueued = false;
|
||||
s.eof_seen = false;
|
||||
s.epoll_registered = false;
|
||||
s.epoll_in_armed = false;
|
||||
s.epoll_out_armed = false;
|
||||
s.is_file = false;
|
||||
s.file.reset();
|
||||
@@ -867,6 +872,7 @@ protected:
|
||||
} else if (op.dir == Direction::write && slot.active_write.get() == &op) {
|
||||
slot.active_write.reset();
|
||||
}
|
||||
on_op_retired_locked(slot);
|
||||
if (slot.closing && !slot.active_read && !slot.active_write &&
|
||||
kind() == BackendPreference::mock) {
|
||||
ready_close_waiters.insert(
|
||||
@@ -886,10 +892,14 @@ protected:
|
||||
bool complete_op_and_collect_tickets(const std::shared_ptr<PendingOp>& op,
|
||||
const Void::Result::Result<Transfer>& res,
|
||||
std::vector<Async::ResumeTicket>& out_tickets,
|
||||
std::vector<CloseWaiter>& close_waiters)
|
||||
std::vector<CloseWaiter>& close_waiters,
|
||||
bool skip_if_executing = false)
|
||||
{
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
if (skip_if_executing && op->in_execution) {
|
||||
return false;
|
||||
}
|
||||
if (op->retired) {
|
||||
duplicate_completions_ignored_.fetch_add(1, std::memory_order_acq_rel);
|
||||
return false;
|
||||
@@ -970,7 +980,8 @@ protected:
|
||||
op,
|
||||
std::unexpected{make_io_error(IoErrorCode::closed, "IO Driver shut down")},
|
||||
tickets,
|
||||
close_waiters)) {
|
||||
close_waiters,
|
||||
/*skip_if_executing=*/true)) {
|
||||
++done;
|
||||
}
|
||||
}
|
||||
@@ -1774,11 +1785,15 @@ public:
|
||||
[[nodiscard]] std::size_t pending_count(DescriptorId id, Direction dir) const noexcept
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
if (stopping_.load(std::memory_order_acquire) ||
|
||||
shutdown_begun_.load(std::memory_order_acquire)) {
|
||||
return 0;
|
||||
}
|
||||
if (!id.valid() || id.slot >= slots_.size()) {
|
||||
return 0;
|
||||
}
|
||||
const auto& s = slots_[id.slot];
|
||||
if (!s.in_use || s.is_file || s.id.generation != id.generation) {
|
||||
if (!s.in_use || s.is_file || s.closing || s.id.generation != id.generation) {
|
||||
return 0;
|
||||
}
|
||||
const auto& op = (dir == Direction::read) ? s.active_read : s.active_write;
|
||||
@@ -1789,11 +1804,15 @@ public:
|
||||
try_acquire(DescriptorId id, Direction dir)
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
if (stopping_.load(std::memory_order_acquire) ||
|
||||
shutdown_begun_.load(std::memory_order_acquire)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!id.valid() || id.slot >= slots_.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto& s = slots_[id.slot];
|
||||
if (!s.in_use || s.is_file || s.id.generation != id.generation) {
|
||||
if (!s.in_use || s.is_file || s.closing || s.id.generation != id.generation) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto& op = (dir == Direction::read) ? s.active_read : s.active_write;
|
||||
@@ -1909,19 +1928,12 @@ protected:
|
||||
std::size_t processed = 0;
|
||||
std::vector<CloseWaiter> close_waiters;
|
||||
for (const auto& op : cancels) {
|
||||
bool executing = false;
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
executing = op->in_execution && !op->retired;
|
||||
}
|
||||
if (executing) {
|
||||
continue; // Held by a MockController token: stop intent only.
|
||||
}
|
||||
if (complete_op_and_collect_tickets(
|
||||
op,
|
||||
std::unexpected{make_io_error(IoErrorCode::cancelled, "IO operation cancelled")},
|
||||
tickets,
|
||||
close_waiters)) {
|
||||
close_waiters,
|
||||
/*skip_if_executing=*/true)) {
|
||||
++processed;
|
||||
}
|
||||
}
|
||||
@@ -2020,19 +2032,12 @@ private:
|
||||
std::size_t processed = 0;
|
||||
std::vector<CloseWaiter> close_waiters;
|
||||
for (const auto& op : expired) {
|
||||
bool executing = false;
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
executing = op->in_execution && !op->retired;
|
||||
}
|
||||
if (executing) {
|
||||
continue;
|
||||
}
|
||||
if (complete_op_and_collect_tickets(
|
||||
op,
|
||||
std::unexpected{make_io_error(IoErrorCode::timeout, "IO operation timed out")},
|
||||
tickets,
|
||||
close_waiters)) {
|
||||
close_waiters,
|
||||
/*skip_if_executing=*/true)) {
|
||||
++processed;
|
||||
}
|
||||
}
|
||||
@@ -2375,9 +2380,15 @@ protected:
|
||||
(void)::epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, s.fd, nullptr);
|
||||
}
|
||||
s.epoll_registered = false;
|
||||
s.epoll_in_armed = false;
|
||||
s.epoll_out_armed = false;
|
||||
}
|
||||
|
||||
void on_op_retired_locked(DescriptorSlot& s) noexcept override
|
||||
{
|
||||
sync_epoll_interest_locked(s);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool closes_native_fds() const noexcept override { return true; }
|
||||
|
||||
void on_slot_final_close_locked(DescriptorSlot& s) noexcept override
|
||||
@@ -2395,7 +2406,9 @@ protected:
|
||||
Direction dir,
|
||||
const std::shared_ptr<PendingOp>&) override
|
||||
{
|
||||
if (!slot.epoll_registered || (dir == Direction::write && !slot.epoll_out_armed)) {
|
||||
if (!slot.epoll_registered ||
|
||||
(dir == Direction::read && !slot.epoll_in_armed) ||
|
||||
(dir == Direction::write && !slot.epoll_out_armed)) {
|
||||
control_queue_.push_back(ControlAction{ControlAction::Type::arm_interest, slot.id});
|
||||
}
|
||||
return {};
|
||||
@@ -2427,13 +2440,30 @@ private:
|
||||
|
||||
void sync_epoll_interest_locked(DescriptorSlot& slot) noexcept
|
||||
{
|
||||
if (!slot.in_use || slot.is_file || slot.fd < 0 || slot.closing || epoll_fd_ < 0) {
|
||||
if (!slot.in_use || slot.is_file || slot.fd < 0 || epoll_fd_ < 0) {
|
||||
return;
|
||||
}
|
||||
const bool want_in =
|
||||
!slot.closing &&
|
||||
(slot.active_read != nullptr && !slot.active_read->retired);
|
||||
const bool want_out =
|
||||
!slot.closing &&
|
||||
(slot.active_write != nullptr && !slot.active_write->retired);
|
||||
if (!want_in && !want_out) {
|
||||
if (slot.epoll_registered) {
|
||||
(void)::epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, slot.fd, nullptr);
|
||||
slot.epoll_registered = false;
|
||||
slot.epoll_in_armed = false;
|
||||
slot.epoll_out_armed = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
const bool want_out = (slot.active_write != nullptr && !slot.active_write->retired);
|
||||
epoll_event ev{};
|
||||
std::uint32_t events = static_cast<std::uint32_t>(EPOLLIN) |
|
||||
static_cast<std::uint32_t>(EPOLLRDHUP);
|
||||
std::uint32_t events = 0;
|
||||
if (want_in) {
|
||||
events |= static_cast<std::uint32_t>(EPOLLIN) |
|
||||
static_cast<std::uint32_t>(EPOLLRDHUP);
|
||||
}
|
||||
if (want_out) {
|
||||
events |= static_cast<std::uint32_t>(EPOLLOUT);
|
||||
}
|
||||
@@ -2442,10 +2472,12 @@ private:
|
||||
if (!slot.epoll_registered) {
|
||||
if (::epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, slot.fd, &ev) == 0) {
|
||||
slot.epoll_registered = true;
|
||||
slot.epoll_in_armed = want_in;
|
||||
slot.epoll_out_armed = want_out;
|
||||
}
|
||||
} else if (slot.epoll_out_armed != want_out) {
|
||||
} else if (slot.epoll_in_armed != want_in || slot.epoll_out_armed != want_out) {
|
||||
if (::epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, slot.fd, &ev) == 0) {
|
||||
slot.epoll_in_armed = want_in;
|
||||
slot.epoll_out_armed = want_out;
|
||||
}
|
||||
}
|
||||
@@ -2485,30 +2517,12 @@ private:
|
||||
}
|
||||
|
||||
for (const auto& op : cancels) {
|
||||
bool executing = false;
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
executing = op->in_execution && !op->retired;
|
||||
if (!executing && !op->retired &&
|
||||
op->desc_id.slot < slots_.size() &&
|
||||
slots_[op->desc_id.slot].in_use &&
|
||||
slots_[op->desc_id.slot].id.generation == op->desc_id.generation) {
|
||||
auto& slot = slots_[op->desc_id.slot];
|
||||
if (op->dir == Direction::write && slot.epoll_out_armed) {
|
||||
slot.active_write.reset();
|
||||
sync_epoll_interest_locked(slot);
|
||||
slot.active_write = op;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (executing) {
|
||||
continue; // The syscall's real result settles it (D1).
|
||||
}
|
||||
if (complete_op_and_collect_tickets(
|
||||
op,
|
||||
std::unexpected{make_io_error(IoErrorCode::cancelled, "IO operation cancelled")},
|
||||
tickets,
|
||||
close_waiters)) {
|
||||
close_waiters,
|
||||
/*skip_if_executing=*/true)) {
|
||||
++done;
|
||||
}
|
||||
}
|
||||
@@ -2570,23 +2584,12 @@ private:
|
||||
std::size_t done = 0;
|
||||
std::vector<CloseWaiter> close_waiters;
|
||||
for (const auto& op : expired) {
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
if (!op->retired && op->dir == Direction::write &&
|
||||
op->desc_id.slot < slots_.size() &&
|
||||
slots_[op->desc_id.slot].in_use &&
|
||||
slots_[op->desc_id.slot].id.generation == op->desc_id.generation) {
|
||||
auto& s = slots_[op->desc_id.slot];
|
||||
s.active_write.reset();
|
||||
sync_epoll_interest_locked(s);
|
||||
s.active_write = op;
|
||||
}
|
||||
}
|
||||
if (complete_op_and_collect_tickets(
|
||||
op,
|
||||
std::unexpected{make_io_error(IoErrorCode::timeout, "IO operation timed out")},
|
||||
tickets,
|
||||
close_waiters)) {
|
||||
close_waiters,
|
||||
/*skip_if_executing=*/true)) {
|
||||
++done;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user