Skip to content

fix: replace steno with internal atomic writer to fix Windows ENOENT race condition - #619

Open
ianshshakya wants to merge 1 commit into
typicode:mainfrom
ianshshakya:main
Open

fix: replace steno with internal atomic writer to fix Windows ENOENT race condition#619
ianshshakya wants to merge 1 commit into
typicode:mainfrom
ianshshakya:main

Conversation

@ianshshakya

Copy link
Copy Markdown

Resolves #610

What does this PR do?

This PR drops the steno dependency and inlines the asynchronous atomic writer directly into TextFile.ts, updating it to use unique temporary filenames instead of a static one.

Why is this necessary?

Currently, when lowdb is used in a multi-process environment (most notably, parallel Jest tests), a severe race condition occurs on Windows resulting in ENOENT crashes.
Because steno and the old TextFileSync statically generated temporary filenames (e.g. .<filename>.tmp), parallel worker processes end up overwriting each other's .tmp files. If Worker A successfully calls rename() before Worker B is finished, Worker B's subsequent rename() will crash with:
ENOENT: no such file or directory, rename '.test-db.json.tmp' -> 'test-db.json'.

The Fix

By natively implementing the atomic writer and appending a random 8-character hex string to the temporary filenames (e.g. .<filename>.<randomStr>.tmp), we guarantee that concurrent parallel processes do not step on each other's toes. The final rename remains atomic, and the ENOENT crash is completely eliminated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENOENT: no such file or directory, rename '.test-db.json.tmp' -> 'test-db.json' v7.0.1

1 participant