|
| 1 | +/* eslint-disable no-console */ |
1 | 2 | const fs = require('node:fs/promises') |
2 | 3 | const { rmdirSync } = require('node:fs') |
3 | 4 | const promiseRetry = require('promise-retry') |
@@ -143,18 +144,28 @@ async function maintainLock (lockPath) { |
143 | 144 | // fs.utimes operates on floating points seconds (directly, or via strings/Date objects), which may not match the underlying filesystem's mtime precision, meaning that we might read a slightly different mtime than we write. always round to the nearest second, since all filesystems support at least second precision |
144 | 145 | let mtime = Math.round(stats.mtimeMs / 1000) |
145 | 146 | const signal = controller.signal |
| 147 | + let touchCount = 0 |
146 | 148 |
|
147 | 149 | async function touchLock () { |
| 150 | + touchCount++ |
148 | 151 | try { |
| 152 | + const currentTouchCount = touchCount |
| 153 | + const start = Date.now() |
| 154 | + console.log(`${currentTouchCount} fs.stat start`) |
149 | 155 | const currentStats = (await fs.stat(lockPath)) |
| 156 | + console.log(`${currentTouchCount} fs.stat done in ${Date.now() - start}ms`) |
150 | 157 | const currentMtime = Math.round(currentStats.mtimeMs / 1000) |
151 | 158 | if (currentStats.ino !== stats.ino || currentMtime !== mtime) { |
| 159 | + console.log(`${currentTouchCount} expected ${currentStats.ino} === ${stats.ino} and ${currentMtime} === ${mtime}`) |
152 | 160 | throw new Error('Lock compromised') |
153 | 161 | } |
154 | 162 | mtime = Math.round(Date.now() / 1000) |
155 | 163 | // touch the lock, unless we just released it during this iteration |
156 | 164 | if (currentLocks.has(lockPath)) { |
| 165 | + const start2 = Date.now() |
| 166 | + console.log(`${currentTouchCount} fs.utimes start`) |
157 | 167 | await fs.utimes(lockPath, mtime, mtime) |
| 168 | + console.log(`${currentTouchCount} fs.utimes done in ${Date.now() - start2}ms`) |
158 | 169 | } |
159 | 170 | } catch (err) { |
160 | 171 | // stats mismatch or other fs error means the lock was compromised |
|
0 commit comments