(pidPath: string, expectedDeadPid?: number)
| 640 | * pid. Returns true when the stale lock is gone (or was already gone). |
| 641 | */ |
| 642 | export function clearStaleDaemonLock(pidPath: string, expectedDeadPid?: number): boolean { |
| 643 | try { |
| 644 | const raw = fs.readFileSync(pidPath, 'utf8'); |
| 645 | const info = decodeLockInfo(raw); |
| 646 | if (info) { |
| 647 | // A different pid took over since we read it — not ours to clear. |
| 648 | if (expectedDeadPid !== undefined && info.pid !== expectedDeadPid) return false; |
| 649 | // Holder is actually alive — never clear a live daemon's lock. |
| 650 | if (info.pid > 0 && isProcessAlive(info.pid)) return false; |
| 651 | } |
| 652 | fs.unlinkSync(pidPath); |
| 653 | return true; |
| 654 | } catch (err: unknown) { |
| 655 | const e = err as NodeJS.ErrnoException; |
| 656 | if (e.code === 'ENOENT') return true; // already gone |
| 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Probe whether `pid` is currently alive (signal-0). Treats EPERM as alive on |
no test coverage detected