@devries-cameron20 / t3code Mirror · read-only / commits / d5dc111599
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

fix(ssh): reconnect and disconnect ignore a remote pid file that no longer names the server (#26963)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 11, 2026 at 11:52 UTC d5dc11159987066ea0d7886d60bbef82ab277c87
2 files changed +175 -4
packages/ssh/src/tunnel.test.ts
+153
@@ -7,6 +7,7 @@ import * as NodePath from "node:path";
7 import { assert, describe, it } from "@effect/vitest";
8 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
9 import * as NodeServices from "@effect/platform-node/NodeServices";
10 +import * as HostProcess from "@t3tools/shared/HostProcess";
11 import * as NetService from "@t3tools/shared/Net";
12 import * as Clock from "effect/Clock";
13 import * as Deferred from "effect/Deferred";
@@ -15,6 +16,7 @@ import * as Effect from "effect/Effect";
16 import * as Fiber from "effect/Fiber";
17 import * as FileSystem from "effect/FileSystem";
18 import * as Layer from "effect/Layer";
19 +import * as Option from "effect/Option";
20 import * as Path from "effect/Path";
21 import * as PlatformError from "effect/PlatformError";
22 import * as Schema from "effect/Schema";
@@ -770,6 +772,157 @@ describe("ssh tunnel scripts", () => {
772 );
773 });
774
775 +// On Linux kill -0 accepts a thread ID and kill <tid> signals its whole process,
776 +// but ps lists only thread-group leaders. After a reboot the managed pid file can
777 +// name a thread or a whole process that is not our server.
778 +describe.runIf(HostProcess.Platform.defaultValue() === "linux")("stale managed pid file", () => {
779 + const FAKE_SERVER = `const fs = require("node:fs");
780 +const http = require("node:http");
781 +const path = require("node:path");
782 +const args = process.argv.slice(2);
783 +const flag = (name) => args[args.indexOf(name) + 1];
784 +const server = http.createServer((_request, response) => response.end("ok"));
785 +server.listen(Number(flag("--port") ?? 0), "127.0.0.1", () => {
786 + const port = server.address().port;
787 + const base = flag("--base-dir");
788 + if (base) {
789 + fs.mkdirSync(path.join(base, "userdata"), { recursive: true });
790 + fs.writeFileSync(
791 + path.join(base, "userdata", "server-runtime.json"),
792 + JSON.stringify({ version: 1, pid: process.pid, port, origin: "http://127.0.0.1:" + port }),
793 + );
794 + }
795 + console.log(port);
796 +});
797 +`;
798 + const target = { alias: "devbox", hostname: "devbox", username: null, port: null } as const;
799 + const staleIds = ["a thread of an unrelated process", "an unrelated process"] as const;
800 +
801 + const isRunning = (pid: number) =>
802 + Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFileString(`/proc/${pid}/stat`)).pipe(
803 + Effect.map((stat) => !/^\d+ \(.*\) Z/u.test(stat)),
804 + Effect.orElseSucceed(() => false),
805 + );
806 +
807 + const prepare = (staleId: (typeof staleIds)[number]) =>
808 + Effect.gen(function* () {
809 + const fs = yield* FileSystem.FileSystem;
810 + const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
811 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-stale-pid-" });
812 + const home = `${root}/home`;
813 + const fakeServer = `${root}/fake-t3.cjs`;
814 + yield* fs.makeDirectory(home, { recursive: true });
815 + yield* fs.writeFileString(fakeServer, FAKE_SERVER);
816 + // Runs the remote command locally under the temp HOME, without -l so the test
817 + // machine's login profile cannot move HOME. setsid keeps a launched server alive
818 + // when the command's process group goes away, as sshd would.
819 + const sshAsLocalShell = ChildProcessSpawner.make((command) => {
820 + if (command._tag !== "StandardCommand" || command.command !== "ssh") {
821 + return realSpawner.spawn(command);
822 + }
823 + if (command.args.includes("-G")) return Effect.succeed(makeSuccessfulProcess(""));
824 + const remoteArgs = command.args.slice(command.args.indexOf("--") + 2);
825 + return realSpawner.spawn(
826 + ChildProcess.make("setsid", ["-w", ...remoteArgs.filter((arg) => arg !== "-l")], {
827 + ...command.options,
828 + env: { PATH: process.env.PATH ?? "", HOME: home },
829 + extendEnv: false,
830 + }),
831 + );
832 + });
833 + const holder = yield* realSpawner.spawn(ChildProcess.make("node", [fakeServer]));
834 + const holderPort = Number(
835 + Option.getOrThrow(
836 + yield* holder.stdout.pipe(Stream.decodeText(), Stream.splitLines, Stream.runHead),
837 + ),
838 + );
839 + const tid = Number(
840 + (yield* fs.readDirectory(`/proc/${holder.pid}/task`)).find(
841 + (task) => task !== `${holder.pid}`,
842 + ),
843 + );
844 + assert.isTrue(Number.isInteger(tid));
845 + return {
846 + fs,
847 + home,
848 + fakeServer,
849 + holder,
850 + holderPort,
851 + sshAsLocalShell,
852 + staleId: staleId === "an unrelated process" ? holder.pid : tid,
853 + stateDir: `${home}/.t3/ssh-launch/${yield* remoteStateKey(target)}`,
854 + };
855 + });
856 +
857 + it.live.each(staleIds)("launches a fresh server when the managed pid file names %s", (kind) =>
858 + Effect.gen(function* () {
859 + const { fs, home, fakeServer, holderPort, sshAsLocalShell, staleId, stateDir } =
860 + yield* prepare(kind);
861 + const launch = SshTunnel.launchOrReuseRemoteServer(target, undefined, {
862 + nodeScriptPath: fakeServer,
863 + }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, sshAsLocalShell));
864 + const readPid = fs
865 + .readFileString(`${stateDir}/pid`)
866 + .pipe(Effect.map((pid) => Number(pid.trim())));
867 + const launchedPids: Array<number> = [];
868 + yield* Effect.addFinalizer(() =>
869 + Effect.forEach(launchedPids, (pid) =>
870 + Effect.sync(() => {
871 + try {
872 + process.kill(pid, "SIGKILL");
873 + } catch {}
874 + }),
875 + ),
876 + );
877 +
878 + const first = yield* launch;
879 + const firstPid = yield* readPid;
880 + launchedPids.push(firstPid);
881 + assert.deepEqual(yield* launch, first);
882 + assert.equal(yield* readPid, firstPid);
883 +
884 + // Reboot: our server is gone and its recorded PID now names something that
885 + // answers on the recorded port.
886 + process.kill(firstPid, "SIGKILL");
887 + yield* fs.remove(`${home}/.t3/userdata/server-runtime.json`);
888 + yield* fs.writeFileString(`${stateDir}/pid`, `${staleId}\n`);
889 + yield* fs.writeFileString(`${stateDir}/port`, `${holderPort}\n`);
890 +
891 + const relaunched = yield* launch;
892 + const pid = yield* readPid;
893 + if (pid !== staleId) launchedPids.push(pid);
894 + assert.notEqual(
895 + relaunched.remotePort,
896 + holderPort,
897 + `the launcher kept ${kind} as its running server`,
898 + );
899 + assert.isTrue(yield* isRunning(pid));
900 + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
901 + );
902 +
903 + it.live.each(staleIds)("disconnecting does not signal %s named by the managed pid file", (kind) =>
904 + Effect.gen(function* () {
905 + const { fs, holder, sshAsLocalShell, staleId, stateDir } = yield* prepare(kind);
906 + yield* fs.makeDirectory(stateDir, { recursive: true });
907 + yield* fs.writeFileString(`${stateDir}/pid`, `${staleId}\n`);
908 + yield* fs.writeFileString(`${stateDir}/managed`, "managed\n");
909 + const layer = Layer.mergeAll(
910 + NodeServices.layer,
911 + Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, sshAsLocalShell),
912 + Layer.succeed(HttpClient.HttpClient, testHttpClient),
913 + Layer.succeed(NetService.NetService, testNetService),
914 + SshAuth.SshPasswordPrompt.disabledLayer,
915 + SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
916 + );
917 +
918 + yield* Effect.flatMap(SshTunnel.SshEnvironmentManager, (manager) =>
919 + manager.disconnectEnvironment(target),
920 + ).pipe(Effect.provide(layer));
921 + assert.isTrue(yield* isRunning(holder.pid), `disconnecting signalled ${kind}`);
922 + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
923 + );
924 +});
925 +
926 // On macOS, Node can bind 127.0.0.1:<port> while another process (such as the
927 // desktop app with network access on) listens on *:<port>. Linux refuses that
928 // bind unless both sockets set SO_REUSEPORT, so on Linux the preload adds it to
packages/ssh/src/tunnel.ts
+22 -4
@@ -542,6 +542,20 @@ fi
542 exec "$T3_RUNTIME_DIR/t3" "$@"
543 `;
544
545 +// kill -0 also passes for a zombie, a reused PID and, on Linux, a thread ID,
546 +// which ps does not list. Only the serve command the launcher started counts;
547 +// where ps cannot look up a PID at all, trust kill -0.
548 +const REMOTE_MANAGED_SERVER_RUNNING_SCRIPT = `managed_server_running() {
549 + [ -n "$1" ] && kill -0 "$1" 2>/dev/null || return 1
550 + if MANAGED_ARGS="$(ps -ww -o args= -p "$1" 2>/dev/null)"; then
551 + case "$MANAGED_ARGS" in
552 + *" serve --host 127.0.0.1 --port "[0-9]*) return 0 ;;
553 + esac
554 + return 1
555 + fi
556 + ! ps -ww -o args= -p "$$" >/dev/null 2>&1
557 +}`;
558 +
559 const REMOTE_LAUNCH_SCRIPT = `set -eu
560 @@T3_NODE_ENV_SCRIPT@@
561 STATE_KEY="$1"
@@ -664,6 +678,7 @@ server_running() {
678 *Z*) return 1 ;;
679 esac
680 }
681 +@@T3_MANAGED_SERVER_RUNNING_SCRIPT@@
682 resolve_default_runtime_port() {
683 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
684 "$RUNNER_FILE" __ssh-helper runtime-port "$DEFAULT_RUNTIME_FILE"
@@ -712,7 +727,7 @@ elif [ -n "$DEFAULT_REMOTE_PORT" ]; then
727 if wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
728 ADOPTED_PID="$DEFAULT_RUNTIME_PID"
729 if [ "$REMOTE_MANAGED" = "managed" ]; then
715 - if [ -n "$REMOTE_PID" ] && kill -0 "$REMOTE_PID" 2>/dev/null; then
730 + if managed_server_running "$REMOTE_PID"; then
731 kill "$REMOTE_PID" 2>/dev/null || true
732 wait_for_pid_exit "$REMOTE_PID"
733 fi
@@ -744,7 +759,7 @@ if [ "$REMOTE_MANAGED" = "external" ]; then
759 REMOTE_PORT=""
760 REMOTE_MANAGED=""
761 fi
747 -elif [ -n "$REMOTE_PID" ] && [ -n "$REMOTE_PORT" ] && server_running "$REMOTE_PID"; then
762 +elif [ -n "$REMOTE_PORT" ] && managed_server_running "$REMOTE_PID"; then
763 # Reconnect owns the local tunnel, not the lifetime of an already running
764 # server. A changed runner is the update path and still replaces it.
765 if [ "$RUNNER_CHANGED" -eq 1 ]; then
@@ -768,7 +783,7 @@ fi
783 if [ -z "$REMOTE_PORT" ] && [ -z "$DEFAULT_RUNTIME_INFO" ]; then
784 for SIBLING_DIR in "$DEFAULT_SERVER_HOME"/ssh-launch/*; do
785 [ "$SIBLING_DIR" != "$STATE_DIR" ] && [ "$(cat "$SIBLING_DIR/managed" 2>/dev/null || true)" = "managed" ] || continue
771 - server_running "$(cat "$SIBLING_DIR/pid" 2>/dev/null || true)" || continue
786 + managed_server_running "$(cat "$SIBLING_DIR/pid" 2>/dev/null || true)" || continue
787 REMOTE_PORT="$(cat "$SIBLING_DIR/port" 2>/dev/null || true)"
788 if [ -n "$REMOTE_PORT" ] && wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
789 REMOTE_MANAGED="external"
@@ -840,7 +855,8 @@ PORT_FILE="$STATE_DIR/port"
855 MANAGED_FILE="$STATE_DIR/managed"
856 REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
857 REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
843 -if [ "$REMOTE_MANAGED" != "external" ] && [ -n "$REMOTE_PID" ] && kill -0 "$REMOTE_PID" 2>/dev/null; then
858 +@@T3_MANAGED_SERVER_RUNNING_SCRIPT@@
859 +if [ "$REMOTE_MANAGED" != "external" ] && managed_server_running "$REMOTE_PID"; then
860 kill "$REMOTE_PID" 2>/dev/null || true
861 WAIT_COUNT=0
862 while kill -0 "$REMOTE_PID" 2>/dev/null && [ "$WAIT_COUNT" -lt 20 ]; do
@@ -930,6 +946,7 @@ function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string {
946 T3_RUNNER_SCRIPT: stripTrailingNewlines(buildRemoteT3RunnerScript(input)),
947 T3_PICK_PORT_SCRIPT: stripTrailingNewlines(REMOTE_PICK_PORT_SCRIPT),
948 T3_WAIT_READY_SCRIPT: stripTrailingNewlines(REMOTE_WAIT_READY_SCRIPT),
949 + T3_MANAGED_SERVER_RUNNING_SCRIPT: REMOTE_MANAGED_SERVER_RUNNING_SCRIPT,
950 T3_DEFAULT_REMOTE_PORT: String(DEFAULT_REMOTE_PORT),
951 T3_REMOTE_PORT_SCAN_WINDOW: String(REMOTE_PORT_SCAN_WINDOW),
952 T3_READY_TIMEOUT_MS: String(REMOTE_READY_TIMEOUT_MS),
@@ -951,6 +968,7 @@ function buildRemotePairingScript(stateKey: string, input?: RemoteT3RunnerOption
968 function buildRemoteStopScript(stateKey: string): string {
969 return applyScriptPlaceholders(REMOTE_STOP_SCRIPT, {
970 T3_STATE_KEY: stateKey,
971 + T3_MANAGED_SERVER_RUNNING_SCRIPT: REMOTE_MANAGED_SERVER_RUNNING_SCRIPT,
972 });
973 }
974