Skip to content

Commit 3697b56

Browse files
committed
ev/kqueue: fix completion-lifecycle use-after-free under concurrent cancellation
The kqueue backend stored a raw *Completion in the kevent udata and recovered it via @ptrFromInt in poll(). A Completion is freed when its fiber unwinds, so completing event[i] in a poll batch can resume a fiber that cancels a sibling and frees its Completion, while a stale event[j] in the same batch still carries the pointer -- poll() then dereferences freed memory (EXC_BAD_ACCESS reading completion.state). It surfaces under concurrent cross-executor cancellation (listener/endpoint teardown). Mirror the epoll backend: key each knote by (ident, filter) in a loop-owned poll_queue and re-derive the Completion from that table in poll() rather than trusting the kevent cookie (udata is no longer used for routing). A stale event resolves to a removed entry or a live Completion, never freed memory. poll() reloads the live entry from poll_queue after each completion instead of carrying a completion.next across markCompletedFromBackend, which can resume a fiber that frees a sibling waiter on the same (ident, filter). Knotes stay persistent level-triggered (no EV_ONESHOT) and the changelist is still batched into poll()'s kevent(). EV.DELETE changes carry a sentinel udata so their EV_ERROR receipts (e.g. ENOENT on an already-removed knote) are skipped rather than mis-routed to a recycled (ident, filter). The key is packed wide enough for NetBSD's i32 kevent.filter. No public API change; checkCompletion/handleKqueueError are untouched. Adds a regression test in src/io.zig.
1 parent c0382d0 commit 3697b56

2 files changed

Lines changed: 197 additions & 151 deletions

File tree

0 commit comments

Comments
 (0)