( pidPath: string, initialContents: string, lock: DaemonLockInfo, platform: NodeJS.Platform = process.platform, )
| 778 | * still abort startup. |
| 779 | */ |
| 780 | export function refreshDaemonLock( |
| 781 | pidPath: string, |
| 782 | initialContents: string, |
| 783 | lock: DaemonLockInfo, |
| 784 | platform: NodeJS.Platform = process.platform, |
| 785 | ): void { |
| 786 | const tmpPid = `${pidPath}.${process.pid}.bound`; |
| 787 | try { |
| 788 | fs.writeFileSync(tmpPid, encodeLockInfo(lock), { mode: 0o600 }); |
| 789 | for (let attempt = 0; ; attempt++) { |
| 790 | // A retry must never replace a record whose owner changed while waiting. |
| 791 | if (fs.readFileSync(pidPath, 'utf8') !== initialContents) { |
| 792 | throw new Error('Lost daemon lock ownership after binding.'); |
| 793 | } |
| 794 | try { |
| 795 | fs.renameSync(tmpPid, pidPath); |
| 796 | return; |
| 797 | } catch (error) { |
| 798 | const code = (error as NodeJS.ErrnoException).code; |
| 799 | const delay = LOCK_REFRESH_RETRY_DELAYS_MS[attempt]; |
| 800 | if (platform !== 'win32' || !['EPERM', 'EACCES', 'EBUSY'].includes(code ?? '') || delay === undefined) { |
| 801 | throw error; |
| 802 | } |
| 803 | Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay); |
| 804 | } |
| 805 | } |
| 806 | } finally { |
| 807 | try { fs.unlinkSync(tmpPid); } catch { /* renamed, or best-effort cleanup */ } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Result of `tryAcquireDaemonLock`. Either we got the lockfile (caller becomes |
no test coverage detected