Skip to content

Commit f978f29

Browse files
committed
fix(ssh): preserve external server ownership during updates
1 parent dd5b540 commit f978f29

2 files changed

Lines changed: 75 additions & 8 deletions

File tree

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - executes the generated POSIX launcher against an isolated filesystem fixture.
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFS from "node:fs";
4+
import * as NodeOS from "node:os";
5+
import * as NodePath from "node:path";
16
import { assert, describe, it } from "@effect/vitest";
27
import * as NodeServices from "@effect/platform-node/NodeServices";
38
import * as NetService from "@t3tools/shared/Net";
@@ -82,6 +87,68 @@ function commandArgs(command: ChildProcess.Command): ReadonlyArray<string> {
8287
const ARCHIVE = { archiveVersion: "1.2.3-preview.20260911.4" } as const;
8388

8489
describe("ssh tunnel scripts", () => {
90+
it.each(["unavailable", "recovering", "discovered", "missing-port", "fresh"] as const)(
91+
"handles SSH launch with %s external service state",
92+
(scenario) => {
93+
const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-owner-"));
94+
try {
95+
const state = NodePath.join(home, ".t3/ssh-launch/test");
96+
const runtime = NodePath.join(home, ".t3/runtime/versions", ARCHIVE.archiveVersion);
97+
NodeFS.mkdirSync(state, { recursive: true });
98+
NodeFS.mkdirSync(runtime, { recursive: true });
99+
if (scenario !== "discovered" && scenario !== "fresh") {
100+
NodeFS.writeFileSync(NodePath.join(state, "managed"), "external\n");
101+
if (scenario !== "missing-port")
102+
NodeFS.writeFileSync(NodePath.join(state, "port"), "3773\n");
103+
}
104+
NodeFS.writeFileSync(NodePath.join(runtime, ".install-complete"), ARCHIVE.archiveVersion);
105+
NodeFS.writeFileSync(
106+
NodePath.join(runtime, "t3"),
107+
`#!/bin/sh
108+
case "$1 $2" in
109+
'--version ') exit 0 ;;
110+
'__ssh-helper runtime-port')
111+
${scenario === "discovered" ? `printf '${process.pid} 3773'` : "exit 1"} ;;
112+
'__ssh-helper wait-ready')
113+
printf '%s\\n' "$4" >> "$HOME/probes"
114+
${scenario === "fresh" ? "exit 0" : scenario === "recovering" ? '[ "$4" -gt 2000 ]' : "exit 1"} ;;
115+
'__ssh-helper pick-port') printf 'pick-port\\n' >> "$HOME/spawns"; printf '3774' ;;
116+
'serve --host') printf 'serve\\n' >> "$HOME/spawns" ;;
117+
*) exit 2 ;;
118+
esac
119+
`,
120+
{ mode: 0o700 },
121+
);
122+
const result = NodeChildProcess.spawnSync("sh", ["-s", "--", "test"], {
123+
input: SshTunnel.buildRemoteLaunchScript(ARCHIVE),
124+
env: { ...process.env, HOME: home },
125+
encoding: "utf8",
126+
timeout: 10_000,
127+
});
128+
assert.isUndefined(result.error);
129+
if (scenario === "fresh") {
130+
assert.equal(result.status, 0, result.stderr);
131+
assert.deepEqual(JSON.parse(result.stdout), { remotePort: 3774, serverKind: "managed" });
132+
return;
133+
}
134+
assert.isFalse(NodeFS.existsSync(NodePath.join(home, "spawns")), result.stderr);
135+
assert.equal(
136+
NodeFS.readFileSync(NodePath.join(state, "managed"), "utf8").trim(),
137+
"external",
138+
);
139+
if (scenario === "recovering") {
140+
assert.equal(result.status, 0, result.stderr);
141+
assert.deepEqual(JSON.parse(result.stdout), { remotePort: 3773, serverKind: "external" });
142+
} else {
143+
assert.equal(result.status, 1);
144+
assert.include(result.stderr, "external T3 server");
145+
}
146+
} finally {
147+
NodeFS.rmSync(home, { recursive: true, force: true });
148+
}
149+
},
150+
);
151+
85152
it("rejects archive versions that are not a single exact version segment", () => {
86153
for (const archiveVersion of [
87154
"../other",

‎packages/ssh/src/tunnel.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,20 +666,20 @@ elif [ -n "$DEFAULT_REMOTE_PORT" ]; then
666666
REMOTE_MANAGED="external"
667667
fi
668668
else
669-
REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
670-
REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)"
671-
REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
669+
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
670+
REMOTE_MANAGED="external"
671+
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
672+
printf 'external\\n' >"$MANAGED_FILE"
672673
fi
673674
fi
674675
if [ "$REMOTE_MANAGED" = "external" ]; then
675676
# A server adopted above has just answered. While it is still alive, probing
676677
# it again could catch a momentary stall and launch a second server beside it.
677678
if server_running "$ADOPTED_PID"; then
678679
:
679-
elif [ -z "$REMOTE_PORT" ] || ! wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
680-
REMOTE_PID=""
681-
REMOTE_PORT=""
682-
REMOTE_MANAGED=""
680+
elif [ -z "$REMOTE_PORT" ] || ! wait_ready "@@T3_READY_TIMEOUT_MS@@"; then
681+
printf 'The external T3 server did not become ready. Retry after its update or restart finishes; no replacement server was started.\\n' >&2
682+
exit 1
683683
fi
684684
elif [ -n "$REMOTE_PID" ] && [ -n "$REMOTE_PORT" ] && server_running "$REMOTE_PID"; then
685685
# Reconnect owns the local tunnel, not the lifetime of an already running
@@ -833,7 +833,7 @@ export function buildRemoteNodeEnvScript(input?: RemoteT3RunnerOptions): string
833833
);
834834
}
835835

836-
function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string {
836+
export function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string {
837837
return applyScriptPlaceholders(REMOTE_LAUNCH_SCRIPT, {
838838
T3_ARCHIVE_MODE: isNodeScriptRunner(input) ? "0" : "1",
839839
T3_NODE_ENV_SCRIPT: buildRemoteNodeEnvScript(input),

0 commit comments

Comments
 (0)