Gathering detailed insights and metrics for proper-lockfile
Gathering detailed insights and metrics for proper-lockfile
Gathering detailed insights and metrics for proper-lockfile
Gathering detailed insights and metrics for proper-lockfile
An inter-process and inter-machine lockfile utility that works on a local or network file system.
npm install proper-lockfile
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
241 Stars
193 Commits
39 Forks
12 Watching
2 Branches
11 Contributors
Updated on 21 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-3.2%
444,146
Compared to previous day
Last week
2.9%
2,430,422
Compared to previous week
Last month
8.5%
10,133,288
Compared to previous month
Last year
19.4%
104,484,829
Compared to previous year
An inter-process and inter-machine lockfile utility that works on a local or network file system.
$ npm install proper-lockfile
There are various ways to achieve file locking.
This library utilizes the mkdir
strategy which works atomically on any kind of file system, even network based ones.
The lockfile path is based on the file path you are trying to lock by suffixing it with .lock
.
When a lock is successfully acquired, the lockfile's mtime
(modified time) is periodically updated to prevent staleness. This allows to effectively check if a lock is stale by checking its mtime
against a stale threshold. If the update of the mtime fails several times, the lock might be compromised. The mtime
is supported in almost every filesystem
.
This library is similar to lockfile but the latter has some drawbacks:
open
with O_EXCL
flag which has problems in network file systems. proper-lockfile
uses mkdir
which doesn't have this issue.O_EXCL is broken on NFS file systems; programs which rely on it for performing locking tasks will contain a race condition.
The lockfile staleness check is done via ctime
(creation time) which is unsuitable for long running processes. proper-lockfile
constantly updates lockfiles mtime
to do proper staleness check.
It does not check if the lockfile was compromised which can lead to undesirable situations. proper-lockfile
checks the lockfile when updating the mtime
.
It has a default value of 0
for the stale option which isn't good because any crash or process kill that the package can't handle gracefully will leave the lock active forever.
proper-lockfile
does not detect cases in which:
lockfile
is manually removed and someone else acquires the lock right afterstale
/update
values are being used for the same file, possibly causing two locks to be acquired on the same fileproper-lockfile
detects cases in which:
lockfile
failAs you see, the first two are a consequence of bad usage. Technically, it was possible to detect the first two but it would introduce complexity and eventual race conditions.
Tries to acquire a lock on file
or rejects the promise on error.
If the lock succeeds, a release
function is provided that should be called when you want to release the lock. The release
function also rejects the promise on error (e.g. when the lock was already compromised).
Available options:
stale
: Duration in milliseconds in which the lock is considered stale, defaults to 10000
(minimum value is 5000
)update
: The interval in milliseconds in which the lockfile's mtime
will be updated, defaults to stale/2
(minimum value is 1000
, maximum value is stale/2
)retries
: The number of retries or a retry options object, defaults to 0
realpath
: Resolve symlinks using realpath, defaults to true
(note that if true
, the file
must exist previously)fs
: A custom fs to use, defaults to graceful-fs
onCompromised
: Called if the lock gets compromised, defaults to a function that simply throws the error which will probably cause the process to dielockfilePath
: Custom lockfile path. e.g.: If you want to lock a directory and create the lock file inside it, you can pass file
as <dir path>
and options.lockfilePath
as <dir path>/dir.lock
1const lockfile = require('proper-lockfile'); 2 3lockfile.lock('some/file') 4.then((release) => { 5 // Do something while the file is locked 6 7 // Call the provided release function when you're done, 8 // which will also return a promise 9 return release(); 10}) 11.catch((e) => { 12 // either lock could not be acquired 13 // or releasing it failed 14 console.error(e) 15}); 16 17// Alternatively, you may use lockfile('some/file') directly.
Releases a previously acquired lock on file
or rejects the promise on error.
Whenever possible you should use the release
function instead (as exemplified above). Still there are cases in which it's hard to keep a reference to it around code. In those cases unlock()
might be handy.
Available options:
realpath
: Resolve symlinks using realpath, defaults to true
(note that if true
, the file
must exist previously)fs
: A custom fs to use, defaults to graceful-fs
lockfilePath
: Custom lockfile path. e.g.: If you want to lock a directory and create the lock file inside it, you can pass file
as <dir path>
and options.lockfilePath
as <dir path>/dir.lock
1const lockfile = require('proper-lockfile'); 2 3lockfile.lock('some/file') 4.then(() => { 5 // Do something while the file is locked 6 7 // Later.. 8 return lockfile.unlock('some/file'); 9});
Check if the file is locked and its lockfile is not stale, rejects the promise on error.
Available options:
stale
: Duration in milliseconds in which the lock is considered stale, defaults to 10000
(minimum value is 5000
)realpath
: Resolve symlinks using realpath, defaults to true
(note that if true
, the file
must exist previously)fs
: A custom fs to use, defaults to graceful-fs
lockfilePath
: Custom lockfile path. e.g.: If you want to lock a directory and create the lock file inside it, you can pass file
as <dir path>
and options.lockfilePath
as <dir path>/dir.lock
1const lockfile = require('proper-lockfile'); 2 3lockfile.check('some/file') 4.then((isLocked) => { 5 // isLocked will be true if 'some/file' is locked, false otherwise 6});
Sync version of .lock()
.
Returns the release
function or throws on error.
Sync version of .unlock()
.
Throws on error.
Sync version of .check()
.
Returns a boolean or throws on error.
proper-lockfile
automatically removes locks if the process exits, except if the process is killed with SIGKILL or it crashes due to a VM fatal error (e.g.: out of memory).
$ npm test
$ npm test -- --watch
during development
The test suite is very extensive. There's even a stress test to guarantee exclusiveness of locks.
Released under the MIT License.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/24 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
46 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More