Skip to content

Serialize concurrent writes to the same filepath (Fixes #13) - #16

Open
dyk1454683243-sudo wants to merge 2 commits into
jonschlinkert:masterfrom
dyk1454683243-sudo:cursor/serialize-concurrent-writes-9a66
Open

dyk1454683243-sudo wants to merge 2 commits into
jonschlinkert:masterfrom
dyk1454683243-sudo:cursor/serialize-concurrent-writes-9a66

Conversation

@dyk1454683243-sudo

@dyk1454683243-sudo dyk1454683243-sudo commented Sep 20, 2026

Copy link
Copy Markdown

Fixes #13

Two overlapping write() calls to the same path can interleave: a later, smaller write may finish first, then an earlier large write overwrites it (or a reader sees a torn/corrupted file).

Change

In-process promise queue keyed by path.resolve(destpath) so write() calls targeting the same file run serially. Last-queued write wins after serialization. write.sync is unchanged (writeFileSync completes before returning, so two sync writes cannot interleave). Public API, overwrite/increment/mkdir behavior, and dependencies are unchanged.

Tests

  • Concurrent large-then-small writes to the same path: each write finishes completely before the next starts, and the final file matches the last-queued write
  • Absolute vs relative path strings share the same queue
  • A failed write does not stall the next write to that path

Queue write() calls per resolved destination path so overlapping
async writes cannot interleave or tear the file. write.sync still
uses writeFileSync and adds a queue barrier for later write()s.

Fixes jonschlinkert#13

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
@dyk1454683243-sudo
dyk1454683243-sudo marked this pull request as ready for review September 20, 2026 19:51
write.sync already completes atomically via writeFileSync. Appending
a no-op to the async queue on each sync write would grow a microtask
chain in tight loops used by callers such as flat-cache.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
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.

Possible race condition if 2 calls write to the same file

2 participants