Repository navigation
Wait for the timer to be canceled - #120
Conversation
WalkthroughA busy-wait loop was added to the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ 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: 2
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/core/timeout.zig(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: lalinsky
Repo: lalinsky/zio PR: 48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.
Learnt from: lalinsky
Repo: lalinsky/zio PR: 47
File: src/sync/Mutex.zig:237-237
Timestamp: 2025-10-15T19:38:44.659Z
Learning: In the `unlock` method of `src/sync/Mutex.zig`, the `runtime` parameter is intentionally retained (marked with `_ = runtime;`) for API consistency and future-proofing, even though it's not currently used in the implementation.
📚 Learning: 2025-10-16T14:33:19.718Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.
Applied to files:
src/core/timeout.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: test (windows-latest)
| // TODO: HUGE HACK: this needs proper waiting | ||
| // If we are the active timeout, we need to wait until it's cleared!!! | ||
| while (task.timer_cancel_c.state() != .dead) { | ||
| executor.yield(.ready, .ready, .no_cancel); | ||
| } |
There was a problem hiding this comment.
Replace busy-wait with proper synchronization primitive.
This busy-wait loop solves the race condition but creates new problems:
- Performance overhead: Even with yields, spinning wastes CPU cycles and adds latency to
clear()calls. - No timeout: If
timer_cancel_cnever reaches.deadstate (due to a bug or deadlock), this hangs forever. - Potential deadlock: If timer cancellation completion depends on this task making progress through other code paths, you have a deadlock.
Consider using a completion/future mechanism where the timer cancellation operation can signal completion that clear() awaits properly, or add a counted reference with proper synchronization.
Do you want me to search the codebase for similar synchronization patterns or help design a proper completion mechanism?
🤖 Prompt for AI Agents
In src/core/timeout.zig around lines 29 to 33, the current busy-wait loop
spinning on task.timer_cancel_c.state() must be replaced with a proper
synchronization primitive: add a completion/event/future that the timer
cancellation path will signal when it reaches the .dead state, and have clear()
await that completion instead of looping; include a configurable timeout and
return an error if the wait times out to avoid indefinite hang, and ensure the
completion is signalled under the same lock/ownership rules as the timer to
prevent races/deadlocks (or use a counted reference + atomic/state transition
with a condition variable or executor-supported await API if available). Ensure
the cancel path signals the completion on all code paths (success and error) and
that clear() handles cancellation/interrupts and cleans up the waiter to avoid
leaks.
|
|
||
| // TODO: HUGE HACK: this needs proper waiting | ||
| // If we are the active timeout, we need to wait until it's cleared!!! | ||
| while (task.timer_cancel_c.state() != .dead) { |
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major
Add defensive iteration limit.
Even as a temporary hack, this unbounded loop is dangerous. Add a maximum iteration count and fail fast if exceeded—it'll catch bugs in testing rather than hanging mysteriously in production.
+ // TODO: HUGE HACK: this needs proper waiting
+ // If we are the active timeout, we need to wait until it's cleared!!!
+ var spin_count: usize = 0;
+ const max_spins: usize = 10000;
while (task.timer_cancel_c.state() != .dead) {
+ spin_count += 1;
+ std.debug.assert(spin_count < max_spins); // Fail fast if cancellation hangs
executor.yield(.ready, .ready, .no_cancel);
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| while (task.timer_cancel_c.state() != .dead) { | |
| // TODO: HUGE HACK: this needs proper waiting | |
| // If we are the active timeout, we need to wait until it's cleared!!! | |
| var spin_count: usize = 0; | |
| const max_spins: usize = 10000; | |
| while (task.timer_cancel_c.state() != .dead) { | |
| spin_count += 1; | |
| std.debug.assert(spin_count < max_spins); // Fail fast if cancellation hangs | |
| executor.yield(.ready, .ready, .no_cancel); | |
| } |
🤖 Prompt for AI Agents
In src/core/timeout.zig around line 31, the while loop "while
(task.timer_cancel_c.state() != .dead)" is unbounded; add a defensive maximum
iteration count (e.g., an integer max_iters constant) and increment a counter
each loop iteration, and if the counter exceeds max_iters return an error (or
call unreachable/asserterror/log and fail fast) to avoid hanging; update
function signature/propagate the error as needed and include a short log message
explaining the exceeded iterations for debugging.
If we are calling
timeout.clear(), we need to wait until the backing timer is canceled. It's because if we exit sooner, the task might actually exit, and cause memory corruption.This is a WIP hack, needs proper waiting.