Atomic everywhere
The lock is a directory created with mkdir, which is atomic on local disks, NFS and Windows. No flock, no native addon.
Zero dependencies. Node, Bun and Windows tested.
lockbadger lets many processes share one file safely. It knows who holds the lock, notices when that process dies and takes over at once, and never steals a lock from a process that is still alive.
npm i lockbadger
Sheet 02
Each lane is one process on a timing diagram. It waits for the lock (dotted), holds it (high, in brass), writes the counter plus one, and releases. Kill the holder and watch who takes over, and when.
This is a simulation. It runs the real lockbadger algorithm (src/core.js, inlined into this page at build time) against an in memory file system and a virtual clock. No real files or processes are involved.
lock is free
Sheet 03
Most lockfile code on npm decides that a lock is abandoned only by its age. When the holder crashes, everybody waits for the full stale timeout, and the code that removes a stale lock rarely checks that it is still the same lock. lockbadger records who owns the lock and checks it at every step.
The lock is a directory created with mkdir, which is atomic on local disks, NFS and Windows. No flock, no native addon.
owner.json holds the pid, hostname, start time and a random token. Release and refresh only touch a lock whose token is theirs.
A holder on this host whose pid is gone is replaced right away. Other hosts fall back to mtime staleness.
Stale locks are removed under a short break mutex, renamed away first and checked by token, so a live lock is never taken.
A held lock refreshes its mtime in the background with an unref'd timer, so your process still exits on its own.
AbortSignal on every wait, a signal that fires if the lock is lost, and cleanup of held locks when the process exits.
Sheet 04
Three steps, and every process that uses the same path takes its turn.
import { lock } from 'lockbadger'; const release = await lock('data/report.json');try { // only one process at a time gets here await updateReport();} finally { await release();}Sheet 05
lock(path, options?) => Promise<release>tryLock(path, options?) => Promise<release | null>isLocked(path, options?) => Promise<boolean>withLock(path, fn, options?) => Promise<T>release() => Promise<void>LockErrorELOCKED ECOMPROMISED ERELEASED| name | default | Options |
|---|---|---|
stale | 10000 | ms without refresh before a lock counts as abandoned |
update | stale / 2 | refresh interval |
retries | Infinity | attempts before ELOCKED (tryLock: always 0) |
retryDelay | 20..500 | ms between attempts, or a function of the attempt |
signal | undefined | cancels the wait |
onCompromised | warning | called if the lock is lost (default: process warning) |
lockPath | path.lock | where the lock directory lives |
checkPid | true | same host dead pid detection |
Sheet 06
Measured against proper-lockfile 4.1.2, the most used lockfile package, installed in a scratch folder. Same machine, same disk.
AMD Ryzen 7 7800X3D 8-Core Processor, win32, Node v24.16.0
per second, higher is better
ms wall time, lower is better
ms, lower is better
proper-lockfile is faster at the uncontended loop: lockbadger writes and verifies an owner file on every cycle, which costs a few file system calls. Under contention and after a crash, lockbadger wins.
Sheet 07
proper-lockfile 4.1.2 is a solid, widely used package. These are the differences we verified by reading its source and running both.
| Feature | lockbadger 0.1.1 | proper-lockfile 4.1.2 |
|---|---|---|
| Atomic mkdir lock directory | ||
| Owner metadata (pid, host, token) | ||
| Instant takeover when the holder process is gone | ||
| Stale removal checks it is still the same lock | ||
| AbortSignal support | ||
| Lock a path that does not exist yet | ||
| Default when a lock is lost | warning | throws (uncaught) |
| Synchronous API | ||
| Runtime dependencies | 0 | 3 |
The ESM entry is the source itself, with JSDoc. No bundler involved.
| Runtime | Status |
|---|---|
| Node 18, 20, 22, 24 | Tested in CI on Linux, plus Windows and macOS on Node 24 |
| Bun | Smoke test passes, multi process counter included (Bun 1.3.14 on Windows) |
| Deno 2 | CI Smoke test wired in CI through node: imports; not run on the author's machine |
| Browsers | No: there is no file system. The simulation above uses an in memory one. |
Sheet 08