The best way to control Claude Code, Codex and more from any computer or phone
fix(cli): t3 connect unlink no longer warns about a server that is gone (#30324)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
MarcoDotIO committed
Nov 5, 2026 at 23:25 UTC
0bf1c9d076c8edc36629848fe9cef4bcad5c45b1
2 files changed
+134
-1
apps/server/src/cli/connect.test.ts
+122
@@ -1,8 +1,25 @@
1
+// @effect-diagnostics nodeBuiltinImport:off - CLI integration exercises the Node HTTP boundary.
2
+import * as NodeHttp from "node:http";
3
+
4
+import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
5
+import * as NodeServices from "@effect/platform-node/NodeServices";
6
+import * as NetService from "@t3tools/shared/Net";
7
import * as RelayClient from "@t3tools/shared/relayClient";
8
import { assert, it } from "@effect/vitest";
9
+import * as ConfigProvider from "effect/ConfigProvider";
10
+import * as Console from "effect/Console";
11
import * as Effect from "effect/Effect";
12
+import * as FileSystem from "effect/FileSystem";
13
+import * as Layer from "effect/Layer";
14
import * as Option from "effect/Option";
15
+import * as TestConsole from "effect/testing/TestConsole";
16
+import { Command } from "effect/cli";
17
18
+import { makeCli } from "../binCli.ts";
19
+import {
20
+ makePersistedServerRuntimeState,
21
+ persistServerRuntimeState,
22
+} from "../serverRuntimeState.ts";
23
import { acquireRelayClientForLink } from "./connect.ts";
24
25
const managedExecutable = {
@@ -40,3 +57,108 @@ it.effect("does not install the relay client when the user declines the managed
57
assert.equal(installCalls, 0);
58
}),
59
);
60
+
61
+it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))(
62
+ "t3 connect unlink",
63
+ (it) => {
64
+ const runUnlink = (recordedPid: number, options?: { readonly portFreed?: boolean }) =>
65
+ Effect.gen(function* () {
66
+ const requests: Array<string> = [];
67
+ const warnings: Array<string> = [];
68
+ const server = yield* Effect.acquireRelease(
69
+ Effect.callback<NodeHttp.Server>((resume) => {
70
+ const listening = NodeHttp.createServer((request, response) => {
71
+ requests.push(`${request.method} ${request.url}`);
72
+ response.writeHead(200, { "content-type": "application/json" });
73
+ response.end(JSON.stringify({ ok: true, endpointRuntimeStatus: null }));
74
+ });
75
+ listening.listen(0, "127.0.0.1", () => resume(Effect.succeed(listening)));
76
+ }),
77
+ (listening) => Effect.sync(() => listening.close()),
78
+ );
79
+ const address = server.address();
80
+ if (address === null || typeof address === "string") {
81
+ return yield* Effect.die(new Error("Expected a TCP address"));
82
+ }
83
+ if (options?.portFreed) {
84
+ yield* Effect.callback<void>((resume) => {
85
+ server.close(() => resume(Effect.void));
86
+ });
87
+ }
88
+
89
+ const fs = yield* FileSystem.FileSystem;
90
+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-connect-unlink-test-" });
91
+ const state = yield* makePersistedServerRuntimeState({
92
+ config: { host: "127.0.0.1", devUrl: undefined },
93
+ port: address.port,
94
+ });
95
+ yield* persistServerRuntimeState({
96
+ path: `${baseDir}/userdata/server-runtime.json`,
97
+ state: { ...state, pid: recordedPid },
98
+ });
99
+
100
+ yield* Command.runWith(makeCli({ cloudEnabled: true }), { version: "0.0.0" })([
101
+ "connect",
102
+ "unlink",
103
+ "--base-dir",
104
+ baseDir,
105
+ ]).pipe(
106
+ // TestConsole has no reader for Console.warn.
107
+ Effect.updateService(Console.Console, (console) => ({
108
+ ...console,
109
+ warn: (...args: ReadonlyArray<unknown>) => {
110
+ warnings.push(args.join(" "));
111
+ },
112
+ })),
113
+ );
114
+
115
+ return { requests, warnings, printed: (yield* TestConsole.logLines).join("\n") };
116
+ }).pipe(
117
+ Effect.scoped,
118
+ Effect.provide(
119
+ Layer.mergeAll(
120
+ ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
121
+ Layer.fresh(TestConsole.layer),
122
+ ),
123
+ ),
124
+ );
125
+
126
+ // Above Linux's PID ceiling, as in pair.test.ts: no process can hold it.
127
+ const deadPid = 4_194_305;
128
+
129
+ it.effect("does not tell the user to restart a server that is gone", () =>
130
+ Effect.gen(function* () {
131
+ const { printed, warnings } = yield* runUnlink(deadPid, { portFreed: true });
132
+
133
+ assert.include(printed, "T3 Connect is disabled locally.");
134
+ assert.notInclude(warnings.join("\n"), "Restart that server");
135
+ }),
136
+ );
137
+
138
+ it.effect("warns that a server that is gone may have left its connector running", () =>
139
+ Effect.gen(function* () {
140
+ const { warnings } = yield* runUnlink(deadPid, { portFreed: true });
141
+
142
+ assert.include(warnings.join("\n"), "its cloudflared process may still be running");
143
+ }),
144
+ );
145
+
146
+ it.effect("does not send a token to whatever took over a dead server's port", () =>
147
+ Effect.gen(function* () {
148
+ const { requests } = yield* runUnlink(deadPid);
149
+
150
+ assert.deepEqual(requests, []);
151
+ }),
152
+ );
153
+
154
+ it.effect("asks a running server to stop its tunnel", () =>
155
+ Effect.gen(function* () {
156
+ const { requests, printed, warnings } = yield* runUnlink(process.pid);
157
+
158
+ assert.deepEqual(requests, ["POST /api/connect/unlink"]);
159
+ assert.include(printed, "T3 Connect is disabled locally.");
160
+ assert.notInclude(warnings.join("\n"), "cloudflared process");
161
+ }),
162
+ );
163
+ },
164
+);
apps/server/src/cli/connect.ts
+12
-1
@@ -50,7 +50,7 @@ import * as RelayTracing from "../cloud/relayTracing.ts";
50
import * as ServerConfig from "../config.ts";
51
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
52
import * as ExternalLauncher from "../process/externalLauncher.ts";
53
-import { readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
53
+import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
54
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
55
import { resolveCliCommand } from "./invocation.ts";
56
import * as CliService from "./service.ts";
@@ -298,6 +298,7 @@ const withCloudCliSessionToken = <A, E, R>(
298
299
type LiveCloudActionResult =
300
| { readonly status: "not-running" }
301
+ | { readonly status: "server-gone" }
302
| { readonly status: "succeeded" }
303
| { readonly status: "failed"; readonly cause: Cause.Cause<unknown> };
304
@@ -307,6 +308,10 @@ const runLiveCloudUnlink = Effect.fn("cloud.cli.run_live_unlink")(function* () {
308
if (Option.isNone(runtimeState)) {
309
return { status: "not-running" } satisfies LiveCloudActionResult;
310
}
311
+ // A killed server leaves its record behind, and another process may own its port by now.
312
+ if (!isProcessAlive(runtimeState.value.pid)) {
313
+ return { status: "server-gone" } satisfies LiveCloudActionResult;
314
+ }
315
316
const environmentAuth = yield* EnvironmentAuth.EnvironmentAuth;
317
const result = yield* Effect.exit(
@@ -458,6 +463,12 @@ const reportCloudDisconnectResults = Effect.fn("cloud.cli.report_disconnect_resu
463
);
464
} else {
465
yield* Console.log("T3 Connect is disabled locally.");
466
+ // A killed server cannot stop the connector it started, and nothing adopts it on restart.
467
+ if (input.liveResult.status === "server-gone") {
468
+ yield* Console.warn(
469
+ "The server that ran T3 Connect is gone, but its cloudflared process may still be running.\nStop that process if it is.",
470
+ );
471
+ }
472
}
473
474
if (Exit.isFailure(input.relayResult)) {