Skip to content

Commit ed54cbb

Browse files
Gooder24claude
andcommitted
fix(ssh): editing a connection no longer strands its remote server on an old version
The SSH launcher keys its ownership records by alias, host, user, and port. After a connection's alias changed, the new key adopted the still-running server it had launched as external, so app updates never replaced it, and pairing through the newer runner was rejected with a 401. The launcher now takes over a default-home server that another state key recorded as managed, and replaces it when that key's runner differs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 92a6b43 commit ed54cbb

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,36 @@ server.listen(Number(flag("--port")), "127.0.0.1", () => {
14391439
60_000,
14401440
);
14411441

1442+
it.effect.skipIf(windowsHost)(
1443+
"keeps managing its server after the connection's state key changes",
1444+
() =>
1445+
Effect.gen(function* () {
1446+
const fs = yield* FileSystem.FileSystem;
1447+
const { home, stateDir, launch, readManagedPid, runLaunch, launchManaged } =
1448+
yield* prepareLaunch;
1449+
const { launched, pid } = yield* launchManaged;
1450+
const renamedStateDir = `${home}/.t3/ssh-launch/renamed-state`;
1451+
1452+
const renamed = yield* runScript(home, [launch, "renamed-state"]);
1453+
assert.equal(renamed.exitCode, 0, renamed.stderr);
1454+
assert.deepEqual(launchResult(renamed.stdout), launched);
1455+
assert.equal((yield* fs.readFileString(`${renamedStateDir}/pid`)).trim(), `${pid}`);
1456+
assert.isFalse(yield* fs.exists(`${stateDir}/managed`));
1457+
assert.isTrue(isAlive(pid));
1458+
1459+
// The server came from the renamed connection's runner, so a different
1460+
// runner here is an update.
1461+
yield* fs.writeFileString(`${renamedStateDir}/run-t3.sh`, "# previous runner\n");
1462+
assert.equal((yield* runLaunch).serverKind, "managed");
1463+
const replacementPid = yield* readManagedPid;
1464+
assert.notEqual(replacementPid, pid);
1465+
assert.isFalse(isAlive(pid));
1466+
assert.isTrue(isAlive(replacementPid));
1467+
assert.isFalse(yield* fs.exists(`${renamedStateDir}/managed`));
1468+
}).pipe(Effect.provide(NodeServices.layer)),
1469+
60_000,
1470+
);
1471+
14421472
it.effect.skipIf(windowsHost)(
14431473
"replaces a managed server that exited but was not reaped",
14441474
() =>

‎packages/ssh/src/tunnel.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,19 @@ try {
635635
}
636636
NODE
637637
}
638+
# Editing a connection's alias, user, or port gives it a new state key, while
639+
# the server it launched keeps running from the default home. Prints the state
640+
# directory that recorded PID $1 as managed, so that server stays managed.
641+
launcher_state_dir_for_pid() {
642+
for OTHER_STATE_DIR in "$HOME/.t3/ssh-launch"/*; do
643+
[ "$OTHER_STATE_DIR" != "$STATE_DIR" ] || continue
644+
[ "$(cat "$OTHER_STATE_DIR/managed" 2>/dev/null || true)" = "managed" ] || continue
645+
[ "$(cat "$OTHER_STATE_DIR/pid" 2>/dev/null || true)" = "$1" ] || continue
646+
printf '%s\\n' "$OTHER_STATE_DIR"
647+
return 0
648+
done
649+
return 1
650+
}
638651
REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
639652
REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)"
640653
REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
@@ -652,6 +665,22 @@ fi
652665
if [ -n "$DEFAULT_REMOTE_PORT" ] && [ "$REMOTE_MANAGED" = "managed" ] && [ "$DEFAULT_RUNTIME_PID" = "$REMOTE_PID" ]; then
653666
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
654667
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
668+
elif [ -n "$DEFAULT_REMOTE_PORT" ] && PREVIOUS_STATE_DIR="$(launcher_state_dir_for_pid "$DEFAULT_RUNTIME_PID")"; then
669+
# Adopting it as external would pin it to the old runner forever, and pairing
670+
# with this runner against an older server fails. It runs the runner its own
671+
# state wrote, not ours, so only that one decides whether this is an update.
672+
if cmp -s "$PREVIOUS_STATE_DIR/run-t3.sh" "$RUNNER_FILE"; then
673+
RUNNER_CHANGED=0
674+
else
675+
RUNNER_CHANGED=1
676+
fi
677+
REMOTE_PID="$DEFAULT_RUNTIME_PID"
678+
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
679+
REMOTE_MANAGED="managed"
680+
printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
681+
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
682+
printf 'managed\\n' >"$MANAGED_FILE"
683+
rm -f "$PREVIOUS_STATE_DIR/pid" "$PREVIOUS_STATE_DIR/managed"
655684
elif [ -n "$DEFAULT_REMOTE_PORT" ]; then
656685
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
657686
if wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then

0 commit comments

Comments
 (0)