Skip to content

fix(harness): a deadline timer aborts a turn when its lease would lapse, not the next renew tick - #835

Merged
webchang merged 1 commit into
rossoctl:mainfrom
webchang:pr/i801-lease-deadline-timer
Nov 16, 2026
Merged

webchang merged 1 commit into
rossoctl:mainfrom
webchang:pr/i801-lease-deadline-timer

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #801

What changed and why

  • harness/src/turn-registry.ts: ActiveTurn judged the lease deadline (leaseMs - renewMs after the last renew that landed) only on renew ticks. With the default timings (30 s lease, 10 s renew), and the test's (600 ms / 200 ms), the deadline is a whole number of ticks, so a tick lands exactly on it. If the clock reads even 1 ms short on that tick, the check passes, and the next check comes a full tick later, when the lease has already lapsed in Redis. That matches the CI failures (expected 600 to be less than 600, 601). It is also a real window where the owner of a lapsed lease keeps running the turn, which §5.1 rules out.
  • The turn now has a deadline timer of its own. It is armed in the constructor from the clock read before BEGIN, and each renew that lands (non-null) rearms it from the clock read before that renew. It fires lease_lost whether a renew is in flight, failing or hung, so the margin before the lapse is a full renewMs by construction instead of depending on where a tick lands.
  • The timer is cleared on end(), stop() (registry close) and any abort, so it can't leak or fire late. If a renew lands after the timer fired, it finds the turn already aborted: it doesn't abort again, the first reason (lease_lost) stays, and it doesn't rearm. The tick-time checkLeaseDeadline is gone: the timer covers both cases it handled (a skipped tick while a renew hangs, and a failed renew).
  • Nothing else changes: renews still never overlap, a null renew still aborts lease_lost at once, and cancelRequested and the unwatched check work as before.

Tests

  • New describe('lease deadline') in harness/test/turn-registry.test.ts: an ActiveTurn over a stub registry with fake timers, so the abort instant is exact and doesn't depend on Redis or the scheduler:
    • a hung renew with the default timings, and a clock that reads 1 ms short after the last good renew: the abort lands at the deadline (30 s), not at the lapse;
    • a deadline that falls between ticks (25 s lease, 10 s renew): the abort lands at the deadline (25 s), not at the next tick;
    • a renew that lands moves the deadline (ten good renews, then a hang);
    • a renew that settles after the deadline fired (and carries a cancel) doesn't abort again, keeps lease_lost and leaves no rearmed timer;
    • a renew that lands 1 ms before the deadline rearms it;
    • no timer is left after end() or stop(), and none fires after an abort for another reason.
  • Before the fix (source stashed), the first two fail and the other four pass:
    • expected [ 40000 ] to deeply equal [ 30000 ]: the abort landed when the lease lapsed;
    • expected [ 30000 ] to deeply equal [ 25000 ]: the abort landed at the next tick.
  • With the fix, all 37 tests in turn-registry.test.ts pass.
  • harness, in a clean env: pnpm test passed 70 files (2 skipped), 878 tests (5 skipped), and pnpm typecheck is clean.
  • The two Redis-backed "before the lease can lapse" tests, run in a loop on a loaded machine (load average about 50 on 16 cores):
    • before the fix: 0 of 50 failed (5 runs in parallel), and 0 of 100 (16 in parallel);
    • after the fix: 0 of 50, and 0 of 100.
    • So the CI flake didn't reproduce locally. The fake-timer tests above pin the root cause instead. With the fix, these two tests have the renewMs headroom (200 ms) the design intends, and their comment now says so.

Every commit is signed off (DCO).

Assisted-By: Claude Code

The turn registry judged the lease deadline (leaseMs - renewMs after the
last renew that landed) only on renew ticks. With the default timings
the deadline is a whole number of ticks, so a tick lands on it exactly;
a clock read there even 1 ms short let it pass, and the next check came
one full tick later, when the lease had already lapsed in Redis. That is
the intermittent "expected 600 to be less than 600" in the "renew never
settles" test, and a window in which a lapsed lease's owner still runs.

ActiveTurn now arms a deadline timer from the clock read before BEGIN,
and every renew that lands rearms it from the clock read before that
renew. It fires lease_lost whether a renew is in flight, failing, or
hung. It is cleared on end, stop and any abort; a renew that lands after
it fired finds the turn aborted and neither aborts again nor rearms.

New fake-timer tests under 'lease deadline' pin the abort instant: on
main a hung renew aborts at the lapse (40 s, not 30 s) when the clock
reads 1 ms short, and at the next tick when the deadline falls between
ticks.

Closes rossoctl#801

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 16, 2026 20:39
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 16, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Replaces the tick-time checkLeaseDeadline in ActiveTurn with a dedicated setTimeout deadline armed from the pre-BEGIN clock and rearmed by every renew that lands, so a turn aborts lease_lost exactly leaseMs - renewMs after its last good renew instead of up to one tick late (#801). Verified the timer is cleared on end(), stop() and every abort path, that a late-landing renew cannot re-abort or rearm (both abort() and armDeadline() guard on signal.aborted), and that the failed-renew and hung-renew cases are both still covered by the timer. The new fake-timer tests pin the abort instant and the no-leak behaviour; no blocking issues found.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (harness/src/turn-registry.ts), Tests (harness/test/turn-registry.test.ts), Docs (design spec §5.1 still accurate)
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (all checks green)

@webchang
webchang merged commit 4c12824 into rossoctl:main Nov 16, 2026
17 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 2026
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
Takes upstream main at 1a7f41e (19 merges since 032c8f6): the sandbox
idle lifetime across docs, control plane, clients and deploy (rossoctl#820, rossoctl#837,
rossoctl#842, rossoctl#843), grant redemption (rossoctl#822), the lease-check log line (rossoctl#824),
egress fixes (rossoctl#829, rossoctl#834), the mock wait (rossoctl#828), the deadline timer
(rossoctl#835), the create rollback (rossoctl#832), multi refusals and the rossoctl#363 leftovers
(rossoctl#833, rossoctl#838), the identity near-miss check (rossoctl#827), mocactl login errors
(rossoctl#836), the ghostunnel probes (rossoctl#830), refusal-burst deferral (rossoctl#831), a
sandbox-worker change (rossoctl#841) and the v0.11 doc truth (rossoctl#821).

Conflicts (one):

- deploy/sbx/README.md, the "Optional inputs" table. Upstream (rossoctl#843)
  rewrote the SBX_LIFETIME_DAYS row (the default idle lifetime, at most
  SBX_MAX_LIFETIME_DAYS) and added the SBX_MAX_LIFETIME_DAYS and
  SBX_LOWER_WORKSPACE_IDLE rows, which widened the table; the fork had
  added an SBX_GATE_RECORD_SCHEMA row at its end. Resolved by taking
  upstream's table whole and appending the fork's SBX_GATE_RECORD_SCHEMA
  row after SBX_PROFILE, then re-padding the table with prettier. The
  fork's env-file example line for SBX_GATE_RECORD_SCHEMA merged cleanly.

Merged without markers, checked: setup.sh keeps the fork's
SBX_GATE_RECORD_SCHEMA validation and its sbx-settings write (so it stays
in the settings hash) beside upstream's SH_SANDBOX_MAX_LIFETIME_SECONDS;
both ocp-sbx and ocp-combined patch-control-plane.yaml keep the optional
SH_GATE_RECORD_SCHEMA from sbx-settings beside the new optional
SH_SANDBOX_MAX_LIFETIME_SECONDS; setup.test.sh keeps the fork's cases
beside upstream's. The fork's RUNBOOK, README and template-onboarding
docs say nothing about sandbox lifetime, refusal bursts, login errors or
the create rollback, so none needed a correction; GUIDE.md is unchanged
and GUIDE.pdf is not regenerated.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

harness: turn-registry's 'renew never settles' test fails intermittently at the lease boundary (abort lands at leaseMs)

3 participants