Skip to content

Commit

Permalink
retry lock acquisition if lock gets released before stat
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Nov 15, 2024
1 parent b0ddb36 commit ce5e4f0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/libraries/FileLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ export async function lockFile(path: string): Promise<() => Promise<void>> {
return setupMTimeEditor(lockPath)
} catch (e) {
if (e.code === 'EEXIST') {
const stat = await fsPromises.stat(lockPath)
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
return setupMTimeEditor(lockPath)
} else {
throw 'LOCKED'
try {
const stat = await fsPromises.stat(lockPath)
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
return setupMTimeEditor(lockPath)
} else {
throw 'LOCKED'
}
} catch (e) {
if (e.code === 'ENOENT') {
//This will run if the lock gets released after the EEXIST error is thrown but before the stat is checked.
//If this is the case, the lock acquisition should be retried.
return await lockFile(path)
} else {
throw e
}
}
} else {
throw e
Expand Down

0 comments on commit ce5e4f0

Please sign in to comment.