Skip to content

Commit 4603817

Browse files
Yigtwxxclaude
andcommitted
fix(lock): keep the denial when no retry can be scheduled
withinDenialBudget() only bounded the denial count, not the caller's retry and timeout limits. A lock-file EPERM still went through waitForRetry(), which throws file_lock_timeout under retries: 0 or an elapsed deadline, so the caller lost the permission diagnosis on exactly the settings that ask to fail fast. The PR claimed bounded denials preserved EPERM; they did not. Hand the original denial back when waiting cannot schedule another attempt. Any other waitForRetry() failure still propagates as itself, and both the create and snapshot-read branches route through the same helper. Adds a regression pinning EPERM with retries: 0 for the create branch. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GGksu4AaxVTcTHZntKTyAs
1 parent 3d0ffe4 commit 4603817

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎scripts/check-file-size.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const LINE_BUDGETS = new Map([
77
["src/permissions.ts", 566],
88
["src/root-impl.ts", 1750],
99
["src/root-path.ts", 862],
10-
["src/sidecar-lock.ts", 525],
10+
["src/sidecar-lock.ts", 540],
1111
["test/api-coverage.test.ts", 983],
1212
["test/new-primitives.test.ts", 1500],
13+
["test/sidecar-lock-regression.test.ts", 540],
1314
]);
1415

1516
function walk(dir) {

‎src/sidecar-lock.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ export function createSidecarLockManager(key: string) {
266266
// Bounded so a genuine denial still surfaces as EPERM, not a lock timeout.
267267
let transientDenials = 0;
268268
const withinDenialBudget = (): boolean => ++transientDenials <= maxTransientLockDenials;
269+
// Waiting can fail on the caller's own retry or deadline limits. Classifying
270+
// a denial as contention must not cost them the original diagnosis, so hand
271+
// the denial back when no further attempt can be scheduled.
272+
const retryOrRethrowDenial = async (denial: unknown): Promise<void> => {
273+
try {
274+
await waitForRetry();
275+
} catch (waitError) {
276+
if ((waitError as NodeJS.ErrnoException).code === "file_lock_timeout") throw denial;
277+
throw waitError;
278+
}
279+
};
269280
const waitForRetry = async (): Promise<void> => {
270281
const elapsed = Date.now() - startedAt;
271282
if (
@@ -383,7 +394,7 @@ export function createSidecarLockManager(key: string) {
383394
});
384395
}
385396
if (lockFileCreateDenied && withinDenialBudget()) {
386-
await waitForRetry();
397+
await retryOrRethrowDenial(err);
387398
continue;
388399
}
389400
if ((err as { code?: unknown }).code !== "EEXIST") {
@@ -404,7 +415,7 @@ export function createSidecarLockManager(key: string) {
404415
} catch (readErr) {
405416
if (!isTransientLockFileDenial(readErr, lockPath) || !withinDenialBudget())
406417
throw readErr;
407-
await waitForRetry();
418+
await retryOrRethrowDenial(readErr);
408419
continue;
409420
}
410421
if (!snapshot) {

‎test/sidecar-lock-regression.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ describe("sidecar lock regressions", () => {
3737
{ code: "EPERM", errno: -4048, syscall: "open", path: lockPath },
3838
);
3939

40+
it.runIf(process.platform === "win32")("keeps the lock-file EPERM when no retry is left", async () => {
41+
// Classifying the denial as contention must not cost the caller the
42+
// diagnosis: with no retry budget the original EPERM has to survive
43+
// instead of being replaced by the loop's timeout error.
44+
const base = await fsp.realpath(await tempRoot("fs-safe-sidecar-eperm-exhausted-"));
45+
const targetPath = path.join(base, "state.json");
46+
const lockPath = `${targetPath}.lock`;
47+
configureFsSafeNative({ mode: "off" });
48+
const realOpen = fsp.open.bind(fsp) as typeof fsp.open;
49+
50+
vi.spyOn(fsp, "open").mockImplementation((async (...args: Parameters<typeof fsp.open>) => {
51+
if (args[0] === lockPath && args[1] === "wx") throw denial(lockPath);
52+
return await realOpen(...args);
53+
}) as typeof fsp.open);
54+
55+
await expect(
56+
acquireFileLock(targetPath, {
57+
managerKey: `eperm-exhausted-${Date.now()}-${Math.random()}`,
58+
staleMs: 60_000,
59+
timeoutMs: 1_000,
60+
retry: { retries: 0 },
61+
payload: async () => ({ pid: process.pid }),
62+
}),
63+
).rejects.toMatchObject({ code: "EPERM", path: lockPath });
64+
});
65+
4066
it.runIf(process.platform === "win32")("propagates an EPERM that names the lock parent", async () => {
4167
// createNativeExclusiveFile() opens dirname(lockPath) before the exclusive
4268
// create, so a denial can name the parent. Only the teardown window on the

0 commit comments

Comments
 (0)