Repository navigation
Cross thread cancel - #209
Conversation
📝 WalkthroughWalkthroughRefactors cancellation: removes the public Cancel type, makes cancellation a loop-level, thread-safe operation via a lock-free cross-thread cancel queue and wake flag, and migrates time-related APIs to Duration/Timestamp across loop, timers, and backends. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 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 |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
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 (3)
src/ev/backends/poll.zig (1)
14-14: Remove unusedCancelimport.The refactor removed the Cancel completion type from the submit function, making this import dead code. A grep search confirms there are no actual references to
Cancelin the file beyond the import itself.src/ev/backends/kqueue.zig (1)
16-16: Remove unusedCancelimport.The
Canceltype isn't referenced anywhere in the file (only appears in a doc comment). This import should be deleted.src/ev/backends/epoll.zig (1)
15-15: Remove unused import on line 15.The
Canceltype is imported but never used. The code only referenceserror.Canceled(the error value), not theCanceltype itself.
🧹 Nitpick comments (1)
src/ev/test/cancel.zig (1)
493-538: Cross-thread cancellation test: minor robustness concern.The test works, but there's a subtle timing window: between
loop1.add(&timer.c)(line 509) and the cancel thread starting, loop1 hasn't calledrun()yet. The timer's state is.runningafter add, so the cancel should work.However, the polling loop at lines 526-532 uses
loop.run(.no_wait)which doesn't block - the 1ms sleep between iterations is the actual wait mechanism. This is fine for testing but slightly inefficient.Consider using
loop.run(.once)instead ofno_wait+ sleep for cleaner blocking:while (!completed.load(.acquire)) { try loop1.run(.once); if (wall_timer.read() > 1 * std.time.ns_per_s) { return error.TestTimeout; } }This would make the test faster since
.onceblocks until an event occurs.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
1 similar comment
✅ Actions performedReview triggered.
|
- Add loop field to Completion to track which loop owns it - Add cancel struct to Completion with next pointer and requested atomic - Add cancel_queue to Loop for cross-thread cancel requests - Add wake_cancel bit to LoopState - Remove redundant loop field from Async (use c.loop instead) - Remove redundant loop field from AutoCancel (use timer.c.loop instead) - Set completion.loop in add() and setTimer() This is Phase 1 of the cross-thread cancellation refactor - adding the infrastructure without changing the cancellation logic yet.
- Rewrite cancel() to handle same-loop vs cross-loop cases - Same-loop: call cancelLocal() directly - Cross-loop: push to target's cancel_queue and wake it - Add cancelLocal() with the actual cancellation logic - Add processCancelQueue() to drain cross-thread requests - Process cancel queue in tick() when wake_cancel flag is set - Simplify cancelInternal() to use cancelLocal() Phase 2 of cross-thread cancellation refactor.
Remove the Cancel completion and just call loop.cancel() directly, then wait for the target operation to complete. Phase 3 of cross-thread cancellation refactor.
- Remove Cancel completion type (struct with target field) - Remove .cancel from Op enum - Remove canceled and canceled_by fields from Completion - Remove cancelInternal() function - Remove recursive completion logic from markCompleted() - Clean up Cancel handling in loop.add() - Update backends to remove .cancel references and outdated comments - Update tests to use cancel.requested instead of canceled field - Remove tests that relied on Cancel completion type This is a net removal of ~380 lines of code. Phase 4 of cross-thread cancellation refactor.
Test that verifies cancelling an operation from a different thread/loop works correctly via the cancel queue mechanism. Phase 5 of cross-thread cancellation refactor.
- Remove error returns from cancel() - it's now fire-and-forget - Double cancel is no-op - Cancel after completion is no-op - Cancel before submit marks it, fails on add - Update tests to reflect idempotent behavior
1de4a9a to
ed6f367
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/ev/backends/io_uring.zig (1)
15-15: Unused import:Canceltype was removed fromcompletion.zig.The
Canceltype no longer exists in the completion module (removed as part of this PR's cancellation refactor). This import will cause a compile error.🔧 Proposed fix
-const Cancel = `@import`("../completion.zig").Cancel;
🤖 Fix all issues with AI agents
In `@src/ev/backends/epoll.zig`:
- Around line 396-401: The current cancel path calls
removeFromPollQueue(getHandle(target), target) and on catch simply logs and
returns, which can leave the completion stuck; change the catch handler to
either reinsert the completion or mark/complete the waiter with error.Canceled
(use the same completion finalization path used elsewhere) so the waiter is not
left in .running — locate removeFromPollQueue and the cancel caller in
epoll.zig, capture the removal error, and call the completion finalize/complete
routine (or requeue via the same queue insertion helper) before returning
instead of only logging.
No description provided.