Skip to content

Commit b3d651a

Browse files
committed
test(ssh): zombie-reap launch test no longer races the shell
The test killed the background child as soon as sh printed its pid. If the kill landed before sh exec'd, dash's SIGCHLD handling reaped the child, so no zombie was left and ps printed nothing. Kill the child only after sh has exec'd into node, which never reaps an inherited child, and print its pid once SIGCHLD confirms it is a zombie.
1 parent 069e63b commit b3d651a

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,26 +1451,28 @@ server.listen(Number(flag("--port")), "127.0.0.1", () => {
14511451

14521452
// A crashed server whose parent never reaps it stays a zombie, which
14531453
// kill -0 still reports. Its pid file and runtime record keep naming it.
1454+
// The child is killed only after sh has exec'd into node, which never
1455+
// reaps it (sh would), and its pid is printed once SIGCHLD confirms the
1456+
// zombie exists.
14541457
const parent = yield* spawner.spawn(
1455-
ChildProcess.make("sh", ["-c", "sleep 600 & echo $!; exec sleep 600"]),
1458+
ChildProcess.make("sh", [
1459+
"-c",
1460+
'sleep 600 & exec node -e "$1" "$!"',
1461+
"sh",
1462+
[
1463+
"const child = Number(process.argv[1]);",
1464+
'process.on("SIGCHLD", () => console.log(child));',
1465+
'process.kill(child, "SIGKILL");',
1466+
"setInterval(() => {}, 1 << 30);",
1467+
].join("\n"),
1468+
]),
14561469
);
14571470
const zombie = Number(
14581471
Option.getOrThrow(
14591472
yield* parent.stdout.pipe(Stream.decodeText(), Stream.splitLines, Stream.runHead),
14601473
),
14611474
);
1462-
process.kill(zombie, "SIGKILL");
1463-
// SIGKILL delivery is asynchronous; establish the zombie fixture before launching.
1464-
const zombieState = yield* runScript(home, [
1465-
"-c",
1466-
`attempt=0; while [ "$attempt" -lt 100 ]; do
1467-
state=$(ps -o stat= -p ${zombie}) || exit 1
1468-
case "$state" in *Z*) printf '%s\\n' "$state"; exit 0;; esac
1469-
attempt=$((attempt + 1))
1470-
done; printf '%s\\n' "$state"; exit 1`,
1471-
]);
1472-
assert.equal(zombieState.exitCode, 0, zombieState.stdout);
1473-
assert.include(zombieState.stdout, "Z");
1475+
assert.include((yield* runScript(home, ["-c", `ps -o stat= -p ${zombie}`])).stdout, "Z");
14741476
yield* fs.writeFileString(`${stateDir}/pid`, `${zombie}\n`);
14751477
yield* fs.writeFileString(
14761478
`${home}/.t3/userdata/server-runtime.json`,

0 commit comments

Comments
 (0)