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

Method tryAcquire

src/utils.ts:524–554  ·  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

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