Repository navigation
AutoCancel: arm against a chosen clock with setClock - #717
Conversation
## Problem `AutoCancel` always armed on `.awake`. Every layer beneath it already understood clocks: `ev.Timer` carries a `clock` field with an `initClock` constructor, `Loop` keeps one timer heap per wall clock and resolves durations against the matching `now` in `armTimer`, and `Timeout.fromStd` is deliberately clockless because "the clock itself travels separately on the `ev.Timer`". `Clock.fromStdTimeout` exists to carry that other half. `AutoCancel.set` never assigned `self.timer.clock`, so it kept the `.awake` default from `.init`, and those two `fromStd` helpers had no caller able to use both halves. A `std.Io`-facing wrapper therefore cannot pass an absolute deadline through. dusty exposes `Request.setTimeout(std.Io.Timeout)`, so a handler can hand it a `.real` deadline; on the zio path it has to flatten that with `toDurationFromNow` into a relative monotonic duration measured at arm time, and a settable clock stops being settable the moment it is armed. ## Change `setClock(timeout, clock)` assigns `timer.clock` alongside the `userdata` and `callback` that `set` already writes. `set(timeout)` becomes `setClock(timeout, .awake)`, so existing behavior is unchanged. The clock is written after the `clear()` that opens the function, and only because of it: `disarmTimer` removes from `timers[clockIndex( timer.clock)]`, so moving the clock under an armed timer would take it out of the wrong heap. The `clear` is already there for the cross-executor re-arm and the in-flight-callback race; this relies on it. Also exports `zio.Clock`, which was missing next to `Duration`, `Timestamp` and `Timeout` and was reachable only as `zio.time.Clock`. ## Tests The absolute-deadline test is the one that needs the feature: a `.real` timestamp is ~1.7e18 ns against an awake epoch of ~1e13 ns, so on the monotonic heap it reads as far-future and never fires. Swapping its `.real` for `.awake` fails the test. The clock-switch test covers the ordering constraint above by re-arming a still-live real-clock timer onto `.awake`. Closes #716
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 4 included reviews per hour; 2 remain after this review. 📝 WalkthroughWalkthrough
ChangesAutoCancel clock-aware arming
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The change adds explicit clock selection for AutoCancel while preserving existing behavior by default, and includes coverage for the new timer paths. No actionable merge-blocking risk remains beyond normal checks and review. Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The PR implements Resolution Include the dusty-side integration that calls Full details: Docstring CoverageExplanation No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (2 skipped: 2 unsupported.)
✨ 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 |
Closes #716.
Problem
AutoCancelalways armed on.awake. Everything beneath it already understood clocks:ev.Timercarries aclockfield and aninitClockconstructor (src/ev/completion.zig:540)Loopkeeps one timer heap per wall clock and resolves durations against the matchingnow(armTimer,src/ev/loop.zig:417)Timeout.fromStdis deliberately clockless, and says so: "The clock itself travels separately on theev.Timer" (src/time.zig)Clock.fromStdTimeoutexists to carry that other halfAutoCancel.setnever assignedself.timer.clock, so it kept the.awakedefault from.init— and the twofromStdhelpers had no caller able to use both halves.The practical consequence is that a
std.Io-facing wrapper cannot pass an absolute deadline through. dusty exposesRequest.setTimeout(std.Io.Timeout), so a handler can hand it a.realdeadline; on the zio path it has to flatten that withtoDurationFromNowinto a relative monotonic duration measured at arm time. A settable clock stops being settable the moment it is armed, and the same call behaves differently depending on which runtime the application linked.Change
It assigns
timer.clockalongside theuserdataandcallbackthatsetalready writes.set(timeout)becomessetClock(timeout, .awake), so existing behaviour is untouched.The clock is written after the
clear()that opens the function, and only because of it:disarmTimerremoves fromtimers[clockIndex(timer.clock)], so moving the clock under an armed timer would take it out of the wrong heap. Thatclearis already there for the cross-executor re-arm and the in-flight-callback race; this just depends on it, with a comment saying so.Also exports
zio.Clock, which was missing next toDuration,TimestampandTimeoutand was reachable only aszio.time.Clock.On the dusty side this collapses the whole conversion to a pass-through:
Tests
Six added;
./check.sh --fullis green (674 unit tests, examples build, smoke test).The absolute-deadline test is the one that actually needs the feature — a
.realtimestamp is ~1.7e18 ns against an awake epoch of ~1e13 ns, so on the monotonic heap it reads as far-future and never fires. I checked it discriminates rather than passing for free: swapping its.realfor.awakefails the test.The clock-switch test covers the ordering constraint above, re-arming a still-live real-clock timer onto
.awake. The duration-on-.realtest is weaker by nature (10ms is 10ms on either clock) and mainly shows the real heap gets driven.Not included
withTimeoutClockcompanion — worth deciding separately.docs/changelog.md, which is curated at release time.