Skip to content

AutoCancel: arm against a chosen clock with setClock - #717

Merged
lalinsky merged 1 commit into
mainfrom
feat/autocancel-setclock
Aug 27, 2026
Merged

lalinsky merged 1 commit into
mainfrom
feat/autocancel-setclock

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Closes #716.

Problem

AutoCancel always armed on .awake. Everything beneath it already understood clocks:

  • ev.Timer carries a clock field and an initClock constructor (src/ev/completion.zig:540)
  • Loop keeps one timer heap per wall clock and resolves durations against the matching now (armTimer, src/ev/loop.zig:417)
  • Timeout.fromStd is deliberately clockless, and says so: "The clock itself travels separately on the ev.Timer" (src/time.zig)
  • Clock.fromStdTimeout exists to carry that other half

AutoCancel.set never assigned self.timer.clock, so it kept the .awake default from .init — and the two fromStd helpers 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 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. 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

pub fn setClock(self: *AutoCancel, timeout: Timeout, clock: Clock) void

It assigns timer.clock alongside the userdata and callback that set already writes. set(timeout) becomes setClock(timeout, .awake), so existing behaviour is untouched.

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. That clear is 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 to Duration, Timestamp and Timeout and was reachable only as zio.time.Clock.

On the dusty side this collapses the whole conversion to a pass-through:

self.inner.setClock(.fromStd(timeout), .fromStdTimeout(timeout));

Tests

Six added; ./check.sh --full is green (674 unit tests, examples build, smoke test).

The absolute-deadline test is the one that actually 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. I checked it discriminates rather than passing for free: swapping its .real for .awake fails the test.

The clock-switch test covers the ordering constraint above, re-arming a still-live real-clock timer onto .awake. The duration-on-.real test is weaker by nature (10ms is 10ms on either clock) and mainly shows the real heap gets driven.

Not included

  • A withTimeoutClock companion — worth deciding separately.
  • docs/changelog.md, which is curated at release time.

## 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
@coderabbitai

coderabbitai Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3ae63721-4646-4e0b-ab97-1dbb1e3231d8

📥 Commits

Reviewing files that changed from the base of the PR and between 3786083 and 09f0c26.

📒 Files selected for processing (2)
  • src/autocancel.zig
  • src/zio.zig

Included review availability: Your plan provides up to 4 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

AutoCancel now accepts a caller-selected Clock through setClock. Existing set calls use .awake. Tests cover real-clock deadlines, re-arming, clearing, and .none. The module root re-exports Clock.

Changes

AutoCancel clock-aware arming

Layer / File(s) Summary
Clock-aware timer arming
src/autocancel.zig, src/zio.zig
AutoCancel.setClock assigns a caller-selected clock while the timer is disarmed. set delegates to setClock(timeout, .awake). Clock is re-exported from the module root.
Clock arming validation
src/autocancel.zig
Tests cover monotonic and real-clock duration arming, absolute deadlines, re-arming across clocks, clearing, and .none.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 09f0c

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

A timer wakes on clocks anew

.awake keeps the old path true
Real deadlines hold their place
Re-arming shifts the clocking space
Clear leaves no event behind
.none rests, with nothing timed

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements AutoCancel.setClock, preserves .awake behavior in set, assigns the clock after clear(), exports zio.Clock, and adds relevant tests [#716]. However, the provided file summar… Include the dusty-side integration that calls self.inner.setClock(.fromStd(timeout), .fromStdTimeout(timeout)), or provide evidence that this change exists in the submitted changeset. Add coverage for absolute deadlines and preservation o…
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: adding AutoCancel.setClock to arm timers against a selected clock.
Description check ✅ Passed The description directly explains the setClock API, clock assignment ordering, zio.Clock export, tests, and the related timeout motivation.
Out of Scope Changes check ✅ Passed The changed API, clock export, implementation ordering, and tests all relate directly to the linked issue. No unrelated code changes are identified.
Docstring Coverage ✅ Passed 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…
Full details: Linked Issues check

Explanation

The PR implements AutoCancel.setClock, preserves .awake behavior in set, assigns the clock after clear(), exports zio.Clock, and adds relevant tests [#716]. However, the provided file summary does not show the required dusty integration that passes both Timeout.fromStd(timeout) and Clock.fromStdTimeout(timeout) without duration conversion.

Resolution

Include the dusty-side integration that calls self.inner.setClock(.fromStd(timeout), .fromStdTimeout(timeout)), or provide evidence that this change exists in the submitted changeset. Add coverage for absolute deadlines and preservation of clock-specific behavior without millisecond truncation [#716].

Full details: Docstring Coverage

Explanation

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.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/autocancel-setclock

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lalinsky
lalinsky merged commit 5de2c44 into main Aug 27, 2026
31 checks passed
@lalinsky
lalinsky deleted the feat/autocancel-setclock branch August 27, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoCancel: add setClock(timeout, clock) to arm against a non-monotonic clock

1 participant