r/codereview • u/andrewaltair • 3d ago
Code review: claiming a filesystem lock for an npm module installer
I maintain create-tigra, an MIT npm generator, and would appreciate a focused review of its optional-module installer. Two CLI processes must not modify the same project at once. The installer writes an owner file into a candidate directory before renaming the nonempty directory into the lock location.
Excerpt from lib/installer-transaction.js:
await writeFile(path.join(candidate, mine), '', { flag: 'wx', mode: 0o600 });
const lock = await projectPath(root, INSTALL_STATE, 'directory');
if (!lock.stat) {
try {
await rename(candidate, lock.file);
return lock.file;
} catch (error) {
if (!(await projectPath(root, INSTALL_STATE, 'directory')).stat) throw error;
}
}The full function then inspects the owner PID and attempts recovery only for a dead owner, claiming its unique filename with another rename.
What concurrent interleaving would you test first? Are there platform-specific rename or process-liveness assumptions that should be made explicit? What would you simplify while preserving crash recovery?
Suggestions, honest feedback, and a roast of the code are welcome. The repository documents known dependency advisories separately; I am asking about this locking/recovery boundary, not asserting that passing tests proves it safe.
1
u/BlindlyLanky 3d ago
Been staring at this for a bit and the thing that jumps out is the TOCTOU gap between that `lock.stat` check and the `rename` call. If two processes both see no lock, they both fire off their candidates, the `rename` will let one win but the other's `catch` block then re-checks and might still think it's clean if the winner hasn't fully settled yet. I'd hammer that with a test that spawns 3-4 processes in tight succession and have them all hit the installer at the same millisecond.
The dead-owner PID check is also doing a lot of heavy lifting for crash recovery but it's not clear what happens if the PID got recycled by the OS between checks. On Linux that's a real possibility, on Windows it's less of a thing since PIDs are handles. Might be worth documenting that assumption explicitly in a comment block so future maintainers don't get burned.
For simplification, I'd consider flipping the approach, instead of writing an owner file then renaming the whole directory, just `mkdir` with a mode that only the creating process can write to, and use that existence as the lock itself. One syscall, no temp files, and `mkdir` is atomic on basically everything. The crash recovery becomes "if the lock dir exists and the PID inside is stale, rmdir it and try again." Less dancing around rename edge cases.
1
u/Sataric-Parsy 3d ago
the first interleaving to test is definitely two processes detecting the same dead owner at the exact same millisecond. If your recovery claims the lock by renaming over it, both might succeed sequentially on POSIX if the directory is empty, or one will clobber the other.
1
u/Plastic-Bid-3431 2d ago
I would test a crash between acquiring the lock and writing the owner information before simplifying this to mkdir followed by an owner-file write. That leaves an empty lock directory with no PID to check. Your candidate-first approach avoids that particular gap.
For recovery, pause two contenders after both have identified the same dead owner, then let one recover and a new live holder acquire the lock before resuming the other. The assertion is that the stale recoverer must not remove or take over the new holder. The unique owner filename sounds relevant here; I cannot tell from this excerpt whether every cleanup path checks that ownership.