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

Function refreshDaemonLock

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

Source from the content-addressed store, hash-verified

778 * still abort startup.
779 */
780export 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

Callers 2

startMethod · 0.85

Calls 1

encodeLockInfoFunction · 0.90

Tested by

no test coverage detected