Repository navigation
Refactor cancellation to use atomic CancelState - #219
Conversation
…nting Replace the previous atomic Status with reference counting approach with a simpler CancelState packed struct: - requested: bool - cancel was requested - in_queue: bool - completion is in cancel queue (queue will call finish) - completed: bool - markCompleted ran (result is set) Key design: - state field remains non-atomic (only loop thread modifies it) - cancel_state is atomic for cross-thread cancellation coordination - in_queue flag determines who calls finishCompletion: - If in_queue=false when markCompleted runs, markCompleted finishes - If in_queue=true, cancelLocal will finish after clearing in_queue - completed flag prevents cancel from overwriting natural results The CAS in cancel() and markCompleted() ensures exactly one path calls finishCompletion, avoiding the use-after-free race where a completion in the cancel queue could be freed before processing.
📝 WalkthroughWalkthroughThis PR replaces the per-Completion Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/ev/loop.zig (1)
369-409:cancel()can runcancelLocaloff-thread when caller uses the target loop.The branch uses
self == targetto decide local vs queued cancel. If a caller on another thread callstarget_loop.cancel(...)(which the doc comment allows), this will runcancelLocaland mutate loop-owned structures off-thread (timer heap, async queue, backend cancel) — data race territory. Consider guardingcancelLocalto only run on the loop thread, otherwise enqueue totarget.cancel_queue, or tighten/clarify the API contract and assert on misuse.
Skip already-completed completions in poll loops to prevent setting results twice. This can happen with cancellations where: - cancel() marks completion as done - A kernel event arrives before the registration is removed - poll() tries to process the stale event The guard was already present in io_uring, now added to kqueue, epoll, and poll backends for consistency and defensive programming.
Summary
cancel.requestedatomic boolean with aCancelStatepacked structstatefield non-atomic (only loop thread modifies it)in_queueflag to coordinate who callsfinishCompletionDesign
The new
CancelStateis an 8-bit packed struct with three flags:requested- cancel was requestedin_queue- completion is in cancel queue (queue will call finish)completed- markCompleted ran (result is set)Key coordination:
cancel()setsrequested=trueandin_queue=truewhen queuingmarkCompleted()setscompleted=true, only finishes if!in_queuecancelLocal()clearsin_queuein defer, finishes ifcompletedwas trueThis ensures exactly one path calls
finishCompletion, preventing the use-after-free race where a completion in the cancel queue could be freed before processing.Test plan