Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/ssh/src/tunnel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,36 @@ server.listen(Number(flag("--port")), "127.0.0.1", () => {
60_000,
);

it.effect.skipIf(windowsHost)(
"keeps managing its server after the connection's state key changes",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const { home, stateDir, launch, readManagedPid, runLaunch, launchManaged } =
yield* prepareLaunch;
const { launched, pid } = yield* launchManaged;
const renamedStateDir = `${home}/.t3/ssh-launch/renamed-state`;

const renamed = yield* runScript(home, [launch, "renamed-state"]);
assert.equal(renamed.exitCode, 0, renamed.stderr);
assert.deepEqual(launchResult(renamed.stdout), launched);
assert.equal((yield* fs.readFileString(`${renamedStateDir}/pid`)).trim(), `${pid}`);
assert.isFalse(yield* fs.exists(`${stateDir}/managed`));
assert.isTrue(isAlive(pid));

// The server came from the renamed connection's runner, so a different
// runner here is an update.
yield* fs.writeFileString(`${renamedStateDir}/run-t3.sh`, "# previous runner\n");
assert.equal((yield* runLaunch).serverKind, "managed");
const replacementPid = yield* readManagedPid;
assert.notEqual(replacementPid, pid);
assert.isFalse(isAlive(pid));
assert.isTrue(isAlive(replacementPid));
assert.isFalse(yield* fs.exists(`${renamedStateDir}/managed`));
}).pipe(Effect.provide(NodeServices.layer)),
60_000,
);

it.effect.skipIf(windowsHost)(
"replaces a managed server that exited but was not reaped",
() =>
Expand Down
29 changes: 29 additions & 0 deletions packages/ssh/src/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,19 @@ try {
}
NODE
}
# Editing a connection's alias, user, or port gives it a new state key, while
# the server it launched keeps running from the default home. Prints the state
# directory that recorded PID $1 as managed, so that server stays managed.
launcher_state_dir_for_pid() {
for OTHER_STATE_DIR in "$HOME/.t3/ssh-launch"/*; do
[ "$OTHER_STATE_DIR" != "$STATE_DIR" ] || continue
[ "$(cat "$OTHER_STATE_DIR/managed" 2>/dev/null || true)" = "managed" ] || continue
[ "$(cat "$OTHER_STATE_DIR/pid" 2>/dev/null || true)" = "$1" ] || continue
printf '%s\\n' "$OTHER_STATE_DIR"
return 0
done
return 1
}
REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)"
REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
Expand All @@ -652,6 +665,22 @@ fi
if [ -n "$DEFAULT_REMOTE_PORT" ] && [ "$REMOTE_MANAGED" = "managed" ] && [ "$DEFAULT_RUNTIME_PID" = "$REMOTE_PID" ]; then
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
elif [ -n "$DEFAULT_REMOTE_PORT" ] && PREVIOUS_STATE_DIR="$(launcher_state_dir_for_pid "$DEFAULT_RUNTIME_PID")"; then
# Adopting it as external would pin it to the old runner forever, and pairing
# with this runner against an older server fails. It runs the runner its own
# state wrote, not ours, so only that one decides whether this is an update.
if cmp -s "$PREVIOUS_STATE_DIR/run-t3.sh" "$RUNNER_FILE"; then
RUNNER_CHANGED=0
else
RUNNER_CHANGED=1
fi
REMOTE_PID="$DEFAULT_RUNTIME_PID"
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
REMOTE_MANAGED="managed"
printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
printf 'managed\\n' >"$MANAGED_FILE"
rm -f "$PREVIOUS_STATE_DIR/pid" "$PREVIOUS_STATE_DIR/managed"
elif [ -n "$DEFAULT_REMOTE_PORT" ]; then
REMOTE_PORT="$DEFAULT_REMOTE_PORT"
if wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
Expand Down
Loading