fix(#4306): extend fault-injection fd-swallow fix across the whole suite (#4308)

* fix(#4306): forward real bytes through io.test.cjs's fault-injection mocks

The bug #1008 fault-injection tests mock fs.writeSync scoped only by file
descriptor. On their "success" arms (the retry-after-EAGAIN/EINTR call, and
the short-write simulation) they fabricated a return byte count without ever
calling the real writeSync -- the bytes went into a local array and nowhere
else.

node:test's process-isolation runner (default on Node >= 22) reads each test
file's own stdout to parse its child-to-parent result protocol. If the
runner's own reporter write for an adjacent test lands on fd 1 while one of
these mocks is installed, that write was silently swallowed instead of
reaching the real pipe -- observed in CI as "Unable to deserialize cloned
data" (a corrupted/truncated byte stream on the parent's read side), not a
thrown exception.

Every "success" arm now forwards the real bytes to orig()/restore() instead
of fabricating a return value, so anything else sharing the fd during the
mocked window still gets its bytes delivered for real. writeAllSync (the
only production caller reaching this mock) always passes a Buffer, so the
forwarded calls use the buffer-form fs.writeSync overload unambiguously.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(#4306): extend fault-injection fd-swallow fix across the whole suite

The originally-fixed instance (tests/io.test.cjs) was one occurrence of a
copy-pasted defect: mocked fs.writeSync arms fabricated a return byte count
without ever forwarding the call to the real fs.writeSync, silently
discarding bytes. Under node:test's process-isolated runner, the parent
reads the child's real stdout to parse v8-serialized report frames
interleaved with plain output (confirmed against node's own
lib/internal/test_runner/runner.js and a matching upstream issue,
nodejs/node#64061) — a swallowed write on that fd corrupts the parent's
parse ("Unable to deserialize cloned data").

Adds a shared, safe capture helper to tests/helpers.cjs, captureFdSync(fd,
fn): it always forwards every write to the real fs.writeSync first, then
records only the observed fd's bytes, sliced by the real return count (not
the requested length), decoded once via Buffer.concat so a short write
can't split a multi-byte codepoint across two decodes.

17 test files migrate their local copy of the unsafe mock to this shared
helper. tests/worktree-base-ref.test.cjs keeps a narrower in-place fix
instead (it needs to record every fd a write touched, which the shared
helper doesn't expose).

tests/io.test.cjs gets two follow-up correctness fixes on top of the
already-committed forwarding fix: the EAGAIN/EINTR/short-write arms now
derive their recorded chunk from the real return count everywhere
(including the string-form overload), and the short-write test no longer
forces a Buffer-shaped truncation call onto a string-form write that could
land on the same fd.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-09-04 23:54:02 -04:00
committed by GitHub
parent 4499933807
commit 9ee6d54cc3
19 changed files with 208 additions and 380 deletions

View File

@@ -578,6 +578,68 @@ function mockPartialWriteThenThrow(fsModule, matches, bytesBeforeThrow, options
return () => { fsModule.writeFileSync = origWriteFileSync; };
}
/**
* Capture the bytes written to `captureFd` while `fn()` runs, WITHOUT ever
* fabricating a byte count for any fd (#4306).
*
* Every previous hand-rolled version of this idiom across the test suite
* mocked `fs.writeSync`, and on its "success" arm returned a fabricated byte
* count while pushing the bytes into a local array instead of ever calling
* the real `fs.writeSync` — the data reached nowhere but that array. That is
* unsafe: Node's `node:test` runner defaults to `--test-isolation=process`
* (Node >= 22), so each test file's own real stdout is what the PARENT
* runner reads to parse its child-to-parent result/TAP protocol. If the
* runner's own reporter write for an adjacent test lands on the mocked fd
* during this window, a mock that fabricates success without delivering the
* bytes silently swallows that write instead of letting it reach the real
* pipe — the parent then tries to parse a truncated stream, observed in CI
* as "Unable to deserialize cloned data" (Node's generic corrupted/truncated
* v8.deserialize error), not as a thrown exception.
*
* This helper always forwards every write, on every fd, to the real
* `fs.writeSync` first — so nothing is ever swallowed, regardless of what
* else shares the fd during the mocked window — and returns the REAL
* result. Only `captureFd`'s traffic is additionally recorded and returned
* to the caller as a joined UTF-8 string; every other fd's bytes still
* reach their real destination (e.g. a test's own stderr diagnostics still
* physically write to stderr, just outside the returned capture), they are
* simply not included in the returned string.
*
* Standalone — no node:test context required; save/restore in a `finally`
* so a thrown assertion still restores the real `fs.writeSync`.
*
* @param {number} captureFd - the fd to capture and return (1 for stdout, 2 for stderr).
* @param {() => void} fn - synchronous function to run while capturing.
* @returns {string} every byte actually written to `captureFd` during `fn()`.
*/
function captureFdSync(captureFd, fn) {
const chunks = [];
const orig = fs.writeSync;
fs.writeSync = (fd, data, ...rest) => {
const n = orig.call(fs, fd, data, ...rest);
if (fd === captureFd) {
// rest[0] is `offset` only for the buffer-form overload; the
// string-form overload's 2nd arg is `position`, which is irrelevant
// here since a string write has no byte offset into `data` itself.
const offset = Buffer.isBuffer(data) && typeof rest[0] === 'number' ? rest[0] : 0;
const buf = Buffer.isBuffer(data)
? data.subarray(offset, offset + n)
: Buffer.from(String(data), 'utf8').subarray(0, n);
// Buffered, not decoded per-call: a real short write can split a
// multi-byte UTF-8 codepoint across two writeSync calls, and decoding
// each half separately would corrupt it. Decode once, after joining.
chunks.push(buf);
}
return n;
};
try {
fn();
} finally {
fs.writeSync = orig;
}
return Buffer.concat(chunks).toString('utf8');
}
/**
* Read a workflow .md file plus every .md file under its sibling
* `<workflow-basename>/steps/` directory, concatenated in document order
@@ -1111,7 +1173,7 @@ function sandboxHome(t, dir) {
});
}
module.exports = { runGsdTools, createTempDir, createTempProject, createTempGitProject, cleanup, tmpRootCandidates, readFileNormalized, readWorkflowCombined, parseFrontmatter, isUsageOutput, captureConsole, toPosixPath, absPlanningPath, runNpm, isolatedNpmEnv, withIsolatedProcessState, delay, waitFor, resetRuntimeWarningCaches, SESSION_ENV_KEYS, saveSessionEnv, restoreSessionEnv, clearSessionEnv, isolateWorkstreamEnv, restoreWorkstreamEnv, TOOLS_PATH, SESSION_IDENTITY_ENV_KEYS, scrubConfigLocationEnv, installSpawnEnv, installSpawnHome, sandboxHome, TEST_HOME_SANDBOX_MARKER, mockPartialWriteThenThrow };
module.exports = { runGsdTools, createTempDir, createTempProject, createTempGitProject, cleanup, tmpRootCandidates, readFileNormalized, readWorkflowCombined, parseFrontmatter, isUsageOutput, captureConsole, toPosixPath, absPlanningPath, runNpm, isolatedNpmEnv, withIsolatedProcessState, delay, waitFor, resetRuntimeWarningCaches, SESSION_ENV_KEYS, saveSessionEnv, restoreSessionEnv, clearSessionEnv, isolateWorkstreamEnv, restoreWorkstreamEnv, TOOLS_PATH, SESSION_IDENTITY_ENV_KEYS, scrubConfigLocationEnv, installSpawnEnv, installSpawnHome, sandboxHome, TEST_HOME_SANDBOX_MARKER, mockPartialWriteThenThrow, captureFdSync };
// Lazy, for the reason builtLib() is lazy: reading either of these is what
// forces the built-lib require, so a test file that needs neither can still