Zero dependencies. Node, Bun and Windows tested.

File locks that survive crashes and never deadlock.

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
kB min + gzip
3.69
dependencies
0
ms to recover from a killed holder
5
counter.lock/ 4 Vault mkdir(dir) stale 1 lock = 1 dir 3 Owner plate owner.json 2 Token token: 9f3a61c2 1 Mtime dial update = stale / 2 the guard
FIG. 1Exploded view of one lock. Scroll and it assembles.

Sheet 02

Processes fighting over one file

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.

Speed
Stale timeout
3.0 s
New processes run on another hostSame host: a dead pid is detected at once. Other host: only the mtime tells, so takeover waits for the stale timeout.
counter.txt
0
increments done
0
lost updates
0
takeovers
0

lock is free

Event log

Sheet 03

Why another lock library?

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.

1

Atomic everywhere

The lock is a directory created with mkdir, which is atomic on local disks, NFS and Windows. No flock, no native addon.

2

Knows the owner

owner.json holds the pid, hostname, start time and a random token. Release and refresh only touch a lock whose token is theirs.

3

Instant recovery

A holder on this host whose pid is gone is replaced right away. Other hosts fall back to mtime staleness.

4

Takeover that cannot steal

Stale locks are removed under a short break mutex, renamed away first and checked by token, so a live lock is never taken.

5

Stays fresh, quietly

A held lock refreshes its mtime in the background with an unref'd timer, so your process still exits on its own.

6

Cancel and clean up

AbortSignal on every wait, a signal that fires if the lock is lost, and cleanup of held locks when the process exits.

Sheet 04

How to lock a file in Node.js

Three steps, and every process that uses the same path takes its turn.

  1. Install it with npm i lockbadger. There is nothing else to set up.
  2. Wrap the code that reads and writes the shared file in withLock(path, fn).
  3. Run as many processes as you like. If one crashes, the next one takes over.
LISTING Aexample.js
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

API

lock(path, options?) => Promise<release>
Waits until the lock is free, then resolves to release(). Rejects with ELOCKED when retries run out, or with signal.reason when aborted.
tryLock(path, options?) => Promise<release | null>
One attempt. Resolves to release() or to null when another process holds the lock.
isLocked(path, options?) => Promise<boolean>
True when a live, not stale, lock exists.
withLock(path, fn, options?) => Promise<T>
Runs fn while holding the lock and always releases. fn receives an AbortSignal that aborts if the lock is lost.
release() => Promise<void>
Removes the lock if it is still ours. Also has .signal and .lockPath.
LockError
ELOCKED ECOMPROMISED ERELEASED
Options
namedefaultOptions
stale10000ms without refresh before a lock counts as abandoned
updatestale / 2refresh interval
retriesInfinityattempts before ELOCKED (tryLock: always 0)
retryDelay20..500ms between attempts, or a function of the attempt
signalundefinedcancels the wait
onCompromisedwarningcalled if the lock is lost (default: process warning)
lockPathpath.lockwhere the lock directory lives
checkPidtruesame host dead pid detection

Sheet 06

Benchmarks

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

Uncontended lock and release

per second, higher is better

lockbadger682
proper-lockfile 4.1.21113

4 processes x 50 increments

ms wall time, lower is better

lockbadger455 ms
proper-lockfile 4.1.2636 ms

Takeover after SIGKILL, stale 5 s

ms, lower is better

lockbadger5 ms
proper-lockfile 4.1.25228 ms

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

Side by side with proper-lockfile

proper-lockfile 4.1.2 is a solid, widely used package. These are the differences we verified by reading its source and running both.

Featurelockbadger 0.1.1proper-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 lostwarningthrows (uncaught)
Synchronous API
Runtime dependencies03

Size

The ESM entry is the source itself, with JSDoc. No bundler involved.

kB raw
22.5
kB min + gzip
3.69
runtime dependencies
0
Compatibility
RuntimeStatus
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 2CI 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

Questions

Give your files a guard that never sleeps on the job.

One install, four functions, zero dependencies.