Skip to content

Commit 115587e

Browse files
simclaude
andcommitted
fix(#5170): test-file children write their results to a blocking stdout, so --test-force-exit no longer drops them (#4031)
CI (docs-lint, plugin-validate) failed with "N tests registered, M reported" while the TAP summary itself said M, so the accounting was right and results really did not reach the report. Root cause, from node's v24 runner source and measured on a Linux node:24 container: the runner forwards --test-force-exit to every test-file child (getRunArgs); each forced child calls process.exit() the moment its root test ends, and its stdout, the pipe its V8-framed results travel on, is non-blocking on POSIX, so frames still queued in the Socket are discarded (nodejs/node#64833, fix proposed in nodejs/node#64875). It needs no concurrency: plugin-validate lost 23 of 54 in a single file. Measurement (Node 24.18.0, Linux, 20 files x 300 tests, concurrency 8, the runner's own events file and ledger, 4 runs each): count-only preload 5049-5074 reported of 6000 (ledger 6000: the child had emitted them all, so the loss is child to parent); preload that makes the child's stdout blocking 6000/6000 on every run; without force-exit 6000/6000. registration-ledger-preload.cjs now sets the child's stdout handle blocking at load (blockChildStdout, best-effort, test-file children only), the same operation the upstream fix performs at force-exit, but done up front so nothing is ever queued. --test-force-exit stays on every platform: it is the Windows post-test hang guard (#1051) and nothing about that changes, whereas dropping it on POSIX would trade a loud accounting failure for silent hangs on leaked handles. The accounting stays as the backstop for any other loss. Unit rows pin blockChildStdout; the behavioral proof is a real shard's own accounting. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent a18dbbc commit 115587e

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎scripts/lib/registration-ledger-preload.cjs‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@
4444
// `ledgerCountedNothing`), and tests/run-tests-accounting.test.cjs pins the hook.
4545
//
4646
// Inert unless it is inside a test-file child (NODE_TEST_CONTEXT is set by
47-
// `node --test` for those, not for the runner parent) AND a ledger path was
48-
// supplied, so requiring it anywhere else is a no-op. Never throws into the
49-
// code it observes.
47+
// `node --test` for those, not for the runner parent), so requiring it anywhere
48+
// else is a no-op. There it does two things: it makes the child's stdout pipe
49+
// blocking (blockChildStdout: this is what stops `--test-force-exit` from
50+
// discarding queued results, the loss this accounting exists to detect), and,
51+
// when a ledger path was supplied, counts results. Never throws into the code it
52+
// observes.
5053

5154
const fs = require('fs');
5255
const path = require('path');
@@ -98,7 +101,40 @@ function install(ledgerPath, serializerPrototype = DefaultSerializer.prototype)
98101
});
99102
}
100103

101-
const ledgerPath = process.env.GSD_RUN_TESTS_LEDGER_FILE;
102-
if (ledgerPath && process.env.NODE_TEST_CONTEXT) install(ledgerPath);
104+
/**
105+
* Make the test-file child's stdout (the pipe its results travel to the `node --test` parent on)
106+
* blocking, so every frame it wrote has reached the OS when the child exits.
107+
*
108+
* Why: the runner passes `--test-force-exit`, and node forwards it to each file child. A forced
109+
* child calls process.exit() as soon as its root test ends; its stdout is a non-blocking pipe on
110+
* POSIX, so frames still queued in the Socket are discarded and the parent never receives those
111+
* results (nodejs/node#64833; the fix proposed there, nodejs/node#64875, does exactly this with
112+
* `_handle.setBlocking(true)` at force-exit). Measured on Node 24.18 / Linux with 20 files x 300
113+
* tests, `--test-force-exit --test-concurrency=8`: 5039-5139 of 6000 results reported without
114+
* this, 6000 of 6000 with it (and 6000 without force-exit). Doing it at load time rather than at
115+
* exit means nothing is ever queued. The Windows hang guard that force-exit exists for (#1051)
116+
* is untouched. Best-effort: a stdout with no pipe handle (a file, a TTY) is left alone.
117+
*/
118+
function blockChildStdout(stdout) {
119+
try {
120+
const target = stdout === undefined ? process.stdout : stdout;
121+
const handle = target && target._handle;
122+
if (handle && typeof handle.setBlocking === 'function') {
123+
handle.setBlocking(true);
124+
return true;
125+
}
126+
} catch {
127+
// Never throw into the test file this observes.
128+
}
129+
return false;
130+
}
131+
132+
// Inside a test-file child only (NODE_TEST_CONTEXT is set by `node --test` for those, not for the
133+
// runner parent): the blocking stdout needs no ledger path; the ledger count needs one.
134+
if (process.env.NODE_TEST_CONTEXT) {
135+
blockChildStdout();
136+
const ledgerPath = process.env.GSD_RUN_TESTS_LEDGER_FILE;
137+
if (ledgerPath) install(ledgerPath);
138+
}
103139

104-
module.exports = { install, resultFileOf };
140+
module.exports = { install, resultFileOf, blockChildStdout };

‎scripts/run-tests.cjs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,10 +1251,14 @@ function analyzeChunkEvents(eventsPath) {
12511251
};
12521252
}
12531253

1254-
// #4031: per-file test accounting for one chunk. `--test-force-exit` can end
1255-
// the `node --test` parent while part of a test file's results are still
1256-
// unread on the child's pipe, so the reporters (and this runner's own ndjson
1254+
// #4031: per-file test accounting for one chunk. `--test-force-exit` is forwarded
1255+
// by node to every test-file child, which calls process.exit() the moment its root
1256+
// test ends while results are still queued in its non-blocking stdout pipe
1257+
// (nodejs/node#64833), so the parent's reporters (and this runner's own ndjson
12571258
// events file) never see them and the run reports a smaller count with exit 0.
1259+
// The loss is prevented at its source (registration-ledger-preload.cjs makes the
1260+
// child's stdout blocking; measured 6000/6000 vs ~5070/6000 on Node 24.18, Linux) and
1261+
// this accounting stays as the loud backstop for whatever else drops results.
12581262
// The count of results the child REGISTERED with its reporter therefore comes
12591263
// from the child, through a channel that does not share that pipe:
12601264
// scripts/lib/registration-ledger-preload.cjs (loaded with `--require`) counts the

‎tests/run-tests-accounting.test.cjs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,27 @@ nt.test('viaprop', () => {});
380380
for (const [item, expected] of cases) assert.equal(resultFileOf(item), expected, JSON.stringify(item));
381381
});
382382

383+
// The loss itself (#4031): `--test-force-exit` is forwarded to every test-file child, whose
384+
// process.exit() discards results still queued in its non-blocking stdout pipe. Measured on Node 24.18 /
385+
// Linux, 20 files x 300 tests, concurrency 8: ~5070 of 6000 reported with a count-only preload, 6000 of
386+
// 6000 once the child's stdout is blocking. The behavioral proof needs a real parent, so it is the
387+
// runner's own accounting on a real shard; these rows pin the unit contract.
388+
test('blockChildStdout: a pipe handle is made blocking; anything else is left alone and never throws', () => {
389+
const { blockChildStdout } = require('../scripts/lib/registration-ledger-preload.cjs');
390+
const calls = [];
391+
assert.equal(blockChildStdout({ _handle: { setBlocking: (v) => calls.push(v) } }), true);
392+
assert.deepEqual(calls, [true], 'blocking, not toggled');
393+
assert.equal(blockChildStdout({}), false, 'a stream with no handle (a file) is left alone');
394+
assert.equal(blockChildStdout({ _handle: {} }), false, 'a handle without setBlocking is left alone');
395+
assert.equal(blockChildStdout({ _handle: { setBlocking: () => { throw new Error('EBADF'); } } }), false, 'a throwing handle is swallowed');
396+
assert.equal(blockChildStdout(null), false);
397+
});
398+
399+
test('inside a test-file child without a ledger path the preload still loads cleanly (the blocking is independent of the ledger)', (t) => {
400+
const noLedger = runWithPreload(t, { context: 'child-v8', ledger: false });
401+
assert.equal(noLedger.r.status, 0, noLedger.r.stderr);
402+
});
403+
383404
test('it is inert outside a test-file child and without a ledger path', (t) => {
384405
const noContext = runWithPreload(t, { context: null, ledger: true });
385406
assert.equal(noContext.r.status, 0, noContext.r.stderr);

0 commit comments

Comments
 (0)