Skip to content

fix(agents): keep sandbox CDP probe deadline on the monotonic clock - #185574

Closed
SunnyShu0925 wants to merge 1 commit into
openclaw:mainfrom
SunnyShu0925:fix/sandbox-browser-cdp-probe-deadline-monotonic
Closed

SunnyShu0925 wants to merge 1 commit into
openclaw:mainfrom
SunnyShu0925:fix/sandbox-browser-cdp-probe-deadline-monotonic

Conversation

@SunnyShu0925

@SunnyShu0925 SunnyShu0925 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Starting a sandbox browser can hang past its configured startup deadline, or fail early on a healthy browser. waitForSandboxCdp seeded its deadline and read its remaining-time checks from Date.now() (wall clock), while the fetch-abort timer and the poll sleep consumed those values through libuv monotonic timers. When the wall clock steps back (NTP correction, manual time change), the while (Date.now() < deadline) guard can never become true, so the probe spins until the hung container is force-removed; when the wall clock steps forward, a healthy request is aborted early and the browser startup is reported as timed out.

  • Problem: the deadline and remaining budget are computed on the wall clock but consumed by monotonic timers, so a wall-clock jump distorts the startup budget in both directions.
  • Solution: seed and read the deadline from performance.now(), so the guard, the abort timer, and the poll sleep share one monotonic domain.
  • What changed: src/agents/sandbox/browser.ts — waitForSandboxCdp now uses performance.now() for the deadline, the loop guard, and the two remaining-time reads. The helper stays private; the regression drives it through the real browser-startup path (onEnsureAttachTarget) against a real node:http peer. The existing "stalled CDP request" test's fake-timer config also fakes performance, so advanceTimersByTimeAsync moves the deadline on the same clock.
  • What did NOT change: the deadline is a process-local closure variable; nothing serializes, persists, or compares it across processes. The browser-registry createdAtMs/lastUsedAtMs timestamps stay wall-clock on purpose; ensureSandboxBrowser and the container lifecycle are untouched.

Why This Change Was Made

The deadline is a purely process-local budget: it is not serialized, persisted, or compared across processes by any Date.now() consumer, so switching to the monotonic clock is the correct, minimal fix (the same pattern already merged for the wall-clock family, e.g. #169908, #155863). Keeping the helper private avoids a test-only production seam: the existing lifecycle fixture already invokes the probe through onEnsureAttachTarget, so the regression extends that path instead of importing the helper.

  • Best fix: yes — the owner function reads one clock end-to-end; no new parser, policy branch, or config surface. A parallel monotonic deadline alongside a wall-clock epoch was not needed; nothing outside the closure consumes the value across a process/DB boundary.
  • Risk: none identified — performance.now() exists on Node 16+, and setTimeout/sleep accept the float it returns.

User Impact

Sandbox browser startup now honors its configured timeout regardless of wall-clock adjustments: a step-back no longer makes the CDP probe spin past the deadline, and a step-forward no longer aborts a healthy probe early. Users see either a timely "browser not reachable" failure or a successful attach, instead of a hung startup or a false timeout.

Evidence

Shipped entry point, real Docker sandbox, real clock step.

openclaw gateway (this checkout, isolated OPENCLAW_STATE_DIR) plus the shipped openclaw agent --agent main -m "hi" turn, with sandbox.mode: "all", backend: "docker", and browser autoStartTimeoutMs: 8000. The scripted model requests the browser tool (open about:blank); the browser plugin attaches to the sandbox bridge, and the bridge's onEnsureAttachTarget calls waitForSandboxCdp against the container's mapped CDP port. The image is the contract-labelled sandbox-browser image (org.openclaw.sandbox-browser.contract=2026-05-12-cdp-relay-auth).

The container's CDP peer answers 500 and logs every request with a CLOCK_MONOTONIC timestamp — that is the peer the probe really polls (each probe request carries the sandbox Authorization header; the plugin's reachability check does not). While the probe waits, the host CLOCK_REALTIME is stepped with sudo date -s by ±60 s, a real NTP-style jump; CLOCK_MONOTONIC/performance.now() is untouched. The step is applied on the first authenticated probe request, i.e. after the budget was seeded on the pre-step clock. The peer is a stub rather than Chromium because this environment has no outbound network to build a real browser image; the probe transport, container lifecycle, and clock are real.

source wall-clock step probe wait (container log, monotonic) outcome
this PR +60 s 8.3 s deadline expires at the 8 s budget
this PR −60 s 8.3 s step-back does not extend the budget
base +60 s 0.2 s premature "not reachable" abort
base −60 s 68.6 s budget stretched by the jump

Raw container log (first authenticated probe request → last), base −60 s vs this PR −60 s:

# base (Date.now), -60s step
[fixture] GET GET /json/version HTTP/1 -> 500 (elapsed=47452ms first=47452ms auth=1)
[fixture] GET GET /json/version HTTP/1 -> 500 (elapsed=116103ms first=47452ms auth=1)   # 68.6s
# this PR (performance.now), -60s step
[fixture] GET GET /json/version HTTP/1 -> 500 (elapsed=54812ms first=54812ms auth=1)
[fixture] GET GET /json/version HTTP/1 -> 500 (elapsed=63084ms first=54812ms auth=1)   # 8.3s

Every run ends in the same production failure — Sandbox browser CDP did not become reachable on 127.0.0.1:<port> within 8000ms. The hung container has been forcefully removed. — the difference is only when it appears. In the base −60 s run the client surfaced a bridge-level error first while the container log shows the probe still waiting 68.6 s: the probe outlived the request.

Regression tests (RED on the base commit, GREEN with the fix).

$ git show HEAD~1:src/agents/sandbox/browser.ts > src/agents/sandbox/browser.ts   # base
$ npx vitest run --config test/vitest/vitest.agents.config.ts src/agents/sandbox/browser.create.test.ts
× keeps sandbox browser attach inside the monotonic deadline across a 60000 ms wall-clock step
  AssertionError: expected 'Sandbox browser CDP did not become re…' to be 'attach target reached before bridge c…'
× keeps sandbox browser attach inside the monotonic deadline across a -60000 ms wall-clock step
 Tests  2 failed | 26 passed (28)

$ git checkout -- src/agents/sandbox/browser.ts                                   # fix
$ npx vitest run --config test/vitest/vitest.agents.config.ts src/agents/sandbox/browser.create.test.ts
 Tests  28 passed (28)

The negative row asserts the deadline still expires after a step-back (no budget stretch); the positive row asserts attach still happens when the browser turns healthy inside the monotonic budget (no premature abort). The sign of the delta is preserved, so the two rows no longer exercise the same rewind.

oxfmt --check and oxlint are clean; the diff has no config, persistence, or new public surface.

@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Maintainer security review required

This external contributor PR changes sensitive security components. A maintainer must review these changes before merging.

Current SHA: 902938fb42fb4669ced5f3367a4ab27f0833b0b2

These sensitive security changes were made:

  • src/agents/sandbox/browser.ts: Controls sandbox containment or tool execution authority. Check filesystem and network isolation, approval scope, and command allowlists.

After reviewing the changes, post a new PR comment containing only approval commands, each on its own line:

/allow-security-sensitive-change

A later push requires a fresh approval comment.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 27, 2026
@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head e2a4c35c0904e424b228a7795731db0ff98292ca, but the PR head is now 902938fb42fb4669ced5f3367a4ab27f0833b0b2. Its old verdict and PR readiness labels are no longer current.

Next step
Run or wait for a fresh ClawSweeper review on the current PR head.

Review history (2 earlier review cycles)
  • reviewed 2026-10-27T06:47:45.468Z sha 4b8b9b3b7e1e5c290234549c2efebd2927a5f470 :: needs real behavior proof before merge. :: [P3] Keep the CDP probe private and test through browser startup | [P3] Exercise a forward jump before the monotonic deadline expires
  • reviewed 2026-10-27T12:37:02.364Z sha e2a4c35c0904e424b228a7795731db0ff98292ca :: blocked before merge. :: none

@SunnyShu0925
SunnyShu0925 force-pushed the fix/sandbox-browser-cdp-probe-deadline-monotonic branch from 4b8b9b3 to e2a4c35 Compare October 27, 2026 12:30
@SunnyShu0925

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review.

@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 27, 2026
@SunnyShu0925
SunnyShu0925 force-pushed the fix/sandbox-browser-cdp-probe-deadline-monotonic branch from e2a4c35 to 5ab1678 Compare October 27, 2026 13:13
@openclaw-barnacle openclaw-barnacle Bot reopened this Oct 27, 2026
waitForSandboxCdp seeded its startup deadline and remaining-time checks
with Date.now() (wall clock) while the abort and poll timers consumed
those values through libuv monotonic timers. A wall-clock step-back made
the while loop's Date.now() < deadline guard unreachable, so the probe
spinned until the container teardown path; a step-forward prematurely
aborted healthy requests. Seed and read the deadline with
performance.now() so every consumer shares one monotonic domain.
@SunnyShu0925
SunnyShu0925 force-pushed the fix/sandbox-browser-cdp-probe-deadline-monotonic branch from 5ab1678 to 902938f Compare October 27, 2026 15:08
@roboclaw-bot roboclaw-bot added the clawtributors PR from a clawtributor label Oct 28, 2026
@clawsweeper clawsweeper Bot removed proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. labels Oct 28, 2026
@SunnyShu0925

Copy link
Copy Markdown
Contributor Author

Closing this PR as superseded by #186556.

Main merged #186556 (2026-10-28), which rewrote waitForSandboxCdp in src/agents/sandbox/browser.ts to seed and read the entire deadline chain with monotonicNow() (the project's standard monotonic clock), including the abort/poll request timeout and remaining-time checks — no wall-clock Date.now() remains in the deadline path. This resolves the exact same wall-clock-step deadline bug this PR fixed with performance.now().

Confirmed on current main (e25c026eb75): waitForSandboxCdp seed/read all use monotonicNow(); only a non-deadline business timestamp (line 278) still uses Date.now(). The PR branch would also conflict with main's additional assertCurrent refactor and would regress main's correct implementation, so keeping it open adds no value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawtributors PR from a clawtributor P2 Normal backlog priority with limited blast radius. security-review-required security-sensitive-changed size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants