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

Function refreshDaemonLock

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

Source from the content-addressed store, hash-verified

765 * still abort startup.
766 */
767export function refreshDaemonLock(
768 pidPath: string,
769 initialContents: string,
770 lock: DaemonLockInfo,
771 platform: NodeJS.Platform = process.platform,
772): void {
773 const tmpPid = `${pidPath}.${process.pid}.bound`;
774 try {
775 fs.writeFileSync(tmpPid, encodeLockInfo(lock), { mode: 0o600 });
776 for (let attempt = 0; ; attempt++) {
777 // A retry must never replace a record whose owner changed while waiting.
778 if (fs.readFileSync(pidPath, 'utf8') !== initialContents) {
779 throw new Error('Lost daemon lock ownership after binding.');
780 }
781 try {
782 fs.renameSync(tmpPid, pidPath);
783 return;
784 } catch (error) {
785 const code = (error as NodeJS.ErrnoException).code;
786 const delay = LOCK_REFRESH_RETRY_DELAYS_MS[attempt];
787 if (platform !== 'win32' || !['EPERM', 'EACCES', 'EBUSY'].includes(code ?? '') || delay === undefined) {
788 throw error;
789 }
790 Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay);
791 }
792 }
793 } finally {
794 try { fs.unlinkSync(tmpPid); } catch { /* renamed, or best-effort cleanup */ }
795 }
796}
797
798/**
799 * 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