The best way to control Claude Code, Codex and more from any computer or phone
fix(server): order explicit service shutdown
Theo Browne committed
Aug 6, 2026 at 04:54 UTC
8b2ea5721a213c3e3e3bcab56eaea6fa0fa82714
4 files changed
+26
-16
apps/server/src/cloud/bootService.test.ts
+1
-1
@@ -31,7 +31,7 @@ it("keeps systemd pinned to the stable launcher rather than a versioned server",
31
});
32
33
expect(unit).toContain("ExecStart=/usr/bin/node /home/theo/.t3/runtime/service-launcher.mjs");
34
- expect(unit).toContain("KillMode=control-group");
34
+ expect(unit).toContain("KillMode=mixed");
35
expect(unit).not.toContain("versions/1.2.3");
36
});
37
apps/server/src/cloud/bootService.ts
+3
-1
@@ -64,7 +64,9 @@ export function renderBootServiceUnit(plan: BootServicePlan): string {
64
`Environment=T3CODE_HOME=${quoteSystemdValue(plan.baseDir)}`,
65
`Environment=${BOOT_SERVICE_UNIT_ENV}=${BOOT_SERVICE_UNIT_FILE}`,
66
`ExecStart=${quoteSystemdValue(plan.nodePath)} ${quoteSystemdValue(plan.launcherPath)}`,
67
- "KillMode=control-group",
67
+ // Let the launcher mark an explicit stop before it signals the server.
68
+ // systemd still SIGKILLs the whole cgroup if graceful shutdown times out.
69
+ "KillMode=mixed",
70
"Restart=always",
71
"RestartSec=5",
72
`StandardOutput=append:${escapeSystemdSpecifiers(plan.logPath)}`,
apps/server/src/serviceLauncher.test.ts
+5
-3
@@ -112,11 +112,13 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
112
113
const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
114
const running = launcher.run();
115
- yield* Effect.promise(() => launcher.stop("SIGTERM"));
116
- yield* Effect.promise(() => running);
115
+ const stopping = launcher.stop("SIGTERM");
116
// An explicit stop leaves the marker that tells a child shutting down
118
- // mid-update that no replacement server is coming.
117
+ // mid-update that no replacement server is coming. It is present as
118
+ // soon as stop() returns its promise, before queued transitions run.
119
assert.isTrue(yield* fs.exists(path.join(root, "runtime", SERVICE_STOP_MARKER_FILE)));
120
+ yield* Effect.promise(() => stopping);
121
+ yield* Effect.promise(() => running);
122
}),
123
);
124
apps/server/src/serviceLauncher.ts
+17
-11
@@ -5,6 +5,7 @@
5
// `t3 service update`. Keep runtime imports limited to Node built-ins.
6
import * as NodeChildProcess from "node:child_process";
7
import * as NodeCrypto from "node:crypto";
8
+import * as NodeFS from "node:fs";
9
import * as NodeFSP from "node:fs/promises";
10
import * as NodePath from "node:path";
11
@@ -268,6 +269,7 @@ export class Launcher {
269
#child: ManagedChild | null = null;
270
#timer: NodeJS.Timeout | undefined;
271
#transitions: Promise<void> = Promise.resolve();
272
+ #stopRequested = false;
273
#stopping = false;
274
#done = false;
275
readonly #completion = Promise.withResolvers<void>();
@@ -312,22 +314,26 @@ export class Launcher {
314
}
315
316
async stop(signal: NodeJS.Signals): Promise<void> {
315
- if (this.#stopping) {
317
+ // This must happen synchronously at signal receipt. A queued update
318
+ // transition may already be terminating the active child, and that child
319
+ // needs to see the marker in its shutdown finalizer. KillMode=mixed also
320
+ // ensures systemd signals the launcher before the rest of the cgroup.
321
+ try {
322
+ NodeFS.writeFileSync(stopMarkerPath(this.#baseDir), "", { mode: 0o600 });
323
+ } catch {
324
+ // Err toward keeping the tunnel; the next link or unlink reconciles it.
325
+ }
326
+ if (this.#stopRequested || this.#stopping) {
327
await this.#completion.promise.catch(() => undefined);
328
return;
329
}
319
- this.#stopping = true;
330
+ this.#stopRequested = true;
331
this.#clearTimer();
332
this.#enqueue(async () => {
322
- // An explicit stop is the service going away, not an update handoff.
323
- // The marker lets the child's shutdown release the managed tunnel even
324
- // while an update is recorded as pending; the next launcher start
325
- // clears it. Written before the child is signalled so the child sees
326
- // it during teardown. Best-effort: if the write fails the child errs
327
- // toward keeping the tunnel, and the next link or unlink reconciles it.
328
- await NodeFSP.writeFile(stopMarkerPath(this.#baseDir), "", { mode: 0o600 }).catch(
329
- () => undefined,
330
- );
333
+ // Let an update transition already in progress start its replacement
334
+ // before this queued stop tears it down. That replacement owns the
335
+ // pre-activation tunnel cleanup path and observes the marker above.
336
+ this.#stopping = true;
337
const child = this.#child?.process;
338
this.#child = null;
339
if (child !== undefined) await terminateChild(child, signal);