The best way to control Claude Code, Codex and more from any computer or phone
fix(server): allow restarts when a stale runtime record matches the server pid (#20336)
Scott Norteman committed
Oct 18, 2026 at 15:28 UTC
23dae7b3a38956448b6999835a51fc162e9604a7
3 files changed
+98
-7
apps/server/src/cli/app.test.ts
+22
-2
@@ -1,11 +1,13 @@
1
// @effect-diagnostics nodeBuiltinImport:off -- The integration fixture binds the same platform socket or named pipe as the CLI.
2
+import * as NodeChildProcess from "node:child_process";
3
+import * as NodeEvents from "node:events";
4
import * as NodeFSP from "node:fs/promises";
5
import * as NodeNet from "node:net";
6
import * as NodeOS from "node:os";
7
import * as NodePath from "node:path";
8
9
import * as NodeServices from "@effect/platform-node/NodeServices";
8
-import { it } from "@effect/vitest";
10
+import { assert, it } from "@effect/vitest";
11
import type { DesktopAppActivationRequest } from "@t3tools/contracts";
12
import { resolveDesktopAppControlAddress } from "@t3tools/shared/desktopAppControl";
13
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -20,6 +22,22 @@ import { describe, expect } from "vite-plus/test";
22
import { makeCli } from "../binCli.ts";
23
import { PersistedServerRuntimeState } from "../serverRuntimeState.ts";
24
25
+const liveProcess = Effect.acquireRelease(
26
+ Effect.promise(async () => {
27
+ const child = NodeChildProcess.spawn(process.execPath, ["-e", "process.stdin.resume()"], {
28
+ stdio: ["pipe", "ignore", "ignore"],
29
+ });
30
+ await NodeEvents.EventEmitter.once(child, "spawn");
31
+ return child;
32
+ }),
33
+ (child) =>
34
+ Effect.promise(async () => {
35
+ const exited = NodeEvents.EventEmitter.once(child, "exit");
36
+ child.kill();
37
+ await exited;
38
+ }),
39
+);
40
+
41
const encodeRuntimeState = Schema.encodeEffect(Schema.fromJsonString(PersistedServerRuntimeState));
42
43
const runCli = (args: ReadonlyArray<string>, env: Record<string, string> = {}) =>
@@ -165,9 +183,11 @@ describe("t3 server command safety", () => {
183
const baseDir = NodePath.join(root, "home");
184
const stateDir = NodePath.join(baseDir, "userdata");
185
const statePath = NodePath.join(stateDir, "server-runtime.json");
186
+ const child = yield* liveProcess;
187
+ assert(child.pid !== undefined);
188
const record = yield* encodeRuntimeState({
189
version: 1,
170
- pid: process.pid,
190
+ pid: child.pid,
191
port: 3773,
192
origin: "http://127.0.0.1:3773",
193
startedAt: "2026-10-01T00:00:00.000Z",
apps/server/src/cli/config.test.ts
+69
-4
@@ -1,4 +1,6 @@
1
// @effect-diagnostics nodeBuiltinImport:off
2
+import * as NodeChildProcess from "node:child_process";
3
+import * as NodeEvents from "node:events";
4
import * as NodeFS from "node:fs";
5
import * as NodeOS from "node:os";
6
@@ -10,6 +12,7 @@ import * as Layer from "effect/Layer";
12
import * as Option from "effect/Option";
13
import * as Path from "effect/Path";
14
import * as Redacted from "effect/Redacted";
15
+import * as Result from "effect/Result";
16
import * as Schema from "effect/Schema";
17
18
import {
@@ -58,6 +61,22 @@ const makeDesktopBootstrap = (
61
...overrides,
62
});
63
64
+const liveProcess = Effect.acquireRelease(
65
+ Effect.promise(async () => {
66
+ const child = NodeChildProcess.spawn(process.execPath, ["-e", "process.stdin.resume()"], {
67
+ stdio: ["pipe", "ignore", "ignore"],
68
+ });
69
+ await NodeEvents.EventEmitter.once(child, "spawn");
70
+ return child;
71
+ }),
72
+ (child) =>
73
+ Effect.promise(async () => {
74
+ const exited = NodeEvents.EventEmitter.once(child, "exit");
75
+ child.kill();
76
+ await exited;
77
+ }),
78
+);
79
+
80
it.layer(NodeServices.layer)("cli config resolution", (it) => {
81
const defaultObservabilityConfig = {
82
traceMinLevel: "Info",
@@ -100,10 +119,13 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
119
const fs = yield* FileSystem.FileSystem;
120
const path = yield* Path.Path;
121
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-" });
122
+ const child = yield* liveProcess;
123
+ assert(child.pid !== undefined);
124
for (const [name, pid, mode, rejectRunningServer] of [
125
["stale", 2_147_483_647, "web", true],
105
- ["desktop", process.pid, "desktop", true],
106
- ["serve", process.pid, "web", false],
126
+ ["own-pid", process.pid, "web", true],
127
+ ["desktop", child.pid, "desktop", true],
128
+ ["serve", child.pid, "web", false],
129
] as const) {
130
const baseDir = path.join(root, name);
131
const stateDir = path.join(baseDir, "userdata");
@@ -119,7 +141,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
141
}),
142
);
143
const cwd = path.join(root, `${name}-project`);
122
- const config = yield* resolveServerConfig(
144
+ const configResult = yield* resolveServerConfig(
145
{
146
...minimalWebFlags(baseDir),
147
mode: Option.some(mode),
@@ -135,13 +157,56 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
157
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
158
),
159
),
160
+ Effect.result,
161
);
139
- expect(config.cwd).toBe(cwd);
162
+ assert(Result.isSuccess(configResult));
163
+ expect(configResult.success.cwd).toBe(cwd);
164
expect(yield* fs.exists(cwd)).toBe(true);
165
}
166
}),
167
);
168
169
+ it.effect(
170
+ "refuses a manual web launch when a runtime record belongs to another live process",
171
+ () =>
172
+ Effect.gen(function* () {
173
+ const fs = yield* FileSystem.FileSystem;
174
+ const path = yield* Path.Path;
175
+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-live-" });
176
+ const child = yield* liveProcess;
177
+ assert(child.pid !== undefined);
178
+ const stateDir = path.join(baseDir, "userdata");
179
+ yield* fs.makeDirectory(stateDir, { recursive: true });
180
+ yield* fs.writeFileString(
181
+ path.join(stateDir, "server-runtime.json"),
182
+ yield* encodeUnknownJson({
183
+ version: 1,
184
+ pid: child.pid,
185
+ port: 3773,
186
+ origin: "http://127.0.0.1:3773",
187
+ startedAt: "2026-10-01T00:00:00.000Z",
188
+ }),
189
+ );
190
+ const cwd = path.join(baseDir, "project");
191
+ const error = yield* resolveServerConfig(
192
+ { ...minimalWebFlags(baseDir), cwd: Option.some(cwd) },
193
+ Option.none(),
194
+ { rejectRunningServer: true },
195
+ ).pipe(
196
+ Effect.provide(
197
+ Layer.merge(
198
+ NetService.layer,
199
+ ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
200
+ ),
201
+ ),
202
+ Effect.flip,
203
+ );
204
+ expect(String(error)).toContain("A T3 Code server is already running");
205
+ expect(String(error)).toContain(`pid ${child.pid}`);
206
+ expect(yield* fs.exists(cwd)).toBe(false);
207
+ }),
208
+ );
209
+
210
it.effect("enables a trimmed reusable auth token only for web dev mode", () =>
211
Effect.gen(function* () {
212
const baseDir = yield* FileSystem.FileSystem.pipe(
apps/server/src/cli/config.ts
+7
-1
@@ -333,7 +333,13 @@ export const resolveServerConfig = (
333
// and supervisor handoff are separate; this preflight cannot arbitrate two starts.
334
if (options?.rejectRunningServer && mode === "web") {
335
const runtime = yield* readPersistedServerRuntimeState(derivedPaths.serverRuntimeStatePath);
336
- if (Option.isSome(runtime) && runtime.value.pid > 0 && isProcessAlive(runtime.value.pid)) {
336
+ // A killed container can leave a record with the new server's own PID.
337
+ if (
338
+ Option.isSome(runtime) &&
339
+ runtime.value.pid > 0 &&
340
+ runtime.value.pid !== process.pid &&
341
+ isProcessAlive(runtime.value.pid)
342
+ ) {
343
return yield* new CliError.UserError({
344
cause: `A T3 Code server is already running for ${baseDir} (pid ${runtime.value.pid}, ${runtime.value.origin}). Connect to that server, stop it before starting another, or use a different --base-dir.`,
345
});