Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/stim-cli/src/__tests__/exec.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,15 @@ test('a cancelled child whose pipes close after the stalled event loop resumes i
test.each([
['keeps output the leader wrote before exiting that was not read yet', '', 'x'.repeat(16 * 1024)],
[
'leaves out a write a detached descendant makes after the leader exited',
`require('node:child_process').spawn(process.execPath, ['-e', 'setTimeout(() => process.stdout.write("late"), 250)'], { detached: true, stdio: ['ignore', 'inherit', 'ignore'] }).unref();`,
'{"ok":true}',
'returns once a child the leader left holding its stdout is stopped, not at the timeout',
`require('node:child_process').spawn(process.execPath, ['-e', 'setTimeout(() => {}, 20000)'], { stdio: ['ignore', 'inherit', 'ignore'] }).unref();`,
'ready',
],
])(
'cleanup at leader exit %s',
async (_, helper, expected) => {
resetExecutor();
const started = Date.now();
const output = await getExecutor().runFileAsync(
process.execPath,
['-e', `${helper} process.stdout.write(${JSON.stringify(expected)})`],
Expand All @@ -478,12 +479,12 @@ test.each([
untrimmed: true,
onSpawn: (child) => {
child.stdout?.pause();
child.once('exit', () => child.stdout?.resume());
child.once('exit', () => setTimeout(() => child.stdout?.resume(), 300));
},
},
);
expect(output).toBe(expected);
await new Promise((resolve) => setTimeout(resolve, 500));
expect(Date.now() - started).toBeLessThan(10_000);
},
30_000,
);
32 changes: 9 additions & 23 deletions packages/stim-cli/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,20 +514,23 @@ function reportChild(
}
}
}
if (cancelReason === 'exit') await drainReadable(child, () => ended);
child.stdin?.destroy();
child.stdout?.destroy();
child.stderr?.destroy();
const awaitClose = async () => {
const awaitClose = async (ms = groupExitBudgetMs(REPORT_CHILD_EXIT_CAP_MS)) => {
let timer: ReturnType<typeof setTimeout> | undefined;
await Promise.race([
nativeClosed,
new Promise<void>((resolve) => {
timer = setTimeout(resolve, groupExitBudgetMs(REPORT_CHILD_EXIT_CAP_MS));
timer = setTimeout(resolve, ms);
}),
]);
clearTimeout(timer);
};
if (cancelReason === 'exit' && !ended) {
await awaitClose(EXIT_OUTPUT_DRAIN_MS);
await new Promise<void>((resolve) => setImmediate(resolve));
}
child.stdin?.destroy();
child.stdout?.destroy();
child.stderr?.destroy();
if (!ended) {
await awaitClose();
if (!ended) await new Promise<void>((resolve) => setImmediate(resolve));
Expand Down Expand Up @@ -559,23 +562,6 @@ function reportChild(
return { cancel, closed, left: () => left };
}

async function drainReadable(child: ChildProcess, closed: () => boolean): Promise<void> {
let read = 0;
const count = (chunk: Buffer) => {
read += chunk.length;
};
child.stdout?.on('data', count);
child.stderr?.on('data', count);
const deadline = performance.now() + EXIT_OUTPUT_DRAIN_MS;
let before: number;
do {
before = read;
await new Promise<void>((resolve) => setImmediate(resolve));
} while (!closed() && read !== before && performance.now() < deadline);
child.stdout?.off('data', count);
child.stderr?.off('data', count);
}

export async function spawnManagedCapture(
context: StimContext,
file: string,
Expand Down
Loading