Skip to content

Commit ee15132

Browse files
authored
Merge pull request #5 from seabearDEV/copilot/sub-pr-2-another-one
Replace busy-wait spin loop with Atomics.wait() in fileLock.ts
2 parents f092477 + 700141c commit ee15132

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/utils/fileLock.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs from 'fs';
22

33
const LOCK_STALE_MS = 10_000; // Consider lock stale after 10 seconds
4+
// Reusable buffer for Atomics.wait()-based sleep (avoids per-call allocation)
5+
const _sleepBuf = new Int32Array(new SharedArrayBuffer(4));
46

57
/**
68
* Acquire an advisory file lock using a .lock file.
@@ -33,10 +35,9 @@ export function acquireLock(filePath: string, maxRetries = 5): void {
3335
}
3436

3537
if (attempt < maxRetries) {
36-
// Busy-wait with backoff (1ms, 2ms, 4ms, 8ms, 16ms)
38+
// Sleep with exponential backoff (1ms, 2ms, 4ms, 8ms, 16ms)
3739
const waitMs = Math.pow(2, attempt);
38-
const start = Date.now();
39-
while (Date.now() - start < waitMs) { /* spin */ }
40+
Atomics.wait(_sleepBuf, 0, 0, waitMs);
4041
continue;
4142
}
4243

0 commit comments

Comments
 (0)