Skip to content

Commit 5f93883

Browse files
fix(file-safety): fall back to the link walk only on a dangling link
resolveLockKey caught every resolution error and fell back to the bounded readlink walk, so a transient EACCES or EIO from realpath or lstat keyed the write to the alias path while a writer reaching the referent directly keyed the referent - two locks for one file, and the alias/referent serialization the unit promises did not hold. Only ENOENT (the dangling-link case the walk exists for) now falls through; every other error propagates, matching resolvePublishTarget's own policy. Regression test: EACCES from realpath rejects resolveLockKey instead of returning an alias key; removing the guard reddens exactly that test. Also restore the rename/unlink/console spies in a finally block in the cleanup-safety test: a failed assertion previously left the spies active for the afterEach rm and every later test.
1 parent d5a68d1 commit 5f93883

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,14 @@ async function canonicalDirKey(absoluteFilePath: string): Promise<string> {
218218
export async function resolveLockKey(absoluteFilePath: string): Promise<string> {
219219
try {
220220
return await canonicalDirKey(await resolvePublishTarget(absoluteFilePath))
221-
} catch {
221+
} catch (error: unknown) {
222+
// Only a dangling link (ENOENT) may fall through to the walk: any other
223+
// resolution error (EACCES, EIO) would key this writer to the alias path
224+
// while a writer reaching the referent directly keys the referent, and
225+
// the two writers would then take different locks for one file.
226+
if (errorCode(error) !== "ENOENT") {
227+
throw error
228+
}
222229
// A real readlink throws for anything that is not a link, so a normal chain
223230
// ends the walk. Two links that point at each other never would, so the
224231
// walk is bounded and callers use the key they actually reached.

‎src/utils/__tests__/safeWriteJson.lockKey.spec.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ describe("safeWriteJson lock key under a peer commit", () => {
159159

160160
expect(await resolveLockKey(file)).toBe(path.join(canonicalDir, "history_item.json"))
161161
})
162+
163+
it("propagates a non-ENOENT resolution failure instead of keying on the alias", async () => {
164+
// EACCES from realpath is not a dangling link: falling back to the walk
165+
// would key this writer to the alias path while a writer reaching the
166+
// referent directly keys the referent, and the two writers would take
167+
// different locks for the same file.
168+
const aliasDir = path.join(os.tmpdir(), "alias-dir")
169+
const file = path.join(aliasDir, "history_item.json")
170+
const eacces = Object.assign(new Error("EACCES: permission denied"), { code: "EACCES" })
171+
mockedRealpath.mockImplementation(async () => {
172+
throw eacces
173+
})
174+
mockedLstat.mockImplementation(
175+
async () => ({ isSymbolicLink: () => false, isFile: () => true }) as unknown as BigIntStats,
176+
)
177+
await expect(resolveLockKey(file)).rejects.toBe(eacces)
178+
})
162179
})
163180

164181
it("does not log a cleanup error when the safety net finds the temp file already gone", async () => {
@@ -175,12 +192,17 @@ it("does not log a cleanup error when the safety net finds the temp file already
175192
const unlinkSpy = vi.spyOn(fs, "unlink").mockRejectedValue(enoent)
176193
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {})
177194

178-
await expect(safeWriteJson(target, { id: "task-1" })).rejects.toThrow("commit rename failed")
179-
180-
// Only the original failure is reported.
181-
expect(consoleError).toHaveBeenCalledTimes(1)
182-
183-
renameSpy.mockRestore()
184-
unlinkSpy.mockRestore()
185-
consoleError.mockRestore()
195+
try {
196+
await expect(safeWriteJson(target, { id: "task-1" })).rejects.toThrow("commit rename failed")
197+
198+
// Only the original failure is reported.
199+
expect(consoleError).toHaveBeenCalledTimes(1)
200+
} finally {
201+
// Restore even when an assertion fails: the afterEach cleanup runs fs.rm
202+
// with these spies still in place otherwise, and later tests inherit a
203+
// rejecting rename.
204+
renameSpy.mockRestore()
205+
unlinkSpy.mockRestore()
206+
consoleError.mockRestore()
207+
}
186208
})

0 commit comments

Comments
 (0)