Skip to content

Commit 39394b6

Browse files
puccezclaude
andcommitted
fix(ssh): reconnecting no longer restarts the managed remote server
The managed server runs on the default T3 home, so it writes the default runtime file too. On every reconnect the launch script read that file, took the managed server for an external one, stopped it, and launched a new one, which ended every provider session running on the remote. When the default runtime file names the PID this launcher recorded as managed, treat it as our own server and take its port from that file, so a stale remembered port cannot hide it either. Without the managed marker the server is adopted as external, as before. Adopting an external server no longer falls back to stopping the PID the runtime file names, and a server that just answered adoption is not probed again while it is alive, so a momentary stall cannot launch a second server on the same home beside it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3857132 commit 39394b6

2 files changed

Lines changed: 305 additions & 38 deletions

File tree

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

Lines changed: 280 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import * as NodeNet from "node:net";
23
import * as NodeServices from "@effect/platform-node/NodeServices";
34
import * as NetService from "@t3tools/shared/Net";
45
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -8,6 +9,7 @@ import * as Effect from "effect/Effect";
89
import * as Fiber from "effect/Fiber";
910
import * as FileSystem from "effect/FileSystem";
1011
import * as Layer from "effect/Layer";
12+
import * as Option from "effect/Option";
1113
import * as Result from "effect/Result";
1214
import * as Sink from "effect/Sink";
1315
import * as Stream from "effect/Stream";
@@ -291,7 +293,10 @@ describe("ssh tunnel scripts", () => {
291293
assert.include(launch, "resolve_default_runtime_port()");
292294
assert.include(launch, 'DEFAULT_RUNTIME_INFO="$(resolve_default_runtime_port');
293295
assert.include(launch, "if (!Number.isInteger(pid) || pid <= 0 || !Number.isInteger(port))");
294-
assert.include(launch, 'PID_TO_STOP="${REMOTE_PID:-$DEFAULT_RUNTIME_PID}"');
296+
assert.include(
297+
launch,
298+
'if [ -n "$DEFAULT_REMOTE_PORT" ] && [ "$REMOTE_MANAGED" = "managed" ] && [ "$DEFAULT_RUNTIME_PID" = "$REMOTE_PID" ]; then',
299+
);
295300
assert.include(launch, 'REMOTE_PORT="$DEFAULT_REMOTE_PORT"');
296301
assert.include(launch, 'rm -f "$PID_FILE"');
297302
assert.include(launch, "printf 'external\\n' >\"$MANAGED_FILE\"");
@@ -687,6 +692,39 @@ describe("ssh tunnel scripts", () => {
687692
);
688693
});
689694

695+
// Runs a generated script with an isolated HOME, as an SSH remote would.
696+
const runScript = (home: string, args: ReadonlyArray<string>) =>
697+
Effect.gen(function* () {
698+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
699+
const child = yield* spawner.spawn(
700+
ChildProcess.make("sh", args, {
701+
env: { PATH: process.env.PATH ?? "", HOME: home },
702+
extendEnv: false,
703+
}),
704+
);
705+
const [stdout, stderr, exitCode] = yield* Effect.all(
706+
[
707+
child.stdout.pipe(
708+
Stream.decodeText(),
709+
Stream.runFold(
710+
() => "",
711+
(acc, chunk) => acc + chunk,
712+
),
713+
),
714+
child.stderr.pipe(
715+
Stream.decodeText(),
716+
Stream.runFold(
717+
() => "",
718+
(acc, chunk) => acc + chunk,
719+
),
720+
),
721+
child.exitCode.pipe(Effect.map(Number)),
722+
],
723+
{ concurrency: "unbounded" },
724+
);
725+
return { stdout, stderr, exitCode };
726+
});
727+
690728
// The archive runner is generated shell; string assertions cannot prove the
691729
// lock excludes concurrent installers. Run the real script against a tiny
692730
// fake archive served from a file:// mirror.
@@ -696,37 +734,7 @@ describe("archive runner script", () => {
696734
const windowsHost = hostPlatform === "win32";
697735
const archiveVersion = "1.2.3-preview.20260911.4";
698736

699-
const runRunner = (home: string, runner: string) =>
700-
Effect.gen(function* () {
701-
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
702-
const child = yield* spawner.spawn(
703-
ChildProcess.make("sh", [runner, "--version"], {
704-
env: { PATH: process.env.PATH ?? "", HOME: home },
705-
extendEnv: false,
706-
}),
707-
);
708-
const [stdout, stderr, exitCode] = yield* Effect.all(
709-
[
710-
child.stdout.pipe(
711-
Stream.decodeText(),
712-
Stream.runFold(
713-
() => "",
714-
(acc, chunk) => acc + chunk,
715-
),
716-
),
717-
child.stderr.pipe(
718-
Stream.decodeText(),
719-
Stream.runFold(
720-
() => "",
721-
(acc, chunk) => acc + chunk,
722-
),
723-
),
724-
child.exitCode.pipe(Effect.map(Number)),
725-
],
726-
{ concurrency: "unbounded" },
727-
);
728-
return { stdout, stderr, exitCode };
729-
});
737+
const runRunner = (home: string, runner: string) => runScript(home, [runner, "--version"]);
730738

731739
// A fake "executable" that answers --version, packed the way the release
732740
// workflow packs the real archive: one top-level directory named after the
@@ -799,3 +807,243 @@ describe("archive runner script", () => {
799807
60_000,
800808
);
801809
});
810+
811+
// Reuse decisions also live in generated shell. Drive the real launch script
812+
// against a fake server that answers readiness probes and writes the default
813+
// runtime file the way `t3 serve` does.
814+
describe("launch script", () => {
815+
const windowsHost = HostProcess.Platform.defaultValue() === "win32";
816+
const FAKE_SERVER = `const fs = require("node:fs");
817+
const http = require("node:http");
818+
const path = require("node:path");
819+
const args = process.argv.slice(2);
820+
const flag = (name) => args[args.indexOf(name) + 1];
821+
// --answer-once stalls every request after the first, like a busy server;
822+
// --exit-after-answer exits right after the first answer.
823+
const answerOnce = args.includes("--answer-once");
824+
const exitAfterAnswer = args.includes("--exit-after-answer");
825+
let answered = false;
826+
const server = http.createServer((_request, response) => {
827+
if (answerOnce && answered) return;
828+
answered = true;
829+
response.end("ok", () => {
830+
if (exitAfterAnswer) process.exit(0);
831+
});
832+
});
833+
server.listen(Number(flag("--port")), "127.0.0.1", () => {
834+
const port = server.address().port;
835+
const userdata = path.join(flag("--base-dir"), "userdata");
836+
fs.mkdirSync(userdata, { recursive: true });
837+
fs.writeFileSync(
838+
path.join(userdata, "server-runtime.json"),
839+
JSON.stringify({ version: 1, pid: process.pid, port, origin: "http://127.0.0.1:" + port }),
840+
);
841+
console.log(port);
842+
});
843+
`;
844+
845+
const isAlive = (pid: number) => {
846+
try {
847+
process.kill(pid, 0);
848+
return true;
849+
} catch {
850+
return false;
851+
}
852+
};
853+
854+
const freeLoopbackPort = Effect.callback<number>((resume) => {
855+
const server = NodeNet.createServer();
856+
server.listen(0, "127.0.0.1", () => {
857+
const address = server.address();
858+
server.close(() =>
859+
resume(Effect.succeed(typeof address === "object" && address ? address.port : 0)),
860+
);
861+
});
862+
});
863+
864+
// Whether anything still accepts connections on the port. Unlike a PID
865+
// check, this cannot mistake an unreaped, exited server for a running one.
866+
const isListening = (port: number) =>
867+
Effect.callback<boolean>((resume) => {
868+
const socket = NodeNet.connect(port, "127.0.0.1");
869+
socket.once("connect", () => {
870+
socket.destroy();
871+
resume(Effect.succeed(true));
872+
});
873+
socket.once("error", () => resume(Effect.succeed(false)));
874+
});
875+
876+
const launchResult = (stdout: string) => JSON.parse(stdout.trim().split("\n").at(-1) ?? "");
877+
878+
// Writes the launch script for a fake server into a temporary HOME. The
879+
// seeded remembered port keeps the test off the default 3773, which a live
880+
// T3 on this machine may own.
881+
const prepareLaunch = Effect.gen(function* () {
882+
const fs = yield* FileSystem.FileSystem;
883+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-launch-script-" });
884+
const home = `${root}/home`;
885+
const stateDir = `${home}/.t3/ssh-launch/test-state`;
886+
const fakeServer = `${root}/fake-t3.cjs`;
887+
const launch = `${root}/launch.sh`;
888+
yield* fs.makeDirectory(stateDir, { recursive: true });
889+
yield* fs.writeFileString(`${stateDir}/port`, `${yield* freeLoopbackPort}\n`);
890+
yield* fs.writeFileString(fakeServer, FAKE_SERVER);
891+
yield* fs.writeFileString(
892+
launch,
893+
SshTunnel.buildRemoteLaunchScript({ nodeScriptPath: fakeServer }),
894+
);
895+
const readManagedPid = fs
896+
.readFileString(`${stateDir}/pid`)
897+
.pipe(Effect.map((pid) => Number(pid.trim())));
898+
// Launched servers outlive the script, so stop each PID the script
899+
// recorded, including one it recorded just before a failed assertion.
900+
const launchedPids: Array<number> = [];
901+
yield* Effect.addFinalizer(() =>
902+
readManagedPid.pipe(
903+
Effect.map((pid) => [...launchedPids, pid]),
904+
Effect.orElseSucceed(() => launchedPids),
905+
Effect.map((pids) => {
906+
for (const pid of pids) {
907+
if (isAlive(pid)) process.kill(pid);
908+
}
909+
}),
910+
),
911+
);
912+
const runLaunch = runScript(home, [launch, "test-state"]).pipe(
913+
Effect.map((result) => {
914+
assert.equal(result.exitCode, 0, result.stderr);
915+
return launchResult(result.stdout);
916+
}),
917+
);
918+
const launchManaged = Effect.gen(function* () {
919+
const launched = yield* runLaunch;
920+
const pid = yield* readManagedPid;
921+
launchedPids.push(pid);
922+
assert.equal(launched.serverKind, "managed");
923+
return { launched, pid };
924+
});
925+
// Starts a separate server on the default home, as a service would, and
926+
// returns the port it reports. An unreaped server runs under a parent that
927+
// never waits on it, so once it exits it lingers as a zombie.
928+
const startDefaultHomeServer = (
929+
flags: ReadonlyArray<string>,
930+
options?: { readonly unreaped?: boolean },
931+
) =>
932+
Effect.gen(function* () {
933+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
934+
const args = [fakeServer, "serve", "--port", "0", "--base-dir", `${home}/.t3`, ...flags];
935+
const server = yield* spawner.spawn(
936+
options?.unreaped
937+
? ChildProcess.make("sh", ["-c", 'node "$@" & exec sleep 600', "sh", ...args])
938+
: ChildProcess.make("node", args),
939+
);
940+
const port = yield* server.stdout.pipe(
941+
Stream.decodeText(),
942+
Stream.splitLines,
943+
Stream.runHead,
944+
);
945+
return Number(Option.getOrThrow(port));
946+
});
947+
return { stateDir, readManagedPid, runLaunch, launchManaged, startDefaultHomeServer };
948+
});
949+
950+
it.effect.skipIf(windowsHost)(
951+
"reuses its own managed server and yields only to a different default-home server",
952+
() =>
953+
Effect.gen(function* () {
954+
const fs = yield* FileSystem.FileSystem;
955+
const { stateDir, readManagedPid, runLaunch, launchManaged, startDefaultHomeServer } =
956+
yield* prepareLaunch;
957+
const { launched, pid: managedPid } = yield* launchManaged;
958+
959+
// The managed server wrote the default runtime file itself, so a
960+
// reconnect must keep it rather than stop it as an external server.
961+
assert.deepEqual(yield* runLaunch, launched);
962+
assert.equal(yield* readManagedPid, managedPid);
963+
assert.isTrue(isAlive(managedPid));
964+
965+
// A remembered port that is stale must not hide our own server: the
966+
// runtime file names its port.
967+
yield* fs.writeFileString(`${stateDir}/port`, `${yield* freeLoopbackPort}\n`);
968+
assert.deepEqual(yield* runLaunch, launched);
969+
assert.equal(yield* readManagedPid, managedPid);
970+
assert.equal(
971+
(yield* fs.readFileString(`${stateDir}/port`)).trim(),
972+
`${launched.remotePort}`,
973+
);
974+
975+
// A different server on the default home, such as a service, still
976+
// takes over from the managed one.
977+
const servicePort = yield* startDefaultHomeServer([]);
978+
assert.deepEqual(yield* runLaunch, {
979+
remotePort: servicePort,
980+
serverKind: "external",
981+
});
982+
assert.isFalse(yield* isListening(launched.remotePort));
983+
assert.isFalse(yield* fs.exists(`${stateDir}/pid`));
984+
}).pipe(Effect.provide(NodeServices.layer)),
985+
60_000,
986+
);
987+
988+
it.effect.skipIf(windowsHost)(
989+
"adopts a server it has no managed record for instead of restarting it",
990+
() =>
991+
Effect.gen(function* () {
992+
const fs = yield* FileSystem.FileSystem;
993+
const { stateDir, launchManaged, runLaunch } = yield* prepareLaunch;
994+
const { launched, pid } = yield* launchManaged;
995+
996+
// An interrupted launch, or a recycled PID, can leave a PID that matches
997+
// the runtime file without the managed marker. A changed runner must not
998+
// turn that into a restart of a server this launcher cannot claim.
999+
yield* fs.remove(`${stateDir}/managed`);
1000+
yield* fs.writeFileString(`${stateDir}/run-t3.sh`, "# superseded runner\n");
1001+
assert.deepEqual(yield* runLaunch, {
1002+
remotePort: launched.remotePort,
1003+
serverKind: "external",
1004+
});
1005+
assert.isTrue(isAlive(pid));
1006+
}).pipe(Effect.provide(NodeServices.layer)),
1007+
60_000,
1008+
);
1009+
1010+
it.effect.skipIf(windowsHost)(
1011+
"keeps an adopted default-home server without launching another beside it",
1012+
() =>
1013+
Effect.gen(function* () {
1014+
const fs = yield* FileSystem.FileSystem;
1015+
const { stateDir, runLaunch, startDefaultHomeServer } = yield* prepareLaunch;
1016+
// Ownership files from an earlier managed launch that lost its PID.
1017+
yield* fs.writeFileString(`${stateDir}/managed`, "managed\n");
1018+
const servicePort = yield* startDefaultHomeServer(["--answer-once"]);
1019+
1020+
// The server answers adoption and then stalls. A second readiness
1021+
// probe would launch another server on the same home.
1022+
assert.deepEqual(yield* runLaunch, { remotePort: servicePort, serverKind: "external" });
1023+
assert.isFalse(yield* fs.exists(`${stateDir}/pid`));
1024+
}).pipe(Effect.provide(NodeServices.layer)),
1025+
60_000,
1026+
);
1027+
1028+
it.effect.skipIf(windowsHost)(
1029+
"launches a replacement when the adopted server exits during the handover",
1030+
() =>
1031+
Effect.gen(function* () {
1032+
const { readManagedPid, runLaunch, launchManaged, startDefaultHomeServer } =
1033+
yield* prepareLaunch;
1034+
const { pid: previousPid } = yield* launchManaged;
1035+
yield* startDefaultHomeServer(["--exit-after-answer"], { unreaped: true });
1036+
1037+
// The service answers adoption and then exits while the managed
1038+
// server is being stopped, staying a zombie, so the script must launch
1039+
// a new server rather than return the dead one. The freed port may be
1040+
// reused.
1041+
const result = yield* runLaunch;
1042+
assert.equal(result.serverKind, "managed");
1043+
const replacementPid = yield* readManagedPid;
1044+
assert.notEqual(replacementPid, previousPid);
1045+
assert.isTrue(isAlive(replacementPid));
1046+
}).pipe(Effect.provide(NodeServices.layer)),
1047+
60_000,
1048+
);
1049+
});

0 commit comments

Comments
 (0)