Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not subscribe to process signal handlers unless necessary #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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