Skip to content

fix(bench): keep V8 trace harness stdout off a non-blocking pipe - #1569

Merged
trueadm merged 2 commits into
mainfrom
fix/v8-trace-harness-stdout
Oct 2, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/v8-trace-harness-stdout

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two V8 diagnostic harnesses, benchmarks/client-hot-paths/functions.mjs and benchmarks/ssr-final-replay/diagnostics.mjs, spawn a worker with --print-bytecode, --trace-opt and %DebugPrint. Each one parsed V8's native trace output and a FUNCTIONS_JSON result line from the same piped stdout. On a loaded machine that protocol drops or splits records. This PR moves both harnesses onto file-backed stdout.

Why it breaks: Node switches a stdout pipe to O_NONBLOCK the first time process.stdout is touched. V8 writes its traces through C stdio on that same fd. When the parent drains the pipe slowly, those writes fail with EAGAIN, and records are lost or split mid-line. The harness then fails with worker did not return results or <name> bytecode missing. Under milder load it can instead exit 0 with a report that is quietly missing trace lines.

Change (identical in both harnesses):

  • run() opens a regular file for the worker's stdout and passes stdio: ['ignore', fd, 'pipe']. A regular file never blocks, and Node writes to it synchronously. Stderr stays piped.
  • The worker writes its result JSON to the path in CLIENT_FUNCTION_RESULT / SSR_FUNCTION_RESULT. When the variable is unset, it still prints FUNCTIONS_JSON for manual --worker runs.
  • Both temp files (worker-<pid>-<n>.log / .json, in the harness's node_modules/.cache directory) are removed after reading, including when the worker fails. A failing worker still reports its stderr plus the tail of stdout.

The functions.mjs change is copied byte-for-byte from the session that found the problem, so both harness fixes ship together here.

No changeset: this is internal benchmark tooling with no package or user-facing change.

Validation

All runs on Node 24.18 / V8 13.6, darwin arm64, on a machine with a load average of about 35–50.

  • Idle report unchanged.

    • diagnostics.mjs: three patched runs are byte-identical to the pre-change report in every field, including all 19 optimizationTrace lines.
    • functions.mjs: every field except optimizationTrace matches the pre-change report. The two pre-change runs differ from each other there, by V8's global opt id counter, so that field already varied run to run. Two patched runs are identical to each other.
  • Throttled drain, worker level. I piped the SSR worker's stdout through a slow reader (4 KB reads, 5 ms sleeps). Output shrank from 4.16 MB to 2.93 MB, 2 of the 3 target bytecode records were lost, and 2 FUNCTION_REFERENCE markers landed mid-line. An earlier client-worker reproduction (2 KB reads, 0.3 ms sleeps) shrank stdout from about 15.8 MB to 11.9 MB and lost the componentSlotImpl bytecode record.

  • Throttled drain, end to end. Each harness ran while its own process was SIGSTOP/SIGCONT-cycled, so it drained the worker's output slowly:

    Harness Throttle (stopped/running ms) Before After
    ssr-final-replay/diagnostics.mjs 40/10, 90/3 4 of 4 failed: ssrTry bytecode missing 4 of 4 passed; report identical to the idle run
    client-hot-paths/functions.mjs 40/10 failed: reconcileKeyed bytecode missing passed; all records present, and one mapSlot deopt event differs in kind (JIT variance, not loss)

    With a weaker, late-starting throttle, the old SSR harness exited 0 but silently dropped the ssrTry deopt line from optimizationTrace.

  • Also checked:

    • SSR_FUNCTION_TRACE_DIRECTORY still dumps the full worker stdout.
    • A worker that crashes (missing artifact) surfaces its error.
    • No worker-* temp files remain after any run.
  • Gates:

    • pnpm sync: no generated changes
    • pnpm format:files:check on both files
    • pnpm typecheck (these .mjs harnesses belong to no TypeScript project)
    • pnpm test: not run. Neither harness is imported by a test or invoked by CI.

Risk / follow-ups

  • Only these two harnesses are changed, and neither runs in CI. Other benchmark scripts that pipe V8 traces would need the same treatment if any are added.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Low Risk
Changes are limited to internal benchmark harnesses that are not run in CI and do not affect packages or runtime behavior.

Overview
Fixes flaky or silently incomplete V8 diagnostic reports in benchmarks/client-hot-paths/functions.mjs and benchmarks/ssr-final-replay/diagnostics.mjs when the parent process drains the worker’s stdout slowly. V8 writes traces through C stdio on a pipe that Node marks non-blocking, which can drop or split lines and break bytecode/trace parsing (or pass with missing trace data).

Each harness’s run() now sends worker stdout to a cache-dir log file (stdio: ['ignore', fd, 'pipe']) and reads it back after the child exits. Structured results go to a separate JSON file via CLIENT_FUNCTION_RESULT / SSR_FUNCTION_RESULT instead of a FUNCTIONS_JSON line mixed into the trace stream; manual --worker runs still log FUNCTIONS_JSON when those env vars are unset. Temp .log / .json files are removed after reading, including on failure.

Reviewed by Cursor Bugbot for commit 2f51fd9. Bugbot is set up for automated code reviews on this repo. Configure here.

The client-hot-paths and ssr-final-replay function harnesses parsed V8's
--print-bytecode/--trace-opt/DebugPrint output and a FUNCTIONS_JSON result
line from one piped stdout. Node makes that pipe O_NONBLOCK, so when the
parent drains slowly V8's C-stdio writes fail with EAGAIN and records are
dropped or split mid-line ("worker did not return results", "<name>
bytecode missing", or a silently shorter optimizationTrace).

Give the worker a regular file for stdout, which never blocks, and have it
write its result to a file named by CLIENT_FUNCTION_RESULT /
SSR_FUNCTION_RESULT. Manual --worker runs without the variable still print
FUNCTIONS_JSON. Both temp files are removed after reading.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 2, 2026
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 14:55
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares 7c6cd951f with b8bfb6021 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 3.34 4.35 +30.2% ±32% ⚪ within noise
octane-tsrx replace 7.96 8.94 +12.3% ±37% ⚪ within noise
octane-tsrx add 3.44 3.98 +15.7% ±18% ⚪ within noise
octane-tsrx update 0.49 0.60 +22.4% ±46% ⚪ within noise
octane-tsrx select 0.24 0.26 +8.3% ±56% ⚪ within noise
octane-tsrx swap 0.45 0.55 +22.2% ±41% ⚪ within noise
octane-tsrx remove 0.42 0.46 +9.5% ±47% ⚪ within noise
octane-tsrx runlots 30.78 30.73 −0.2% ±15% ⚪ within noise
octane-tsrx select_lots 0.27 0.29 +7.4% ±49% ⚪ within noise
octane-tsrx clear 47.06 45.37 −3.6% ±20% ⚪ within noise
octane-jsx run 4.47 3.88 −13.2% ±26% ⚪ within noise
octane-jsx replace 9.79 8.60 −12.2% ±26% ⚪ within noise
octane-jsx add 3.83 3.60 −6.0% ±32% ⚪ within noise
octane-jsx update 0.82 0.79 −3.7% ±42% ⚪ within noise
octane-jsx select 0.67 0.41 −38.8% ±46% ⚪ within noise
octane-jsx swap 0.93 0.66 −29.0% ±45% ⚪ within noise
octane-jsx remove 0.81 0.70 −13.6% ±35% ⚪ within noise
octane-jsx runlots 34.74 30.86 −11.2% ±33% ⚪ within noise
octane-jsx select_lots 3.34 3.05 −8.7% ±41% ⚪ within noise
octane-jsx clear 51.73 51.48 −0.5% ±19% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm
trueadm merged commit 5378778 into main Oct 2, 2026
30 checks passed
@trueadm
trueadm deleted the fix/v8-trace-harness-stdout branch October 2, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant