Skip to content

Browser service: a Deadline whose timer fired reads as spent, so a paste answered timeout never runs - #400

Merged
SSFSKIM merged 2 commits into
mainfrom
fix/deadline-early-timer
Nov 17, 2026
Merged

SSFSKIM merged 2 commits into
mainfrom
fix/deadline-early-timer

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 17, 2026

Copy link
Copy Markdown
Owner

The bug

In src/browser-service, Deadline.race armed setTimeout(reject, at - Date.now()), while check(), expired(), remaining() and timeoutMs() compared Date.now() with at. Node can fire that timer while Date.now() still reads up to about 1 ms under at. A probe under load saw it in 4 of 500 new Deadline(20) races (#171 M5 task 92).

So paste.mjs's exclusively could tell its caller "timeout", and the queued paste, reached in that gap, would still pass its own deadline.check() and run. A retry of the timed-out command then pastes twice.

The fix (ruling 781)

A deadline is now spent once a timer armed on it fires, whatever the clock reads.

  • Deadline.whenSpent(onSpent) arms the timer at the deadline's end and marks the deadline spent (a private flag) before calling back. It returns a disarm function.
  • race arms its bound through it.
  • expired() and remaining() answer from the mark first, so check(), timeoutMs() and child() agree with them. Nothing re-reads the clock to decide otherwise.

Two other users armed a timer of their own on a deadline's span and had the same contradiction. Both now arm through whenSpent:

  • Backend.request's abandon timer (backend.mjs). A request abandoned early left its deadline unspent, so the next request on that deadline was still sent. The callers' "was the deadline spent?" decisions also read the clock: cdp.mjs failedOnStarterBudget, and the timeout wording in chooser, actions, downloads and waits.
  • Downloads.waitForDownload's wait timer (downloads.mjs). It was a separate setTimeout(timeoutMs) beside a Deadline(timeoutMs). On an early fire the wait answered timeout, while the release's retry loop (releasePaused, !budget.expired()) still passed and let the download through.

Every Deadline user in src/browser-service, checked

Fixed

user test
Deadline.race, which paste's exclusively relies on 1, 2
Backend.request's abandon timer 3
Downloads.waitForDownload's wait timer 4

Covered by the fix. Each of these wakes through race or the request timer on the same deadline, then reads expired() or check(), so each now agrees with the timer:

  • paste.mjs: the queued operation's check(), and typeText's race(allowsRichText).
  • cdp.mjs:
    • withinDeadline (attachment, prepareDispatch, frame-session init), with checkDeadline and callTimeout before each dispatch;
    • ensureAttachedTab's joined retry;
    • failedOnStarterBudget;
    • tryInitializeAttachedFrameSession.
  • clipboard-bridge.mjs ensure: its race, the joined retry, and the install's later steps.
  • downloads.mjs: bounded, and the releasePaused and reconfigureOrRecover loops.
  • The timeout wording after a deadline error (deadline.expired() && isDeadlineError(error)) in chooser.mjs, actions.mjs (download media), downloads.mjs, waits.mjs and evaluate.mjs. After an early fire these used to answer the raw sentence; now they name the command's own timeout.
  • cursor.mjs moveMouse, whose race's error is swallowed.
  • webmcp.mjs tabNotification.

Checked, and they don't share the bug. None of them answers timeout and then has later work on the same budget contradict it:

  • waits.mjs waitForNetworkIdle's wake timer. On an early wake it re-reads expired(), re-arms for the 0 to 1 ms left, and gives the same answer a moment later.
  • Event waits that are given deadline.remaining() as a number: cdp.waitForEvent (in waits.mjs and navigation.mjs) and waitForPageLoadEvent. Their timeout reaches the caller only after the command's own call returns. That call's dispatch check reads the same clock either way, so the outcome is the one an exact timer would give. The page-load wait resolves rather than rejects.
  • Poll loops that sleep min(POLL, remaining()) and then re-read the clock: selectors.mjs poll, evaluate.mjs evaluateOnSelector, and the downloads retries on the service's own budgets. An early wake gives one more attempt, whose result is the answer. wheel-scroll.mjs sleeps a plain POLL_MS.
  • dialogs.mjs answerDialog. The deadline only bounds the call, and the close wait has a timer of its own.
  • Point checks with no timer of their own: snapshot, injected, input, reads, actions' within and budgetOf, selectors' attemptBudget, and visible-dom's child-frame budgets derived from deadline.at.

No Deadline user exists outside src/browser-service.

Tests

test/browser-service/support/early-timer.mjs (new) forces the early timer deterministically. setTimeout uses node:test's mocked timers and Date.now() is the test's own. elapse(ms) runs the timers due within ms while the clock moves only ms - 1, so each timer fires 1 ms short of its end. The existing controlled-clock.mjs moves Date and the timers together, so its timers are never early.

  1. paste.test.mjs: a paste answered timeout by its deadline's timer never runs.
  2. errors-budgets.test.mjs: after an early fire the Deadline reads spent through expired, remaining, check, timeoutMs, race and child. whenSpent marks it the same way, and a disarmed timer never fires.
  3. backend.test.mjs: a request abandoned on its deadline's timer leaves the deadline spent, and the next request on it is never sent.
  4. downloads.test.mjs: a download wait answered timeout by its timer lets no release of the download through after it.

RED on the unfixed source: 0 of 4 pass.

  • paste: the queued paste never ran. The result was {calls: 2, evaluated: 1} where {calls: 0, evaluated: 0} was expected: the timeout was answered and the queued paste then ran.
  • budgets: expired() read false, expected true.
  • backend: the timeout answered is the deadline's, false !== true.
  • downloads: no release of the download after the wait timed out, 2 !== 1.

With only the race change (the backend and downloads changes set back), paste and budgets pass and backend and downloads still fail. So each of those two needs its own change.

GREEN: 4 of 4. The whole browser-service suite (node --test "test/browser-service/*.test.mjs") has 432 tests: 430 pass, 0 fail and 2 skipped. The skips are the two drift checks that need npm ci.

The existing real-clock paste deadline test is left as it is. With the fix, its queued paste is refused on the real clock as well.

Plugin 0.28.7.

ci-gate on e0f0669: the full run 44122424068 was RED 1/3: plugin and parity-browser passed, and test failed once. That failure is outside browser-service: test/linux-engine-diff/run-tmp.test.mjs:113 read '' for the after-the-run engine's TMPDIR. It is the stand-in write race #171 M5 task 92 found and fixed on m5-testdebt (the stand-ins now write by rename), which is not yet on main. A test-gate rerun, 44124905434, was GREEN.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

…ste answered timeout never runs

Node can fire a timer while Date.now() still reads up to about 1 ms under
the end it was armed for (4 of 500 20 ms Deadline races under load, task
92's probe). Deadline.race then rejected with timeout while check() and
expired() still compared the clock with the end and let work through:
paste.mjs's exclusively answered its caller timeout, and the queued paste,
reached in that gap, passed its own check and ran. A retry of that
command pastes twice.

Per ruling 781 the deadline is now spent once a timer armed on it fires,
whatever the clock reads: whenSpent(onSpent) arms that timer and marks
the deadline spent before calling back, race arms its bound through it,
and expired(), remaining(), check(), timeoutMs() and child() all answer
from the mark first. Two other timers armed on a deadline's span answered
timeout while work on the same budget went on, and now arm through
whenSpent too:
- Backend.request's abandon timer: a request abandoned on it left the
  deadline unspent, so the next request on it was still sent, and the
  callers' "is the deadline spent?" decisions read the clock.
- Downloads.waitForDownload's wait timer: the wait answered timeout while
  the download's release retried on the same deadline and let it through.

The tests force the early timer deterministically (support/early-timer.mjs:
mocked setTimeout, Date.now() held 1 ms behind it): a paste, the Deadline
itself, a backend request and a download wait.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T16:39:48.637790Z e0f0669 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@SSFSKIM
SSFSKIM merged commit 136183a into main Nov 17, 2026
5 checks passed
SSFSKIM added a commit that referenced this pull request Nov 17, 2026
… cua 0.28.7)

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
SSFSKIM added a commit that referenced this pull request Nov 17, 2026
…367)

Sminos-Session: cursor235#b93055dd (cua on macbook)
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.

1 participant