Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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