Skip to content

Commit 9d895cd

Browse files
t3dotggclaude
andcommitted
fix(server): keep a shadow thread lock directory while it holds writer locks
Codex deletes each thread lock on release, so a lock file that is still there may belong to a live writer, such as an app-server left by a killed server. Replacing the directory then would let a second writer lock the same thread in the shared home, and the old writer's cleanup would delete the new lock through the symlink. Keep the local directory until a later setup finds no thread locks in it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6b7e9b3 commit 9d895cd

2 files changed

Lines changed: 36 additions & 28 deletions

File tree

‎apps/server/src/provider/Drivers/CodexHomeLayout.test.ts‎

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
121121
'{"models":["shared"]}\n',
122122
);
123123
yield* writeTextFile(path.join(sharedHome, "auth.json"), '{"shared":true}\n');
124+
yield* writeTextFile(path.join(sharedHome, "auth.lock"), "");
124125
yield* fileSystem.makeDirectory(shadowHome, { recursive: true });
125126
yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
127+
yield* writeTextFile(path.join(shadowHome, "auth.lock"), "");
126128
yield* fileSystem.symlink(
127129
path.join(sharedHome, "models_cache.json"),
128130
path.join(shadowHome, "models_cache.json"),
@@ -149,13 +151,17 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
149151
.readLink(path.join(shadowHome, "auth.json"))
150152
.pipe(Effect.result);
151153
const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"));
154+
const authLockLinkResult = yield* fileSystem
155+
.readLink(path.join(shadowHome, "auth.lock"))
156+
.pipe(Effect.result);
152157

153158
expect(sessionsTarget).toBe(path.join(sharedHome, "sessions"));
154159
expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
155160
expect(mcpOauthLocksTarget).toBe(path.join(sharedHome, "mcp-oauth-locks"));
156161
expect(modelsCacheExists).toBe(false);
157162
expect(authLinkResult._tag).toBe("Failure");
158163
expect(authContents).toContain("shadow");
164+
expect(authLockLinkResult._tag).toBe("Failure");
159165
}),
160166
);
161167

@@ -222,7 +228,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
222228
);
223229

224230
it.effect.skipIf(!symlinksSupported)(
225-
"replaces Codex-created local thread writer locks with the shared lock directory",
231+
"shares local thread writer locks once no writer still holds one",
226232
() =>
227233
Effect.gen(function* () {
228234
const fileSystem = yield* FileSystem.FileSystem;
@@ -231,8 +237,10 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
231237
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
232238
const shadowHome = path.join(shadowRoot, "shadow");
233239
const shadowLocks = path.join(shadowHome, "thread-writer-locks");
240+
const threadLock = path.join(shadowLocks, "019a0000-0000-7000-8000-000000000000.lock");
234241

235242
yield* writeTextFile(path.join(shadowLocks, ".coordination.lock"), "");
243+
yield* writeTextFile(threadLock, "");
236244

237245
const layout = yield* resolveCodexHomeLayout(
238246
decodeCodexSettings({
@@ -242,40 +250,16 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
242250
);
243251

244252
yield* materializeCodexShadowHome(layout);
253+
expect(yield* fileSystem.exists(threadLock)).toBe(true);
245254

255+
yield* fileSystem.remove(threadLock);
256+
yield* materializeCodexShadowHome(layout);
246257
expect(yield* fileSystem.readLink(shadowLocks)).toBe(
247258
path.join(sharedHome, "thread-writer-locks"),
248259
);
249260
}),
250261
);
251262

252-
it.effect.skipIf(!symlinksSupported)("keeps the shadow auth lock private", () =>
253-
Effect.gen(function* () {
254-
const fileSystem = yield* FileSystem.FileSystem;
255-
const path = yield* Path.Path;
256-
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
257-
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
258-
const shadowHome = path.join(shadowRoot, "shadow");
259-
const shadowLock = path.join(shadowHome, "auth.lock");
260-
261-
yield* writeTextFile(path.join(sharedHome, "auth.lock"), "");
262-
yield* writeTextFile(shadowLock, "");
263-
264-
const layout = yield* resolveCodexHomeLayout(
265-
decodeCodexSettings({
266-
homePath: sharedHome,
267-
shadowHomePath: shadowHome,
268-
}),
269-
);
270-
271-
yield* materializeCodexShadowHome(layout);
272-
273-
const lockLinkResult = yield* fileSystem.readLink(shadowLock).pipe(Effect.result);
274-
expect(lockLinkResult._tag).toBe("Failure");
275-
expect(yield* fileSystem.exists(shadowLock)).toBe(true);
276-
}),
277-
);
278-
279263
it.effect.skipIf(!symlinksSupported)(
280264
"accepts Codex-created shadow-local runtime directories",
281265
() =>

‎apps/server/src/provider/Drivers/CodexHomeLayout.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,30 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu
259259
});
260260
}
261261

262+
if (input.entryName === "thread-writer-locks") {
263+
const lockNames = yield* input.fileSystem.readDirectory(link).pipe(
264+
Effect.catchTags({
265+
PlatformError: (cause) =>
266+
new CodexShadowHomeFileSystemError({
267+
sharedHomePath: input.sharedHomePath,
268+
effectiveHomePath: input.effectiveHomePath,
269+
operation: "readDirectory",
270+
path: link,
271+
entryName: input.entryName,
272+
cause,
273+
}),
274+
}),
275+
);
276+
// Codex deletes a thread lock on release, so one that is left may still guard a live
277+
// writer. Keep the local directory until a later setup finds it empty.
278+
if (lockNames.some((name) => name !== ".coordination.lock")) {
279+
return yield* Effect.logWarning(
280+
"Codex shadow home still has thread writer locks; it will share them on a later setup.",
281+
{ path: link },
282+
);
283+
}
284+
}
285+
262286
yield* input.fileSystem.remove(link, { recursive: true }).pipe(
263287
Effect.catchTags({
264288
PlatformError: (cause) =>

0 commit comments

Comments
 (0)