Skip to content

Commit 1650848

Browse files
PR Batch Testerclaude
andcommitted
fix(ssh): do not report a reaped server as still running
server_running treated an empty ps result as "running" whenever kill -0 had just succeeded. If the server's reaper removed it between the two probes, wait_for_pid_exit's final check reported a stopped server as alive and kept its ownership files. Ask kill -0 again after ps, so a PID that is gone counts as stopped while a missing ps still trusts kill -0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fe3e259 commit 1650848

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,76 @@ server.on("listening", () => process.stdout.end("ready\\n"));
743743
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
744744
);
745745

746+
it.live("treats a server reaped between liveness probes as stopped", () =>
747+
Effect.gen(function* () {
748+
const { fs, fakeServer, run, stateDir, writeVariant, lockPath } = yield* prepareLaunch;
749+
let launchedPid: number | undefined;
750+
yield* Effect.addFinalizer(() =>
751+
Effect.sync(() => {
752+
if (launchedPid !== undefined && isAlive(launchedPid))
753+
process.kill(launchedPid, "SIGKILL");
754+
}),
755+
);
756+
yield* fs.writeFileString(
757+
fakeServer,
758+
`${FAKE_SERVER}
759+
process.on("SIGTERM", () => {});
760+
server.on("listening", () => process.stdout.end("ready\\n"));
761+
`,
762+
);
763+
const ready = yield* Deferred.make<void>();
764+
// The final probe's kill -0 still finds the server, which is then killed
765+
// and reaped before ps reads its state, as an asynchronous reaper can.
766+
const script = yield* writeVariant("reaped-between-probes", [
767+
['>>"$LOG_FILE" 2>&1 < /dev/null &', '2>"$LOG_FILE" < /dev/null &'],
768+
[
769+
' REMOTE_PID="$!"',
770+
` REMOTE_PID="$!"
771+
printf 'launched:%s\\n' "$REMOTE_PID"
772+
read -r release
773+
wait_ready() { return 1; }
774+
kill() {
775+
if [ "$1" = "-0" ] && [ "$2" = "\${REAP_ON_PROBE:-}" ]; then
776+
REAP_ON_PROBE=""
777+
command kill -0 "$2" || return
778+
command kill -KILL "$2"
779+
wait "$2" || true
780+
return 0
781+
fi
782+
command kill "$@"
783+
}`,
784+
],
785+
[" WAIT_COUNT=$((WAIT_COUNT + 1))\n sleep 0.1", " WAIT_COUNT=20"],
786+
[
787+
` if server_running "$PID_TO_WAIT"; then\n printf 'Remote T3 server with PID %s did not stop.`,
788+
` REAP_ON_PROBE="$PID_TO_WAIT"\n if server_running "$PID_TO_WAIT"; then\n printf 'Remote T3 server with PID %s did not stop.`,
789+
],
790+
]);
791+
const result = yield* run(
792+
"alias",
793+
{
794+
onLine: (line) =>
795+
Effect.gen(function* () {
796+
if (line.startsWith("launched:"))
797+
launchedPid = Number(line.slice("launched:".length));
798+
if (line === "ready") yield* Deferred.succeed(ready, undefined);
799+
}),
800+
release: Deferred.await(ready),
801+
},
802+
script,
803+
);
804+
assert.equal(result.exitCode, 1, result.stderr);
805+
assert.include(result.stderr, "did not become ready");
806+
assert.notInclude(result.stderr, "did not stop");
807+
if (launchedPid === undefined) assert.fail("the launch did not report its spawned PID");
808+
assert.isFalse(isAlive(launchedPid));
809+
for (const file of ["pid", "port", "managed", "process-start"]) {
810+
assert.isFalse(yield* fs.exists(`${stateDir("alias")}/${file}`), file);
811+
}
812+
assert.isFalse(yield* fs.exists(lockPath));
813+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
814+
);
815+
746816
it.live("does not take over a server whose recorded process identity differs", () =>
747817
Effect.gen(function* () {
748818
const { fs, stateDir, readManagedPid, launch } = yield* prepareLaunch;

‎packages/ssh/src/tunnel.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,11 +717,13 @@ wait_for_pid_exit() {
717717
}
718718
# kill -0 also succeeds for a server that exited but was not reaped yet, so
719719
# a zombie does not count as running. Without ps to tell, trust kill -0.
720+
# A reaper can remove it between the two probes, so ask kill -0 again.
720721
server_running() {
721722
[ -n "$1" ] && kill -0 "$1" 2>/dev/null || return 1
722723
case "$(ps -o stat= -p "$1" 2>/dev/null || true)" in
723724
*Z*) return 1 ;;
724725
esac
726+
kill -0 "$1" 2>/dev/null
725727
}
726728
resolve_default_runtime_port() {
727729
if [ "$T3_ARCHIVE_MODE" = "1" ]; then

0 commit comments

Comments
 (0)