Skip to content

Commit f2afd48

Browse files
committed
diag: #5635 Windows restore race (throwaway, not for merge)
Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 640a8c3 commit f2afd48

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎src/memory/__tests__/file-lock.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ describe("withFileLock", () => {
557557
};
558558
}
559559

560-
it("does not let a writer that created the lock while a live one was aside run beside its holder", async () => {
560+
it.each(Array.from({ length: 20 }, (_, i) => i))("does not let a writer that created the lock while a live one was aside run beside its holder %i", async () => {
561561
await writeStaleLock();
562562
vi.spyOn(console, "error").mockImplementation(() => {});
563563
const opens = watchOpens();
@@ -588,10 +588,10 @@ describe("withFileLock", () => {
588588
await Promise.race([thirdStarted, sawBreak]);
589589
});
590590

591-
await expect(
592-
withFileLock(lockPath, async () => "breaker ran", fast),
593-
).rejects.toThrow("Timed out");
591+
const breakerResult = await withFileLock(lockPath, async () => "breaker ran", fast).catch((e: unknown) => e);
594592
const outcome = await thirdOutcome;
593+
process.stderr.write(`DIAG5635 breaker=${breakerResult instanceof Error ? "timeout" : String(breakerResult)} thirdCalls=${third.mock.calls.length} successorStaged=${successor !== undefined} dir=${JSON.stringify(await fs.readdir(dir))}\n`);
594+
expect(breakerResult).toBeInstanceOf(Error);
595595

596596
// The third writer never ran beside the successor: its lock was
597597
// overwritten by the restored one, it noticed, and it went on waiting.

‎src/memory/index.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,21 @@ async function breakStaleLock(
486486
return false;
487487
}
488488
const asidePath = `${lockPath}.${randomBytes(8).toString("hex")}.stale`;
489-
await fs.rename(lockPath, asidePath);
489+
try {
490+
await fs.rename(lockPath, asidePath);
491+
} catch (e) {
492+
process.stderr.write(`DIAG5635 aside failed: ${(e as NodeJS.ErrnoException).code}\n`);
493+
throw e;
494+
}
490495
const taken = await fs.stat(asidePath, { bigint: true });
491496
if (taken.dev !== dev || taken.ino !== ino) {
492497
// Not the lock that was judged: a successor's live one. Put it back.
493-
await fs.rename(asidePath, lockPath);
498+
try {
499+
await fs.rename(asidePath, lockPath);
500+
} catch (e) {
501+
process.stderr.write(`DIAG5635 restore failed: ${(e as NodeJS.ErrnoException).code}\n`);
502+
throw e;
503+
}
494504
return false;
495505
}
496506
console.error(`Breaking stale memory file lock ${lockPath}`);

0 commit comments

Comments
 (0)