Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
fix: Avoid race condition error on multi-process communication (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Aug 31, 2020
1 parent 7d9575b commit 4b4f85e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ async function toInMemoryBase64 (srcPath, opts = {}) {
*/
function getLockFileGuard (lockFile, opts = {timeout: 120}) {
const lock = B.promisify(_lockfile.lock);
const performLock = async () => await lock(lockFile, {wait: opts.timeout * 1000});
const check = B.promisify(_lockfile.check);
const unlock = B.promisify(_lockfile.unlock);

Expand All @@ -469,9 +470,17 @@ function getLockFileGuard (lockFile, opts = {timeout: 120}) {
// on the same spin of the event loop can also initiate a lock. If the lockfile does exist
// then just use the regular async 'lock' method which will wait on the lock.
if (!_lockfile.checkSync(lockFile)) {
_lockfile.lockSync(lockFile);
try {
_lockfile.lockSync(lockFile);
} catch (e) {
// Avoid cross-process race condition
if (!_.includes(e.message, 'EEXIST')) {
throw e;
}
await performLock();
}
} else {
await lock(lockFile, {wait: opts.timeout * 1000});
await performLock();
}
} catch (e) {
throw new Error(`Could not acquire lock on ${lockFile}. Original error: ${e}`);
Expand Down

0 comments on commit 4b4f85e

Please sign in to comment.