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

Method tryAcquire

src/utils.ts:525–555  ·  view source on GitHub ↗

Null once the lock is ours; otherwise what kept us out.

(depth: number)

Source from the content-addressed store, hash-verified

523
524 /** Null once the lock is ours; otherwise what kept us out. */
525 private tryAcquire(depth: number): LockHolder | null {
526 if (this.create()) return this.taken();
527 let holder = this.readHolder();
528 if (holder.state === 'free') {
529 // Released between our create and our read.
530 if (this.create()) return this.taken();
531 holder = this.readHolder();
532 }
533 // A long sync can hold the lock for hours. Its age says nothing about
534 // whether the writer is still alive; only a dead PID is stale.
535 if (holder.state !== 'stale') return holder;
536 if (depth >= MAX_LOCK_BREAK_DEPTH) return { state: 'held' };
537 const breaker = new FileLock(`${this.lockPath}.break`);
538 // Refused: a live process is replacing this stale lock right now.
539 const breaking = breaker.tryAcquire(depth + 1);
540 if (breaking) return breaking;
541 try {
542 // What we read before holding the break lock may be out of date: another
543 // process may have replaced the stale lock, or released its own, since.
544 const current = this.readHolder();
545 if (current.state === 'held') return current;
546 // Nobody else replaces a stale lock while we hold the break lock, and no
547 // create succeeds while the file exists, so this unlinks the stale lock.
548 if (current.state === 'stale') {
549 try { fs.unlinkSync(this.lockPath); } catch { /* gone, or the create below says who has it */ }
550 }
551 return this.create() ? this.taken() : this.readHolder();
552 } finally {
553 breaker.release();
554 }
555 }
556
557 private taken(): null {
558 this.held = true;

Callers 1

acquireMethod · 0.95

Calls 4

createMethod · 0.95
takenMethod · 0.95
readHolderMethod · 0.95
releaseMethod · 0.95

Tested by

no test coverage detected