@devries-cameron20 / t3code-1 / commits / 6fd14f46d6
Fork 0

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

fix(server): servers on one machine no longer update a shared provider CLI at once (#38849)

Julius Marminge committed Nov 18, 2026 at 19:41 UTC 6fd14f46d667ff4db712478e8c877a1de80a80fe
2 files changed +110 -1
apps/server/src/provider/providerMaintenanceCommandCoordinator.test.ts new
+48
@@ -0,0 +1,48 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - A separate Node process stands in for another server.
2 +import * as NodeChildProcess from "node:child_process";
3 +import * as NodeCrypto from "node:crypto";
4 +import * as NodeModule from "node:module";
5 +import * as NodeUtil from "node:util";
6 +import { assert, it } from "@effect/vitest";
7 +import * as Effect from "effect/Effect";
8 +import {
9 + machineLockFile,
10 + makeProviderMaintenanceCommandCoordinator,
11 +} from "./providerMaintenanceCommandCoordinator.ts";
12 +
13 +const execFile = NodeUtil.promisify(NodeChildProcess.execFile);
14 +const lockModule = NodeModule.createRequire(import.meta.url).resolve("proper-lockfile");
15 +const probe = (file: string) =>
16 + Effect.promise(async () => {
17 + const { stdout } = await execFile(process.execPath, [
18 + "-e",
19 + `
20 + const { lock } = require(process.argv[1]);
21 + lock(process.argv[2], { realpath: false }).then(async release => {
22 + await release(); process.stdout.write('acquired');
23 + }, error => {
24 + if (error.code === 'ELOCKED') process.stdout.write('blocked');
25 + else { console.error(error); process.exitCode = 1; }
26 + });
27 + `,
28 + lockModule,
29 + file,
30 + ]);
31 + return stdout;
32 + });
33 +
34 +it.live("holds the update's lock key against other server processes while it runs", () =>
35 + Effect.gen(function* () {
36 + const lockKey = `test:${NodeCrypto.randomUUID()}`;
37 + const coordinator = yield* makeProviderMaintenanceCommandCoordinator({
38 + makeAlreadyRunningError: () => "already running",
39 + });
40 + const duringUpdate = yield* coordinator.withCommandLock({
41 + targetKey: "instance:test",
42 + lockKey,
43 + run: probe(machineLockFile(lockKey)),
44 + });
45 + assert.strictEqual(duringUpdate, "blocked");
46 + assert.strictEqual(yield* probe(machineLockFile(lockKey)), "acquired");
47 + }),
48 +);
apps/server/src/provider/providerMaintenanceCommandCoordinator.ts
+62 -1
@@ -1,6 +1,14 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - Update leases coordinate Node server processes.
2 +import * as NodeCrypto from "node:crypto";
3 +import * as NodeFSP from "node:fs/promises";
4 +import * as NodeOS from "node:os";
5 +import * as NodePath from "node:path";
6 import * as KeyedLock from "@t3tools/shared/KeyedLock";
7 +import { lock } from "proper-lockfile";
8 import * as Effect from "effect/Effect";
9 import * as Ref from "effect/Ref";
10 +import * as Schedule from "effect/Schedule";
11 +import * as Schema from "effect/Schema";
12
13 export interface ProviderMaintenanceCommandCoordinatorShape<E> {
14 readonly withCommandLock: <A, R>(input: {
@@ -11,6 +19,59 @@ export interface ProviderMaintenanceCommandCoordinatorShape<E> {
19 }) => Effect.Effect<A, E, R>;
20 }
21
22 +class MachineLockError extends Schema.TaggedError<MachineLockError>()("MachineLockError", {
23 + held: Schema.Boolean,
24 + cause: Schema.Defect(),
25 +}) {}
26 +
27 +const lockDirectory = () => NodePath.join(NodeOS.tmpdir(), "t3code-provider-updates");
28 +
29 +/** @internal */
30 +export const machineLockFile = (lockKey: string) =>
31 + NodePath.join(
32 + lockDirectory(),
33 + NodeCrypto.createHash("sha256").update(lockKey).digest("hex").slice(0, 32),
34 + );
35 +
36 +// Other T3 servers on this machine can run the same installation's updater; one replacing its own
37 +// files makes the other's spawn or capability check fail. Without a usable lock directory, run anyway.
38 +const withMachineLock = <A, E, R>(
39 + lockKey: string,
40 + run: Effect.Effect<A, E, R>,
41 +): Effect.Effect<A, E, R> => {
42 + const directory = lockDirectory();
43 + const file = machineLockFile(lockKey);
44 + const acquire = Effect.acquireRelease(
45 + Effect.tryPromise({
46 + try: async () => {
47 + await NodeFSP.mkdir(directory, { recursive: true });
48 + return await lock(file, {
49 + realpath: false,
50 + stale: 60_000,
51 + update: 10_000,
52 + onCompromised: () => {},
53 + });
54 + },
55 + catch: (cause) =>
56 + new MachineLockError({
57 + held: (cause as { code?: unknown } | null)?.code === "ELOCKED",
58 + cause,
59 + }),
60 + }),
61 + // A compromised lease rejects on release; the update has already finished by then.
62 + (release) => Effect.promise(() => release().catch(() => undefined)),
63 + ).pipe(
64 + Effect.retry({ while: (error) => error.held, schedule: Schedule.spaced("1 second") }),
65 + Effect.catch((error) =>
66 + Effect.logWarning("Could not lock provider update across servers", {
67 + lockKey,
68 + cause: String(error.cause),
69 + }),
70 + ),
71 + );
72 + return Effect.scoped(acquire.pipe(Effect.andThen(run)));
73 +};
74 +
75 export const makeProviderMaintenanceCommandCoordinator = Effect.fn(
76 "makeProviderMaintenanceCommandCoordinator",
77 )(function* <E>(input: { readonly makeAlreadyRunningError: (targetKey: string) => E }) {
@@ -48,7 +109,7 @@ export const makeProviderMaintenanceCommandCoordinator = Effect.fn(
109 }
110
111 return yield* (onQueued ?? Effect.void).pipe(
51 - Effect.andThen(locks.withLock(lockKey, run)),
112 + Effect.andThen(locks.withLock(lockKey, withMachineLock(lockKey, run))),
113 Effect.ensuring(releaseTarget(targetKey)),
114 );
115 });