|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +const lockFile = path.join(__dirname, '..', '.rebuild-lock'); |
| 5 | + |
| 6 | +function isProcessAlive(pid: number): boolean { |
| 7 | + try { |
| 8 | + process.kill(pid, 0); |
| 9 | + return true; |
| 10 | + } catch (e) { |
| 11 | + // EPERM: process exists but we can't signal it — treat as alive |
| 12 | + // ESRCH: no such process — stale lock |
| 13 | + return (e as NodeJS.ErrnoException).code === 'EPERM'; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function tryAcquire(): number | undefined { |
| 18 | + let fd: number | undefined; |
| 19 | + try { |
| 20 | + fd = fs.openSync(lockFile, 'wx'); |
| 21 | + fs.writeSync(fd, String(process.pid)); |
| 22 | + return fd; |
| 23 | + } catch { |
| 24 | + try { if (fd !== undefined) fs.closeSync(fd); } catch { /* ignore */ } |
| 25 | + return undefined; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function release(fd: number): void { |
| 30 | + try { fs.closeSync(fd); } catch { /* ignore */ } |
| 31 | + try { fs.unlinkSync(lockFile); } catch { /* ignore */ } |
| 32 | +} |
| 33 | + |
| 34 | +function clearStaleLock(): void { |
| 35 | + try { |
| 36 | + const pid = parseInt(fs.readFileSync(lockFile, 'utf8'), 10); |
| 37 | + if (!isNaN(pid) && !isProcessAlive(pid)) { |
| 38 | + fs.unlinkSync(lockFile); |
| 39 | + } |
| 40 | + } catch { /* ignore — another waiter may have already cleared it */ } |
| 41 | +} |
| 42 | + |
| 43 | +function waitForLockRelease(): void { |
| 44 | + const timer = new Int32Array(new SharedArrayBuffer(4)); |
| 45 | + while (fs.existsSync(lockFile)) { |
| 46 | + clearStaleLock(); |
| 47 | + Atomics.wait(timer, 0, 0, 250); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Runs `work` while holding an exclusive lock on `lockFile`. If another caller |
| 53 | + * holds the lock, blocks until it is released (or until a stale lock from a |
| 54 | + * crashed process is detected and cleared). |
| 55 | + */ |
| 56 | +export function withRebuildLock(work: () => void): void { |
| 57 | + let fd = tryAcquire(); |
| 58 | + |
| 59 | + if (fd === undefined) { |
| 60 | + waitForLockRelease(); |
| 61 | + // Try once more after waiting — another waiter may have cleared a stale |
| 62 | + // lock and then done the rebuild itself, so we may not need to rebuild. |
| 63 | + fd = tryAcquire(); |
| 64 | + } |
| 65 | + |
| 66 | + if (fd !== undefined) { |
| 67 | + try { |
| 68 | + work(); |
| 69 | + } finally { |
| 70 | + release(fd); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments