Skip to content

Commit 41832ac

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 6953d6b commit 41832ac

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
@@ -299,7 +299,14 @@ async function canonicalDirKey(absoluteFilePath: string): Promise<string> {
299299
export async function resolveLockKey(absoluteFilePath: string): Promise<string> {
300300
try {
301301
return await canonicalDirKey(await resolvePublishTarget(absoluteFilePath))
302-
} catch {
302+
} catch (error: unknown) {
303+
// Only a dangling link (ENOENT) may fall through to the walk: any other
304+
// resolution error (EACCES, EIO) would key this writer to the alias path
305+
// while a writer reaching the referent directly keys the referent, and
306+
// the two writers would then take different locks for one file.
307+
if (errorCode(error) !== "ENOENT") {
308+
throw error
309+
}
303310
// A real readlink throws for anything that is not a link, so a normal chain
304311
// ends the walk. Two links that point at each other never would, so the
305312
// 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
@@ -170,6 +170,23 @@ describe("safeWriteJson lock key under a peer commit", () => {
170170

171171
expect(await resolveLockKey(file)).toBe(path.join(canonicalDir, "history_item.json"))
172172
})
173+
174+
it("propagates a non-ENOENT resolution failure instead of keying on the alias", async () => {
175+
// EACCES from realpath is not a dangling link: falling back to the walk
176+
// would key this writer to the alias path while a writer reaching the
177+
// referent directly keys the referent, and the two writers would take
178+
// different locks for the same file.
179+
const aliasDir = path.join(os.tmpdir(), "alias-dir")
180+
const file = path.join(aliasDir, "history_item.json")
181+
const eacces = Object.assign(new Error("EACCES: permission denied"), { code: "EACCES" })
182+
mockedRealpath.mockImplementation(async () => {
183+
throw eacces
184+
})
185+
mockedLstat.mockImplementation(
186+
async () => ({ isSymbolicLink: () => false, isFile: () => true }) as unknown as BigIntStats,
187+
)
188+
await expect(resolveLockKey(file)).rejects.toBe(eacces)
189+
})
173190
})
174191

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

189-
await expect(safeWriteJson(target, { id: "task-1" })).rejects.toThrow("commit rename failed")
190-
191-
// Only the original failure is reported.
192-
expect(consoleError).toHaveBeenCalledTimes(1)
193-
194-
renameSpy.mockRestore()
195-
unlinkSpy.mockRestore()
196-
consoleError.mockRestore()
206+
try {
207+
await expect(safeWriteJson(target, { id: "task-1" })).rejects.toThrow("commit rename failed")
208+
209+
// Only the original failure is reported.
210+
expect(consoleError).toHaveBeenCalledTimes(1)
211+
} finally {
212+
// Restore even when an assertion fails: the afterEach cleanup runs fs.rm
213+
// with these spies still in place otherwise, and later tests inherit a
214+
// rejecting rename.
215+
renameSpy.mockRestore()
216+
unlinkSpy.mockRestore()
217+
consoleError.mockRestore()
218+
}
197219
})

0 commit comments

Comments
 (0)