Skip to content

Commit ce5e4f0

Browse files
retry lock acquisition if lock gets released before stat
1 parent b0ddb36 commit ce5e4f0

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/libraries/FileLock.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,21 @@ export async function lockFile(path: string): Promise<() => Promise<void>> {
5050
return setupMTimeEditor(lockPath)
5151
} catch (e) {
5252
if (e.code === 'EEXIST') {
53-
const stat = await fsPromises.stat(lockPath)
54-
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
55-
return setupMTimeEditor(lockPath)
56-
} else {
57-
throw 'LOCKED'
53+
try {
54+
const stat = await fsPromises.stat(lockPath)
55+
if (performance.now() - stat.mtime.getTime() > mtimeLimit) {
56+
return setupMTimeEditor(lockPath)
57+
} else {
58+
throw 'LOCKED'
59+
}
60+
} catch (e) {
61+
if (e.code === 'ENOENT') {
62+
//This will run if the lock gets released after the EEXIST error is thrown but before the stat is checked.
63+
//If this is the case, the lock acquisition should be retried.
64+
return await lockFile(path)
65+
} else {
66+
throw e
67+
}
5868
}
5969
} else {
6070
throw e

0 commit comments

Comments
 (0)