MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / refreshDaemonLock

Function refreshDaemonLock

src/mcp/daemon.ts:643–672  ·  view source on GitHub ↗
(
  pidPath: string,
  initialContents: string,
  lock: DaemonLockInfo,
  platform: NodeJS.Platform = process.platform,
)

Source from the content-addressed store, hash-verified

641 * still abort startup.
642 */
643export function refreshDaemonLock(
644 pidPath: string,
645 initialContents: string,
646 lock: DaemonLockInfo,
647 platform: NodeJS.Platform = process.platform,
648): void {
649 const tmpPid = `${pidPath}.${process.pid}.bound`;
650 try {
651 fs.writeFileSync(tmpPid, encodeLockInfo(lock), { mode: 0o600 });
652 for (let attempt = 0; ; attempt++) {
653 // A retry must never replace a record whose owner changed while waiting.
654 if (fs.readFileSync(pidPath, 'utf8') !== initialContents) {
655 throw new Error('Lost daemon lock ownership after binding.');
656 }
657 try {
658 fs.renameSync(tmpPid, pidPath);
659 return;
660 } catch (error) {
661 const code = (error as NodeJS.ErrnoException).code;
662 const delay = LOCK_REFRESH_RETRY_DELAYS_MS[attempt];
663 if (platform !== 'win32' || !['EPERM', 'EACCES', 'EBUSY'].includes(code ?? '') || delay === undefined) {
664 throw error;
665 }
666 Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay);
667 }
668 }
669 } finally {
670 try { fs.unlinkSync(tmpPid); } catch { /* renamed, or best-effort cleanup */ }
671 }
672}
673
674/**
675 * Result of `tryAcquireDaemonLock`. Either we got the lockfile (caller becomes

Callers 2

startMethod · 0.85

Calls 1

encodeLockInfoFunction · 0.90

Tested by

no test coverage detected