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

Method tryAcquire

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

498
499 /** Null once the lock is ours; otherwise what kept us out. */
500 private tryAcquire(depth: number): LockHolder | null {
501 if (this.create()) return this.taken();
502 let holder = this.readHolder();
503 if (holder.state === 'free') {
504 // Released between our create and our read.
505 if (this.create()) return this.taken();
506 holder = this.readHolder();
507 }
508 // A long sync can hold the lock for hours. Its age says nothing about
509 // whether the writer is still alive; only a dead PID is stale.
510 if (holder.state !== 'stale') return holder;
511 if (depth >= MAX_LOCK_BREAK_DEPTH) return { state: 'held' };
512 const breaker = new FileLock(`${this.lockPath}.break`);
513 // Refused: a live process is replacing this stale lock right now.
514 const breaking = breaker.tryAcquire(depth + 1);
515 if (breaking) return breaking;
516 try {
517 // What we read before holding the break lock may be out of date: another
518 // process may have replaced the stale lock, or released its own, since.
519 const current = this.readHolder();
520 if (current.state === 'held') return current;
521 // Nobody else replaces a stale lock while we hold the break lock, and no
522 // create succeeds while the file exists, so this unlinks the stale lock.
523 if (current.state === 'stale') {
524 try { fs.unlinkSync(this.lockPath); } catch { /* gone, or the create below says who has it */ }
525 }
526 return this.create() ? this.taken() : this.readHolder();
527 } finally {
528 breaker.release();
529 }
530 }
531
532 private taken(): null {
533 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