diff --git a/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/7640-child-spawn-before-fast-exit.ts.json b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/7640-child-spawn-before-fast-exit.ts.json new file mode 100644 index 000000000..0b82f6091 --- /dev/null +++ b/packages/compiler/test/ts7/baselines/order-parity/tests/corpus/7640-child-spawn-before-fast-exit.ts.json @@ -0,0 +1,6 @@ +{ + "order": [ + "/tests/corpus/7640-child-spawn-before-fast-exit.ts" + ], + "diags": [] +} diff --git a/packages/runtime/src/scr_child.c b/packages/runtime/src/scr_child.c index df7543500..a70b10894 100644 --- a/packages/runtime/src/scr_child.c +++ b/packages/runtime/src/scr_child.c @@ -2780,7 +2780,8 @@ static void scr_child_settle(ScrChild *c) { scr_children_reffed_n--; } if (c->state == SCR_CHILD_EXITED) { - scr_child_stream_drain_now(c->out_stream); + if (!c->spawn_done) scr_child_fire_spawn(c); + if (!scr_exc_pending()) scr_child_stream_drain_now(c->out_stream); if (!scr_exc_pending()) scr_child_stream_drain_now(c->err_stream); if (!scr_exc_pending()) scr_ipc_settle(c->ipc); } @@ -5607,9 +5608,12 @@ static void scr_child_settle(ScrChild *c) { /* Streams first, settled after: an exit listener registered from a * draining 'data'/'end' callback still fires below, like Node's. The * SPAWN-FAILURE path skips the drain — Node fires 'error' first and - * the streams' immediate EOF delivers 'end' on the next turn. */ + * the streams' immediate EOF delivers 'end' on the next turn. A child + * spawned during this pass can exit before the next pass fires 'spawn'; + * Node emits 'spawn' before any data or 'exit', so it fires here first. */ if (c->state == SCR_CHILD_EXITED) { - scr_child_stream_drain_now(c->out_stream); + if (!c->spawn_done) scr_child_fire_spawn(c); + if (!scr_exc_pending()) scr_child_stream_drain_now(c->out_stream); if (!scr_exc_pending()) scr_child_stream_drain_now(c->err_stream); if (!scr_exc_pending()) scr_ipc_settle(c->ipc); } diff --git a/tests/corpus/7640-child-spawn-before-fast-exit.ts b/tests/corpus/7640-child-spawn-before-fast-exit.ts new file mode 100644 index 000000000..9c232d96c --- /dev/null +++ b/tests/corpus/7640-child-spawn-before-fast-exit.ts @@ -0,0 +1,20 @@ +// A child spawned from a 'close' listener that exits before the listener +// returns still emits 'spawn' before 'exit'. +import { spawn } from "node:child_process"; + +function busyWait(ms: number): void { + const end = Date.now() + ms; + while (Date.now() < end) {} +} + +const events: string[] = []; +spawn("sh", ["-c", "exit 0"], { stdio: "ignore" }).on("close", () => { + const child = spawn("sh", ["-c", "exit 4"], { stdio: "ignore" }); + child.on("spawn", () => events.push("spawn")); + child.on("exit", (code) => events.push(`exit ${code}`)); + child.on("close", (code) => { + events.push(`close ${code}`); + console.log(events.join(", ")); + }); + busyWait(300); +}); diff --git a/tests/harness/corpus-costs/7640-child-spawn-before-fast-exit.ts.json b/tests/harness/corpus-costs/7640-child-spawn-before-fast-exit.ts.json new file mode 100644 index 000000000..b56ad904d --- /dev/null +++ b/tests/harness/corpus-costs/7640-child-spawn-before-fast-exit.ts.json @@ -0,0 +1 @@ +{ "plain": 2600, "san": 4000 }