Last active
December 31, 2015 20:49
-
-
Save timmclean/8042936 to your computer and use it in GitHub Desktop.
Race condition in isaacs/lockfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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