* Create the lock file with our PID already in it, so no reader ever finds * it empty: write a private file, then hard-link it into place, which fails * if the lock exists. Where hard links are missing, fall back to an * exclusive create, whose PID lands a moment later (see * EMPTY_LOCK_
()
| 566 | * EMPTY_LOCK_GRACE_MS). False when the lock already exists. |
| 567 | */ |
| 568 | private create(): boolean { |
| 569 | const pid = String(process.pid); |
| 570 | const tmp = `${this.lockPath}.${process.pid}.${threadId}.tmp`; |
| 571 | try { |
| 572 | fs.writeFileSync(tmp, pid); |
| 573 | fs.linkSync(tmp, this.lockPath); |
| 574 | return true; |
| 575 | } catch (err) { |
| 576 | if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false; |
| 577 | // No hard links here (or no private file): create in place instead. |
| 578 | } finally { |
| 579 | try { fs.unlinkSync(tmp); } catch { /* never written */ } |
| 580 | } |
| 581 | try { |
| 582 | fs.writeFileSync(this.lockPath, pid, { flag: 'wx' }); |
| 583 | return true; |
| 584 | } catch (err) { |
| 585 | if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false; |
| 586 | throw err; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | private readHolder(): LockHolder { |
| 591 | let content: string; |