Skip to content

fix(cli): leave CPU wait out of the viewer bridge readiness budget - #309

Merged
ElbertePlinio merged 7 commits into
mainfrom
fix/301-306-viewer-bridge-port-tests
Oct 8, 2026
Merged

ElbertePlinio merged 7 commits into
mainfrom
fix/301-306-viewer-bridge-port-tests

Conversation

@ElbertePlinio

@ElbertePlinio ElbertePlinio commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

The viewer bridge readiness bound no longer fails on a busy host, and the tests that auto-allocate emulator ports stop taking each other's ports.

#301: the bridge already reports readiness by writing bridge.json. Under load, 85 to 87% of its startup time was spent waiting for a CPU, mostly while node loaded the CLI bundle. Its own CPU time stayed under 1 s. The 8 s readiness budget now counts elapsed time minus the bridge's CPU wait, read from /proc/<pid>/schedstat, with a wall-time cap of 64 s. A hung bridge still fails within 8 s on an idle host, and a bridge that exits still fails at once. The error now says how long the bridge waited, how much of it was CPU wait, and where its log is. Where schedstat is missing, the budget falls back to wall time as before. The viewer mode test timeout went from 70 s to 90 s, because it starts nine processes. Bridge startup holds the session VNC lock, so a live holder is now waited on for up to 90 s instead of 10 s, and the bridge cap is tied to that bound. MCP session_destroy passes the request's cancellation signal into the lock wait and into each session's teardown, so a cancelled request destroys nothing that had not started.

#306: the read-only shared start in emulator.test.ts now passes the test port range. The CLI Android lifecycle tests hold a host-wide auto-floor test port lock, and still prove allocation from the 5556 floor. Every other test that allocates from 5556 holds the same lock: the CLI desktop-plus-Android failure test, the MCP android_start test (#308), and the security no-sudo test.

Closes #301
Closes #306
Closes #308

Requirement Verified by Scenario Result Evidence
#301 decide the bound under load Measurement 16 bridge starts under 16 busy loops on 2 CPUs; schedstat 2.9 to 7.97 s wall, 85 to 87% CPU wait, under 1 s own CPU ~/Artifacts/pickforge-batch4-2026-10-08/worker-Y-report.md
#301 budget excludes CPU wait, hang still fails Automated test, experiment Hang case checks the new message; budget cut to 2 s under load as an experiment Pass; viewer test passed 3 of 3 with 3 to 8 s wall waits viewer-bridge.test.ts, worker report
#301 repeat runs under load Repeat runs Viewer mode test under 16 busy loops 6 of 6 passed, 31.9 to 49.1 s against 90 s Worker report
#306 private port or lock for the CLI test Reproduction 5556 held by another process origin/main failed (emulator-5558); fixed passed Worker report
#306 shared start uses the test range Repeat runs Two CLI lifecycle runs plus one emulator.test.ts run at once, 8 rounds 24 of 24 passed Worker report
Lock wait covers permitted startup (review round 1) Automated test Fake bridge ready after 11 s, concurrent lock caller Waits past 10 s and sees the bridge; failed at 10.1 s before viewer-bridge.test.ts
Cancelled MCP destroy destroys nothing (review rounds 2 and 3) Automated test Held lock; single session and all=true with an Android session; request aborted after 100 ms Sessions and emulator stay alive; later destroy succeeds; fails without the fix session-destroy-cancel.test.ts, viewer-state.test.ts
#306 and #308 every 5556 prober locked (review round 1) Held-lock experiment, concurrent runs auto-floor held 20 s; CLI, MCP and security suites concurrently 3 rounds Tests wait (23 to 25 s) instead of running through (4 to 8 s); all rounds pass Worker report
Repo checks lint, typecheck, build, PickCheck, coverage Head 44e1601 All pass; 2986 tests, thresholds hold ~/Artifacts/pickforge-batch4-2026-10-08/Y-cov-r4.log

QA device pass: skipped. The user-facing change is a timeout and an error message that only appear under heavy load. Driving the real viewer would open a browser on the live desktop, and the commands test already runs the real bridge with a fake browser.

Under 16 busy loops on 2 pinned CPUs, the viewer bridge reported ready
after up to 8.0 s, so the fixed 8 s bound sometimes expired. Timing probes
showed that about 93% of the wait is the bridge loading the CLI before its
own code runs: about 0.4 s of CPU on an idle host. /proc/<pid>/schedstat
showed that 85% to 87% of the loaded wall time was run-queue wait; the
bridge's own time stayed under 1 s.

The bridge already reports readiness through bridge.json, so the parent
keeps that signal. The 8 s budget now counts only time the bridge was not
waiting for a CPU. A hung bridge still fails within 8 s on an idle host,
and a bridge that exits still fails at once. A wall cap of 8 times the
budget ends a bridge that never gets a CPU. The timeout error now says how
long it waited, how much of that was CPU wait, and where the bridge log is.

With the budget cut to 2 s as an experiment, the viewer mode test passed
3 of 3 runs under the same load, while the wall waits were 3 s to 8 s.

The viewer mode test runs seven CLI calls and two bridge starts, so its
timeout now counts nine spawns (90 s). Under load it took 32 s to 49 s.

Closes #301
…orts

The CLI lifecycle test starts an emulator without --port and expected
emulator-5556. The read-only shared start in emulator.test.ts also had no
port range, so it probed 5556 too. When both probes ran at once, or a real
emulator held 5556, the allocator moved to 5558 and the CLI test failed.

The shared start now uses the emulator test's private port window. The
CLI lifecycle tests hold a host-wide "auto-floor" test port lock, so a
concurrent run of the same file cannot probe the floor at the same time.
The CLI test still proves allocation from the 5556 floor: it expects the
first free console port pair from the floor, which is 5556 unless a real
emulator holds it.

With 5556 held by another process, the old test failed with emulator-5558
and the new test passed. Two CLI lifecycle runs and an emulator.test.ts
run, started together 8 times, passed 24 of 24.

Closes #306
…ge startup

Viewer bridge startup holds the session VNC lock until the bridge reports
ready. Since #301 that can take up to 64 s on a busy host, but other callers
of the lock gave up after 10 s. A concurrent viewer start, session destroy
or takeover could fail while a permitted startup was still running.

The lock wait for a live holder is now 90 s. A dead holder is still detected
by its pid and replaced at once, so stale-lock recovery is unchanged. The
bridge wall cap is now also bounded by the lock wait minus a 20 s margin for
the probe, the old bridge stop and the cleanup, so the two bounds cannot
drift apart. At the defaults the cap stays 64 s.

Releasing the lock during the readiness wait is not safe. The bridge is not
recorded yet, so a concurrent start would launch a second bridge and rotate
its token, and a destroy would not see the bridge to stop it.

A new test holds a startup for 11 s and checks that a concurrent lock caller
waits for it and sees the recorded bridge. Before this change it failed at
10.1 s with "Timed out waiting to ensure VNC".
The CLI desktop+android failure test, the MCP android_start tests and the
security session_create android test start an emulator without a port, so
they probe from the 5556 floor. They did not hold the "auto-floor" test port
lock, so they could still take the floor while the CLI lifecycle tests
expected it.

Each of them now holds the lock in its describe. The CLI test gets its own
nested describe so the rest of the desktop tests do not wait.

With the lock held by another process for 20 s, the three tests finished at
4 s to 8 s before this change. After it they waited and finished at 23 s to
25 s. Without a holder they take 1 s to 4 s.

Closes #308
The 90 s wait for a live session VNC lock holder outlasts the MCP client's
60 s request deadline. A cancelled session_destroy kept waiting and tore the
session down after the lock was released.

withSessionVncLock now takes an optional AbortSignal. It stops waiting when
aborted and checks the signal again after it gets the lock, before the
operation runs. Desktop and browser teardown pass it through, and MCP
session_destroy passes the request signal. CLI callers are unchanged.
@ElbertePlinio
ElbertePlinio merged commit a597670 into main Oct 8, 2026
10 checks passed
@ElbertePlinio
ElbertePlinio deleted the fix/301-306-viewer-bridge-port-tests branch October 8, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant