Skip to content

Instantly share code, notes, and snippets.

@timmclean
Last active December 31, 2015 20:49
Show Gist options
  • Save timmclean/8042936 to your computer and use it in GitHub Desktop.
Save timmclean/8042936 to your computer and use it in GitHub Desktop.
Race condition in isaacs/lockfile
Got a lock at 798ms
Got a lock at 800ms
Got a lock at 1794ms
Got a lock at 1794ms
Got a lock at 1795ms
Got a lock at 1796ms
Got a lock at 1797ms
Got a lock at 2794ms
Got a lock at 2795ms
Got a lock at 2796ms
Got a lock at 2797ms
Got a lock at 3794ms
Got a lock at 3794ms
Got a lock at 3795ms
Got a lock at 3795ms
Got a lock at 3795ms
var lockfile = require('lockfile');
var opts = {stale: 1000};
var startTime = Date.now();
var stopAfter = 5000;
lockfile.lock('test.lock', opts, function (err) {
if (err) throw err;
var stop = false;
function attemptToLock() {
if (stop) {
return;
}
lockfile.lock('test.lock', opts, function (err) {
if (err) {
attemptToLock();
return;
}
console.log("Got a lock at " + (Date.now() - startTime) + "ms");
});
}
setTimeout(function () {stop = true;}, stopAfter);
for (var i=0; i<16; i++) {
attemptToLock();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment