Skip to content

Commit 8ae4909

Browse files
committed
fix(dsh): enforce writer lock deadline
1 parent 00438d7 commit 8ae4909

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎src/integrations/writer-lock.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ export async function withIntegrationWriterLock<T>(
6868
if (errorCode(error) !== "EEXIST") {
6969
throw new IntegrationWriterLockIOError(lockPath, "acquire", error);
7070
}
71-
if (seams.now() - startedAt >= LOCK_DEADLINE_MS) {
71+
const elapsedMs = seams.now() - startedAt;
72+
if (elapsedMs >= LOCK_DEADLINE_MS) {
7273
throw new IntegrationWriterLockBusyError(lockPath);
7374
}
74-
const delayMs = LOCK_DELAYS_MS[Math.min(delayIndex, LOCK_DELAYS_MS.length - 1)]!;
75+
const backoffMs = LOCK_DELAYS_MS[Math.min(delayIndex, LOCK_DELAYS_MS.length - 1)]!;
76+
// Keep the final retry inside the advertised two-second deadline.
77+
const delayMs = Math.min(backoffMs, LOCK_DEADLINE_MS - elapsedMs);
7578
delayIndex += 1;
7679
await seams.delay(delayMs);
7780
}

‎tests/dsh-writer-lock.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ describe("DSH sibling writer lock", () => {
5151
test("times out as typed busy without deleting a contender", async () => {
5252
let now = 0;
5353
let removes = 0;
54+
const delays: number[] = [];
5455
const seams: IntegrationWriterLockSeams = {
5556
writeFile: async () => { throw eexist(); },
5657
removeFile: async () => { removes += 1; },
5758
now: () => now,
58-
delay: async ms => { now += ms; },
59+
delay: async ms => { delays.push(ms); now += ms; },
5960
pid: 7,
6061
};
6162
await expect(withIntegrationWriterLock("/tmp/settings.yaml", async () => undefined, seams))
6263
.rejects.toBeInstanceOf(IntegrationWriterLockBusyError);
63-
expect(now).toBe(2_100);
64+
expect(now).toBe(2_000);
65+
expect(delays.at(-1)).toBe(100);
66+
expect(Math.max(...delays)).toBe(200);
6467
expect(removes).toBe(0);
6568
});
6669

‎tests/management-integration-routes.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ describe("cross-process integration writer lock mapping", () => {
449449
code: "integration_mutation_busy",
450450
clientId: "dsh",
451451
});
452-
expect(now).toBe(2_100);
452+
expect(now).toBe(2_000);
453453
expect(existsSync(configPath)).toBe(false);
454454
});
455455

0 commit comments

Comments
 (0)