fix: make workspace registry lock process-bound

This commit is contained in:
2026-08-03 22:42:57 +02:00
parent 553bb41138
commit e2d1117614
3 changed files with 128 additions and 67 deletions
@@ -137,3 +137,44 @@ test("maps lock filesystem failures to a stable redacted error", async () => {
});
expect((error as Error).message).not.toContain(file);
});
test("releases its queue after a malformed lock failure so a later attempt can acquire", async () => {
const root = mkdtempSync(join(tmpdir(), "thoth-workspace-git-queue-"));
temporaryRoots.push(root);
const locks = join(root, "locks");
mkdirSync(join(locks, "repository.lock"), { recursive: true });
const lock = new WorkspaceRepositoryLock(locks);
await expect(lock.run(async () => "unreachable")).rejects.toMatchObject({ code: "git_unavailable" });
rmSync(join(locks, "repository.lock"), { recursive: true, force: true });
const result = await Promise.race([
lock.run(async () => "recovered"),
new Promise<string>((resolve) => setTimeout(() => resolve("timed out"), 250)),
]);
expect(result).toBe("recovered");
});
test("parallel contenders recover a stale lock file without overlapping critical sections", async () => {
const root = mkdtempSync(join(tmpdir(), "thoth-workspace-git-contenders-"));
temporaryRoots.push(root);
const locks = join(root, "locks");
mkdirSync(locks);
writeFileSync(join(locks, "repository.lock"), JSON.stringify({ pid: 999_999_999 }));
const first = new WorkspaceRepositoryLock(locks);
const second = new WorkspaceRepositoryLock(locks);
let active = 0;
let maximum = 0;
const critical = async () => {
active += 1;
maximum = Math.max(maximum, active);
await new Promise((resolve) => setTimeout(resolve, 25));
active -= 1;
};
const results = await Promise.allSettled([first.run(critical), second.run(critical)]);
expect(results.filter((result) => result.status === "fulfilled")).toHaveLength(1);
expect(results.filter((result) => result.status === "rejected")).toHaveLength(1);
expect(maximum).toBe(1);
});