Skip to content

Commit 5131bc0

Browse files
author
easonLiangWorldedtech
committed
fix: fold a lock key from the deepest existing ancestor, not the immediate parent
Two windows failures remained at 927636e and their names are the diagnosis: 'should create parent directory if it doesn't exist' and 'should handle multi-level directory creation', both 'Lock file is already being held'. They are the fallback itself. When a component of the target is missing, realpath of the immediate parent fails and the fallback can fold case but not a short (8.3) name - while resolveLockKey canonicalises through the highest ancestor it can reach. One file then yields two unequal keys, one .lock directory is asked for twice, and the second acquisition collides with the first. _lockIdentityKey now walks up from the target and canonicalises the deepest EXISTING ancestor, appending the segments below it and folding case on Windows. The walk starts at the PARENT, not at the file: the lock is the entry <path>.lock beside the file, so the final component must never be resolved through a symlink - doing so folds a link and its referent into one key, and those are two different .lock entries, which is exactly the pair this unit takes on purpose. That distinction was earned, not assumed: the first version of this change started the walk at the file and three existing tests went red. Their mocks were right (realpath does resolve symlinks); the fold was over-folding. Three win32 tests, one per situation: the whole path exists and is spelled short; the parent is missing and the two spellings differ by case; and the mixed case the CI runner hit - an existing ancestor spelled short with a tail that does not exist yet, which is every create into a directory about to be made. The mocks model the filesystem with one rule (the short spelling of an existing directory resolves to the real one, and nothing below it exists yet) rather than enumerating paths. Negative controls discriminate the two foldings from each other: folding only the immediate parent turns the mixed test red and nothing else; comparing the two keys as exact strings turns all three red. Both mutants were restored byte-exact. Ownership: git log --all -S linkPathLockKey shows this unit's b809020 (10:21:31) as the first commit to define the pair, ahead of U8's 84dd3a7 (10:42:48), and each commit is contained only in its own branch. The fold therefore lands here and ports to U8, where the same defect shows as four safeWriteJson.test.ts failures; the port is not byte-identical because U8 folds with a case-only comparison in a different shape. Verification: 122 passed / 6 skipped across the safeWriteJson, file-safety and ApplyPatchTool specs; prettier --write then --check with the repo config clean; tsc --noEmit with the local paths override 0 errors; eslint . --ext=ts --max-warnings=0 exits 0; eslint-suppressions.json unchanged.
1 parent 927636e commit 5131bc0

2 files changed

Lines changed: 90 additions & 19 deletions

File tree

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,64 @@ describe("safeWriteJson lock key under a peer commit", () => {
304304
expect(mockedAcquireFileLock).toHaveBeenCalledTimes(1)
305305
},
306306
)
307+
308+
// The mixed case the CI runner hit, and the reason folding cannot stop at the immediate parent:
309+
// an ancestor that exists and is spelled as its 8.3 short name, with a tail that does not exist
310+
// yet - a create into a directory that is about to be made. resolveLockKey canonicalises through
311+
// the highest ancestor it can reach, so a fold that only asks for the immediate parent falls
312+
// back to the short spelling while the referent key is already canonical: two unequal keys,
313+
// one .lock directory, and the second acquisition collides with the first.
314+
it.runIf(process.platform === "win32")(
315+
"takes one lock when an existing ancestor is spelled short and the tail does not exist",
316+
async () => {
317+
const realDir = await makeDir("lockkey-mixed-")
318+
const shortDir = path.join(path.dirname(realDir), "LOCKKE~2")
319+
const requested = path.join(shortDir, "not-yet", "history_item.json")
320+
const canonical = path.join(realDir, "not-yet", "history_item.json")
321+
322+
// One filesystem rule, not a list of paths: the short spelling of the existing directory
323+
// resolves to the real one, and nothing below it exists yet, so every deeper probe
324+
// rejects exactly as it would on a host where the create has not happened.
325+
mockedRealpath.mockImplementation(async (probe) => {
326+
const norm = String(probe).toLowerCase().replace(/\\/g, "/")
327+
const short = shortDir.toLowerCase().replace(/\\/g, "/")
328+
const real = realDir.toLowerCase().replace(/\\/g, "/")
329+
if (norm === short) {
330+
return realDir
331+
}
332+
if (norm === real || norm.startsWith(real + "/")) {
333+
return String(probe)
334+
}
335+
throw enoent
336+
})
337+
mockedLstat.mockImplementation(
338+
async () => ({ isSymbolicLink: () => false, isFile: () => true }) as unknown as BigIntStats,
339+
)
340+
const acquired: string[] = []
341+
mockedAcquireFileLock.mockImplementation(async (key) => {
342+
const identity = String(key).toLowerCase()
343+
if (acquired.map((k) => k.toLowerCase()).includes(identity)) {
344+
throw new Error("Lock file is already being held")
345+
}
346+
acquired.push(String(key))
347+
return async () => {}
348+
})
349+
350+
await expect(safeWriteJson(requested, { mcpServers: {} })).resolves.toBeUndefined()
351+
expect(acquired).toHaveLength(1)
352+
// One lock, naming one entry. Which spelling it carries is the implementation's choice
353+
// (it keeps the requested one when the two fold together); what must hold is that the
354+
// spelling is the SAME ENTRY the canonical path names, so a writer arriving by the real
355+
// path queues behind this one rather than beside it.
356+
const entryOf = (probe: string) => {
357+
const norm = probe.toLowerCase().replace(/\\/g, "/")
358+
const short = shortDir.toLowerCase().replace(/\\/g, "/")
359+
const real = realDir.toLowerCase().replace(/\\/g, "/")
360+
return norm.startsWith(short) ? real + norm.slice(short.length) : norm
361+
}
362+
expect(entryOf(acquired[0])).toBe(entryOf(canonical))
363+
},
364+
)
307365
})
308366

309367
it("does not log a cleanup error when the safety net finds the temp file already gone", async () => {

‎src/utils/safeWriteJson.ts‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,42 @@ function _scopeErrorCode(error: unknown): string | undefined {
168168
*
169169
* acquireFileLock locks `<absolute path>.lock` with realpath:false (see fileLock.ts), so the
170170
* lock's identity is the directory ENTRY the path names, not the string. On Windows the
171-
* filesystem folds two spellings of one entry in two different ways: case anywhere, and short
172-
* (8.3) names in a component - `RUNNER~1` against `runneradmin`. A string comparison folds only
173-
* the first, so the second leaves two keys that look different and collide on one .lock
174-
* directory: the second acquisition answers 'Lock file is already being held' against the
175-
* first one's own lock.
171+
* filesystem folds two spellings of one entry in two ways: case anywhere, and short (8.3) names
172+
* inside a component - RUNNER~1 for a long user directory, which is what a CI runner hands out.
173+
* A string comparison folds only the first, so the second leaves two keys that look different,
174+
* one .lock directory, and a second acquisition that collides with the first one's own lock:
175+
* 'Lock file is already being held' once the retries are spent.
176176
*
177-
* So fold the way the lock is placed: canonical parent directory plus basename, case-folded on
178-
* Windows. Canonicalising the parent is what folds a short name, because that folding belongs to
179-
* the filesystem rather than to any string rule. When the parent does not exist yet - a create,
180-
* the common case - realpath fails and the resolved spelling is all there is; case folding still
181-
* applies to it.
177+
* So fold the way the lock is placed: canonicalise the deepest EXISTING ancestor and append the
178+
* segments below it, case-folded on Windows. Canonicalising an existing ancestor is what folds a
179+
* short name, because that folding belongs to the filesystem rather than to any string rule; a
180+
* tail that does not exist yet can only be folded by the string rule. Realpathing the whole path,
181+
* or only its immediate parent, loses the short-name folding as soon as one component is missing
182+
* - which is every create into a directory that is about to be made, and is what the CI runner
183+
* hit: resolveLockKey canonicalises through a higher ancestor, so a fold that stops below it
184+
* returns two unequal keys for one file.
182185
*/
183186
async function _lockIdentityKey(absoluteFilePath: string): Promise<string> {
184-
const parent = path.dirname(absoluteFilePath)
185-
let canonicalParent = parent
186-
try {
187-
canonicalParent = await fs.realpath(parent)
188-
} catch {
189-
// No canonical form exists yet: the directory is about to be created, so the resolved
190-
// spelling is the only spelling.
187+
// The walk starts at the parent, not at the target: the lock is the entry <path>.lock beside
188+
// the file, so the final component must never be resolved through a symlink. Canonicalising it
189+
// would fold a link and its referent into one key, and those are two different .lock entries -
190+
// which is exactly the pair this unit takes on purpose.
191+
const missing: string[] = [path.basename(absoluteFilePath)]
192+
let current = path.dirname(absoluteFilePath)
193+
for (;;) {
194+
let canonical: string | undefined
195+
try {
196+
canonical = await fs.realpath(current)
197+
} catch {
198+
// This component is not there yet; an ancestor of it may be.
199+
}
200+
if (canonical !== undefined || path.dirname(current) === current) {
201+
const folded = path.join(canonical ?? current, ...missing)
202+
return process.platform === "win32" ? folded.toLowerCase() : folded
203+
}
204+
missing.unshift(path.basename(current))
205+
current = path.dirname(current)
191206
}
192-
const folded = path.join(canonicalParent, path.basename(absoluteFilePath))
193-
return process.platform === "win32" ? folded.toLowerCase() : folded
194207
}
195208
async function safeWriteJson(filePath: string, data: any, options?: SafeWriteJsonOptions): Promise<void> {
196209
const absoluteFilePath = path.resolve(filePath)

0 commit comments

Comments
 (0)