Repository navigation
Add Signal.timedWait with timeout support - #75
Conversation
Implements timeout support for signal waiting by adding: - timedWaitForIo() in src/io/base.zig for timed I/O operations - Signal.timedWait() in src/signal.zig with timeout parameter - Comprehensive tests for both timeout and success cases - Fix xev io_uring poll operation to handle ECANCELED properly The implementation follows the same pattern as other sync primitives (ResetEvent, Notify) and properly handles cancellation using the shield mechanism. Fixes #73
WalkthroughA new public Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Signal
participant Runtime as Runtime (I/O)
participant Executor
participant XEV as XEV (backend)
User->>Signal: Signal.timedWait(rt, timeout_ns)
Signal->>Signal: create WaitContext & completion
Signal->>Runtime: timedWaitForIo(completion, timeout_ns)
Runtime->>Executor: timedWaitForReadyWithCallback(completion, callback, timeout_ns)
alt I/O ready before timeout
XEV-->>Executor: completion ready
Executor-->>Runtime: callback invoked (ready)
Runtime->>Signal: resume task (success)
else Timeout fires first
Executor-->>Runtime: callback invoked (timeout)
Runtime->>XEV: cancel I/O (once)
Runtime->>Signal: resume task (error.Timeout)
else External cancellation
XEV-->>Executor: operation returns CANCELED
Executor-->>Runtime: callback invoked (canceled)
Runtime->>XEV: cancel I/O (once)
Runtime->>Signal: resume task (error.Canceled)
end
Signal->>Signal: consume counter if needed
Signal-->>User: return (void / Timeout / Canceled)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🔇 Additional comments (4)
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: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/io/base.zig(1 hunks)src/signal.zig(2 hunks)vendor/libxev/src/backend/io_uring.zig(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-10T05:59:17.087Z
Learnt from: lalinsky
PR: lalinsky/zio#41
File: src/udp.zig:79-82
Timestamp: 2025-10-10T05:59:17.087Z
Learning: In UDP socket operations (src/udp.zig), error.Canceled from result_data.result represents OS/IO-level cancellation (e.g., io_uring), not user-requested cancellation. User cancellation is handled by waitForXevCompletion() via try. OS-level error.Canceled should be mapped to error.Unexpected as it represents an unexpected system event.
Applied to files:
vendor/libxev/src/backend/io_uring.zig
🔇 Additional comments (4)
src/signal.zig (3)
274-323: Signal.timedWait mirrors wait semantics correctly and integrates with timeout path.Fast‑path counter check, Async wait wiring, and post‑success counter consumption look solid.
410-437: Test: timeout case is focused and deterministic.Cleanly asserts Timeout without side effects. LGTM.
439-476: Test: signal-before-timeout happy path covered.Covers end‑to‑end flow; timing margins are reasonable. LGTM.
vendor/libxev/src/backend/io_uring.zig (1)
772-777: I need broader context about how poll errors are used upstream. Let me search more comprehensively:The web search confirms that ECANCELED is returned by io_uring when IO_POLL_CANCEL_FLAG is set, representing a legitimate system event. Now let me verify the specific UDP context from the retrieved learning:
Verified: ECANCELED → error.Canceled for poll is correct and aligns with other operations.
No code in the non-vendor codebase appears to special-case poll cancellations or depend on Unexpected being returned from poll operations. The mapping is consistent with accept/connect/send operations and correctly represents io_uring's OS-level poll cancellation semantics.
Summary
Implements timeout support for signal waiting by adding
Signal.timedWait(), which allows waiting for OS signals with a timeout.Changes
timedWaitForIo()insrc/io/base.zig- Generic function for timed I/O operations with timeout supportSignal.timedWait()insrc/signal.zig- Wait for signal with timeout parameter (nanoseconds)ECANCELEDerror in.polloperation to prevent "unexpected errno: 125" warningsImplementation Details
The implementation follows the same pattern as other sync primitives (
ResetEvent.timedWait,Notify.timedWait):timedWaitForReadyWithCallbackfrom the runtime for timeout handlingerror{ Timeout, Canceled }Test Results
All 125 tests pass, including the two new tests:
Signal: timedWait timeout- Verifies timeout behavior when no signal arrivesSignal: timedWait receives signal before timeout- Verifies successful signal receptionFixes #73
Summary by CodeRabbit
New Features
Bug Fixes
Tests