Skip to content

Commit

Permalink
fix: do not subscribe to process signal handlers unless necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Dec 20, 2022
1 parent 9f8c303 commit 0ae637e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/lockfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ function lock(file, options, callback) {
lastUpdate: Date.now(),
};

ensureTeardown();

// We must keep the lock fresh to avoid staleness
updateLock(file, options);

Expand Down Expand Up @@ -326,15 +328,24 @@ function getLocks() {
return locks;
}

// Remove acquired locks on exit
/* istanbul ignore next */
onExit(() => {
for (const file in locks) {
const options = locks[file].options;
let hasTeardown = false;

try { options.fs.rmdirSync(getLockFile(file, options)); } catch (e) { /* Empty */ }
}
});
function ensureTeardown() {
if (hasTeardown) { return; }
hasTeardown = true;

// Remove acquired locks on exit.
// Do not setup exit listeners unless there's a need since they subscribe to
// node.js signal handlers and change default node.js behavior when handling signals.
/* istanbul ignore next */
onExit(() => {
for (const file in locks) {
const options = locks[file].options;

try { options.fs.rmdirSync(getLockFile(file, options)); } catch (e) { /* Empty */ }
}
});
}

module.exports.lock = lock;
module.exports.unlock = unlock;
Expand Down

0 comments on commit 0ae637e

Please sign in to comment.