Repository navigation
fix(fabricator): stop bytecode hangs from header aliasing and stdout traces - #294
Conversation
|
Hi @chrhoffmann and thanks for your PR! I will back from vacation on Monday and i will review this ASAP! |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #294 +/- ##
==========================================
+ Coverage 87.62% 88.26% +0.64%
==========================================
Files 23 23
Lines 7926 8217 +291
Branches 1214 1270 +56
==========================================
+ Hits 6945 7253 +308
+ Misses 974 957 -17
Partials 7 7
🚀 New features to boost your workflow:
|
robertsLando
left a comment
There was a problem hiding this comment.
Summary
Verdict: Ship with minor changes.
The child-side rewrite is genuinely correct — I fuzzed it against every one of the 119 split offsets plus byte-at-a-time delivery of a 3-frame stream and a 3MB body: zero failures. The three new tests are real regressions, not tautologies: against the base script the multi-frame case yields frames=1, expected 2, and the bad-header case exits 0 silently, which is the actual silent-corruption path. Dropping the raw console.log(stdout.toString()) binary dump is right. buildFabricatorRequestChunks is genuinely wired into fabricate(), not test-only.
Two things to settle before merge, and one correction to the PR description.
Top 3 risks
- The one reachable fabricator hang lives in the parent decoder this PR didn't touch.
- Non-debug users now see strictly less on failure than before this PR.
- A protocol corruption silently degrades to
--fallback-to-source, so a real bug ships as a green build.
Themes
- One protocol, two half-implementations. The child got
while+ bounds guards + trailing-byte retention; the parent decoder got none of it. Four independent review passes landed on this. - The correct parser exists only in the test file.
parseBlobFrameshandles multi-frame and negative sizes; production'sonDatadoes neither. - Diagnosability moved backwards, in a PR whose stated purpose is explaining failures.
Findings that fall outside the diff (can't be line-anchored)
Major — lib/fabricator.ts:160-171, the parent's onData
This reads sizeOfBlob with none of the guards just added to the child, and it is reachable, not theoretical:
bakes come from the user's --options (lib/index.ts:171), and fabricate's filter strips only --prof/--v8-options/--trace-opt/--trace-deopt. So --options trace-gc writes GC traces to stdout, straight into the framed response stream:
Buffer.from('[123:0x5').readInt32LE(0)=858927451→stdout.length >= 4 + sizeOfBlobnever becomes true,cbnever fires, and pkg hangs forever (there is no timeout anywhere in this path).- A byte with the high bit set gives a negative size →
stdout.length >= 4 + (-1)is true →Buffer.alloc(-1)throwsERR_OUT_OF_RANGEinside a'data'handler, uncaught, crashing pkg instead of routing throughonError/--fallback-to-source.
This is pre-existing, so I'm not treating it as a merge blocker — but it's the only actual deadlock in this file, and the PR title is "prevent stdio deadlocks". Worth either fixing here or being explicit that it's out of scope.
Separately: onData consumes one frame and drops everything past 4 + sizeOfBlob, and removeListener('data', ...) does not pause a flowing stream. Today that's masked only because producer.ts:478 issues one request per Multistream callback — and note the child's old stdin = Buffer.alloc(0) used to enforce that lock-step. This PR removes that enforcement, so the child can now pipeline while the parent still can't read it.
Major — no timeout on the child's response
If the child accepts a valid frame and then hangs, fabricate never calls back and pkg stalls with no log line. That's the failure mode immediately adjacent to the one this PR targets.
Major — tests don't reach the parent half
The tests exercise the extracted script and helper; fabricate() itself, the parent decode, and the stderr routing change are all untested — and that untested half is where every remaining gap is. Driving the tests through fabricate() with a Target whose binaryPath is process.execPath would cover both halves, and would remove the need for the two new exports.
Minor — lib/fabricator.ts:143
onClose(code: number) but 'close' emits number | null. Runtime behaviour is fine; the type isn't.
On the PR description
I tried to reproduce all three claimed root causes. Results:
- Claim A (shared header buffer aliasing) — real Node hazard, not reachable here. A repro confirms
Writable.write()retains Buffers by reference: with 1MB pre-filled to force backpressure, the base sendsH1=3145728instead ofH1=36. But at the mutation point onlyh(4B) +snap(~40B) are in flight — far under the 64KB pipe buffer — souv_try_writecompletes them synchronously. Without pre-filled backpressure (4 configurations tried, including a child delaying reads by 400ms) the base is uncorrupted. Worth the 2-line fix as latent-hazard cleanup; it is not a hang anyone is hitting. - Claim B (trailing-byte truncation) — theoretical.
fabricatehas exactly one caller (lib/producer.ts:478), inside aMultistreamfactory, andmultistreamcalls_next()only from the current stream'sonEnd. Targets are serialized too (lib/index.ts:313-326). One request → full response → next. Trailing bytes can never exist on the child's stdin. - Claim C (debug stderr causes backpressure deadlock) — not a bug. The base passes
process.stdoutasstdio[2], so Node inherits the fd rather than creating an unread pipe. Repro: child writes 5MB to stderr → exit 0 in 587ms. The PR's'pipe'does correctly attach a reader, so it doesn't introduce one either. Net: a routing change, not a deadlock fix.
The changes are still worth having — but the description reads as three observed deadlocks, and I could not reproduce any of them as such. Could you share the actual reproducer, or retitle to drop the deadlock claim? Also, the validation section cites npm run test:unit / npm run lint / npm run build; this repo is yarn-only at the root (npm would create a stray package-lock.json).
Two things verified clean, for the record: non-ASCII snap paths work correctly (toString('utf8', 4, 4 + sizeOfSnap) takes byte offsets, matching Buffer.from(snap) — checked end-to-end with /snapshot/ünïcodé-èà.js), and the child.stderr listener is attached once per spawn inside if (!child) with kill() deleting the cache key, so there's no leak. The 256MB ceiling also can't regress real payloads — bodies are per-file source buffers and module.wrap already caps at Node's 512MB string limit.
Coverage: correctness, DRY, performance, design/API, tests, operability, readability. Security not run — no files in its lane (build-time IPC, no auth/crypto/network surface). No prior unresolved review threads.
Review follow-up for yao-pkg#294: - shared tryParseFabricatorResponse guards the parent decoder against garbage/negative headers (reachable hang via e.g. --options trace-gc leaking bake output into stdout) instead of stalling forever or throwing ERR_OUT_OF_RANGE inside a data handler - framing violations now exit 3 (FABRICATOR_PROTOCOL_EXIT_CODE), distinct from exit 2 'V8 refused to compile'; producer aborts the build loudly instead of silently degrading to --fallback-to-source - 60s response timeout per request; hung children are killed and reported with a bounded stderr tail attached to every failure - unexpected-close output embedded as a printable-safe snippet; fabricateTwice no longer retries deterministic or protocol errors - remainder copies no longer pin large backing stores - tests run against the production parser, cover body-header rejection, zero-length payloads, deterministic split offsets, and an end-to-end fabricate() run; all child waits timeout-wrapped - document frame protocol, 256MB ceiling rationale and exit-code contract in docs/ARCHITECTURE.md
Review follow-up for yao-pkg#294: - shared tryParseFabricatorResponse guards the parent decoder against garbage/negative headers (reachable hang via e.g. --options trace-gc leaking bake output into stdout) instead of stalling forever or throwing ERR_OUT_OF_RANGE inside a data handler - framing violations now exit 3 (FABRICATOR_PROTOCOL_EXIT_CODE), distinct from exit 2 'V8 refused to compile'; producer aborts the build loudly instead of silently degrading to --fallback-to-source - 60s response timeout per request; hung children are killed and reported with a bounded stderr tail attached to every failure - unexpected-close output embedded as a printable-safe snippet; fabricateTwice no longer retries deterministic or protocol errors - remainder copies no longer pin large backing stores - tests run against the production parser, cover body-header rejection, zero-length payloads, deterministic split offsets, and an end-to-end fabricate() run; all child waits timeout-wrapped - document frame protocol, 256MB ceiling rationale and exit-code contract in docs/ARCHITECTURE.md
bc2d1ab to
64a8921
Compare
robertsLando
left a comment
There was a problem hiding this comment.
Deep review — 4 files, +777 / −65 LOC
Verdict: Needs work. Two blockers, both in the wiring around a parser that is itself solid.
Top 3 risks
- The pipelined fast path answers a request that was never sent, delivering another file's bytecode — the exact silent corruption this PR exists to stop (
lib/fabricator.ts:398). - The 60s timeout is tagged as a protocol error, so a slow-but-healthy compile now hard-fails with
--fallback-to-sourceignored (lib/fabricator.ts:277). - The stated root cause —
--options trace-gcwriting to the child's stdout — is documented but not removed. The frame channel still shares stdout, so that flag turns from a hang into an unbuildable configuration.
Strengths. The corrected, honestly-labelled problem statement in the PR description is genuinely rare and made this review much faster. Tests assert against the production parser rather than a copy. 11 of the 13 threads from the previous round are verifiably addressed. The frame layout and exit-code contract are now written down in docs/ARCHITECTURE.md.
Themes
- Guards on a channel that's still polluted. The PR identifies stdout-sharing as the root cause, then hardens the reader instead of moving the frames off stdout. Everything in the
trace-gcfamily below follows from that one decision. - New fatal class, old plumbing.
FABRICATOR_PROTOCOLis now the only build-aborting error infabricator.ts, but it doesn't go throughwasReported(), doesn't appear in the user-facing docs, and doesn't clean up the partially-written output file. - The parser is tested; the state machine around it isn't. Both blockers live in code the suite never drives, which is why they survived a green run.
Two findings that fall outside the diff
[Major · Design] lib/fabricator.ts — the bakes filter is the actual root cause, and it's untouched.
fabricate() strips only --prof / --v8-options / --trace-opt / --trace-deopt. But --options trace-gc writes to the child's stdout (measured: ~15KB stdout, 0 stderr), as do trace-gc-verbose, print-bytecode, trace-ic and trace-turbo. This PR converts that from an infinite hang into a hard, unescapable abort — a supported user flag becomes an unbuildable configuration with no override. The durable fix is to stop sharing stdout between V8 traces and frames: move the frame channel to a dedicated fd (stdio[3]). Widening the filter is the cheaper stopgap.
[Minor · Docs] docs-site/ still promises the old contract.
guide/bytecode.md:59, guide/targets.md:99 and guide/getting-started.md:127 all state that --fallback-to-source ships source "when bytecode generation fails". The new always-fatal exception is documented only in the contributor-facing docs/ARCHITECTURE.md. Users hitting the new abort will read the guide and conclude pkg is broken.
Coverage
Specialists run: Correctness, DRY (repo-wide), Performance, Design/API/BackCompat, Tests, Operability, Readability. Every lane returned findings; none came back clean.
Not run: Security — no changed file matched its lane. The child's spawn arguments are build-time flags rather than untrusted input, and the -e script is a module constant with only numeric constants interpolated.
Reviewed against a dedicated worktree at 64a8921, so repo-wide reuse checks and sibling-file context were complete.
|
Thanks for this PR. We have production evidence that the shared header buffer aliasing is reachable. On Windows it is the cause of Environment: GitLab.com hosted Windows runners ( Symptom: the build hangs forever after the walker finishes. pkg and the fabricator child both sit at 0 CPU with every thread waiting, and there is no output or error. 19 of 28 instrumented builds hung. Evidence the aliasing is the cause: We patched
We then ran arms side by side in the same window, so all ran under the same runner conditions:
One note on how #294 handles this case. With the aliasing, It would be great to see this merged and released. We're happy to test a prerelease on the same runners. Until a release ships, this workaround fixes it in CI after |
- Move the response channel to a dedicated fd 3 pipe; ignore child stdout so V8 trace bakes (--options trace-gc, print-bytecode, ...) cannot corrupt frames. - One persistent listener set per child; drop the pipelined fast path. Output with no pending request or bytes after a response is a protocol error. - Evict children on exit, SIGKILL on failure, reset the stderr tail per request and keep its end, not its head. - Timeouts get their own FABRICATOR_TIMEOUT code and degrade like compile failures instead of aborting the build; coded errors are never retried. - Protocol exit code 3 -> 65 (outside Node's reserved range); protocol aborts go through wasReported() with remedy hints. - Linear-time frame accumulation on both sides; cap cachedData on the child. - Unit tests drive fabricate() against stub children (stdout noise, corrupt response, timeout, compile failure, child reuse). - Fix the ARCHITECTURE.md protocol section and note the fatal case in the --fallback-to-source guide.
- Ignore late events from a killed child instead of reporting a desync; shutdown() goes through the same kill path (SIGKILL). - One message shape for spawn/pipe errors and exits; no 'file undefined'. - Debug-log a cached child that dies with no request pending. - Tests: fabricateTwice retry policy, exit 65 through fabricate(), trailing and unsolicited bytes, respawn after timeout, and producer() routing (protocol error aborts even with --fallback-to-source; compile failure falls back).
- Timeout goes through fail() like every other failure.
- Document that stray output between requests restarts the child with a
warning rather than failing the build.
- Tests: shared preload snippets, explicit require('fs') in each snippet,
simpler countedBakes, one comment per reason.
A failed build left a truncated binary at --output. producer() now destroys the output stream and removes the file before rejecting.
- Fold the response parser into onResponseData; leftover bytes were always an error, so the remainder plumbing goes away. - Keep the stderr tail as one bounded Buffer. - Use 'still the registered child' instead of a separate killed flag. - One message context for compile, protocol and timeout errors. - Test: a response split across chunks is reassembled byte for byte; negative response headers are protocol errors.
- Ignore stdin EPIPE so 'close' classifies the exit: a child exiting 65 mid-write is a protocol error, not a retryable compile failure. - Protocol hint no longer names an unactionable cause. - Tests: exit-65 test sends 1MB so EPIPE happens first; child reuse is asserted; body header guard tested at both bounds; unsolicited-write test waits on a marker instead of a fixed delay.
finalize-pr: 5 review rounds, exit with no Blocker/Major openDecided by the maintainer
Dropped, with reasons
|
Summary
Fixes two ways the bytecode fabricator can hang a
pkgbuild forever, andmakes every other fabricator failure either fall back cleanly or abort
loudly, never stall or ship wrong bytecode.
Problems fixed
1. Shared header buffer aliasing (reachable, Windows CI hangs). The
parent wrote both request headers from one reused 4-byte buffer. When
child.stdinwas still draining the previous request, the first headerwas queued by reference and then overwritten with the body length before
it was sent. The child then read the wrong snap size and waited forever.
Confirmed in production on GitLab Windows runners
(comment):
19 of 28 instrumented builds hung, every hung request started with a
non-empty
writableLength, and using separate header buffers took hangsfrom 2/4 to 0/5. Each request now builds its own header buffers
(
buildFabricatorRequestChunks).2. V8 trace flags corrupting the response stream.
--options trace-gc(also
print-bytecode,trace-ic, ...) is passed to the fabricator childand prints to its stdout, which was also the response channel. The parent
read trace text as a frame size and waited forever. Responses now come back
on a dedicated pipe at fd 3, and the child's stdout is ignored. Checked
with a 3.8MB file and
--options trace-gc: the old code fails withInvalid blob size header: 842150491, the new code compiles it.Behaviour
--fallback-to-sourceships source, otherwise skip + warn--fallback-to-source, with remedy hintsUser-visible changes:
--optionsno longer hang.instead of hanging the build.
file to source. The message points at
--no-bytecodeand the issuetracker.
--output.that request, so
Pkg: Cached data not produced.is visible without--debug.files between 256MB and 512MB that used to compile are now per-file
failures.
Changes
lib/fabricator.tsOutput with no request pending, or bytes left after a response, is a
protocol error. Children are evicted when they exit and respawned on the
next call.
FABRICATOR_MAX_FRAME_PART_SIZE. Frames are assembled in linear time:chunks are only concatenated once enough bytes for the next parse step
have arrived.
FABRICATOR_PROTOCOL_EXIT_CODE),outside Node's reserved exit codes. Exit 2 still means "V8 refused this
file".
FABRICATOR_FRAME_TOO_LARGE,FABRICATOR_TIMEOUT,FABRICATOR_PROTOCOL, read withfabricatorErrorCode()); coded failuresare never retried by
fabricateTwice.lib/producer.tsFABRICATOR_PROTOCOLaborts the build throughwasReported()with hintlines; everything else keeps the existing fallback/skip path. When
producer()fails for any reason it closes the output stream and removesthe partial file before rejecting.
Docs
docs/ARCHITECTURE.md: new "Bytecode Fabricator IPC" section with theframe layout, size cap and the outcome table above.
docs-site/guide/bytecode.md: notes that--fallback-to-sourcecoverscompile failures and timeouts, but not a corrupted fabricator exchange.
Tests (
test/unit/fabricator.test.ts)headers exit 65, zero-length parts.
fabricate()against the real child, with misbehaviour injected through--requirepreload bakes: stdout noise is ignored, corrupt response,trailing bytes, unsolicited output (child restarts), exit 65, compile
failure, timeout then respawn, two requests on one child, a response
split across chunks, and exit 65 while a 1MB request is still being
written.
fabricateTwice: plain failures retried once, coded failures never.producer()with a minimal fake base binary: a protocol error abortseven with
--fallback-to-sourceand removes the partial output; acompile failure falls back to source.
Validation
yarn build,yarn lintcleanyarn test:unit: 311 pass, fabricator suite repeated 30x without flakesnode test/test.js node22 no-npm "test-50-*"passes locally (excepttest-50-ignore-files, which fails only inside a.claude/worktreepath and passes in a normal checkout)