* Acquire the lock. Throws if the lock is held by another live process. * * A free lock costs one create. A stale one, whose PID is dead or missing, * is replaced only by the process holding ` .break` (itself a * FileLock), after reading it again there (#2979). Before, two processes
()
| 486 | * write as the only writer. |
| 487 | */ |
| 488 | acquire(): void { |
| 489 | const refusal = this.tryAcquire(0); |
| 490 | if (refusal) { |
| 491 | const who = refusal.state === 'held' && refusal.pid !== undefined ? ` (PID ${refusal.pid})` : ''; |
| 492 | throw new Error( |
| 493 | `CodeGraph database is locked by another process${who}. ` + |
| 494 | `If this is stale, run 'codegraph unlock' or delete ${this.lockPath}` |
| 495 | ); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /** Null once the lock is ours; otherwise what kept us out. */ |
| 500 | private tryAcquire(depth: number): LockHolder | null { |
no test coverage detected