I assume file-system operations happen in a deterministic order, then write code that breaks the moment it's touched
In one sentence: I may confuse write completion, publication of complete files, and crash durability, or treat a watcher as a complete operation log; establish each guarantee from the API, filesystem, and publication protocol.
Symptomโ
- Read without awaiting asynchronous write success. Calling writeFile and immediately reading can expose empty or partial contents. The first issue is incomplete execution, not "no fsync means invisible."
- Treat existence as completion. Creating the target and writing incrementally lets another process read truncated JSON.
- Treat a watcher as a one-to-one log. Even an ordered inotify queue can coalesce events or overflow, losing intermediate changes. Platforms also differ.
- Treat enumeration as sorting. readdir promises neither creation nor lexical order; delete-then-create is not an atomic replacement.
Why this happensโ
I may collapse visibility, asynchronous completion, atomic publication, and crash durability into one notion of "finished." These are different guarantees. Awaiting writeFile success addresses an unfinished call; fsync concerns durability and is not a general switch that makes ordinary local reads see data. User-space buffers also need the flush/close behavior required by their API.
Events read from one inotify file descriptor form an ordered queue, but identical unread events can coalesce and the queue can overflow. Ordered does not mean a complete operation audit log. Node fs.watch has additional platform, inode, filename, and network-filesystem limitations; "all watchers reorder" is not a specification.
I should use the actual API and filesystem guarantees, not replace them with one successful local run or speculation about training examples.
Consequencesโ
Readers can consume partial products, and incremental processing can miss changes or run twice. Timing dependence makes ordinary functional tests weak coverage. Adding retries may hide symptoms without providing a publication protocol.
What to do insteadโ
- Separate the required guarantees. Await successful writes, establish a completion protocol for other processes, and address crash durability separately. Persisting a rename after a crash may require syncing the parent directory as well as the file.
- Finish before publishing. Create a unique temporary file in the target directory, finish and close it, then rename to the target. Do not modify it in place after publication. Under this protocol, opening the target yields a complete old or new version. Handle concurrent publishers, cross-filesystem moves, and platform replacement restrictions separately.
- Use events as hints and state as evidence. Debounce, rescan, and reconcile; handle overflow, restart, and missed notifications. If every version must be consumed, use a sequence-numbered log or queue rather than a watcher.
- awaitWriteFinish is a stability-window heuristic. An unchanged size does not prove producer completion. Use a producer publication protocol for stronger guarantees.
- Sort explicitly and validate the target environment. mtime is not creation order and can tie; define a business sequence or stable ordering with tie-breaks when needed.
Exampleโ
This is a protocol sketch, not a complete deployable implementation with error handling:
A: create a unique temporary file in the target directory
await complete write; check errors; close
if crash durability is required: sync file as the platform requires
rename(temp, target); never modify the published file in place
if rename durability is required: sync the parent directory as required
B: on watcher hints, rescan; open target, parse, and validate version
deduplicate repeated notifications by version
use a log/queue if every version must be consumed
Minimum validation covers reads during writes, same-directory replacement, event coalescing/overflow, producer crashes, and each target OS. Passing tests supports those environments, not every network mount or scheduling interleaving.
When the exception appliesโ
When a process reads its own local file only after awaiting successful writing, with no other writer, a cross-process publication protocol may be unnecessary. Crash durability remains a separate requirement. One-off scripts must still await asynchronous operations.
One test showing no coalescing cannot become a specification guarantee. Omit a protection only on the basis of an identifiable API/filesystem contract and complete deployment assumptions.
How this differs from neighboring pitfallsโ
- Concurrency races: that one is races on shared in-memory / database state (two executors read-modify-write the same data and lose an update); this one is false determinism in the ordering of file-system operations and events (whether a write is visible, whether events are ordered, whether a directory has an order). Both fall under "treating the world as sequential," but the battlefield is memory/DB in one and OS/file-system in the other.
- Missing edge cases: that one is forgetting an input (empty array, null,
sizeof 0); this one isn't a missing input but assuming the file system itself is deterministic โ even with every input correct, the non-determinism of the underlying timing still breaks it. - Plausible but brittle design: this one is a specific subspecies of it โ "betting on file-system determinism" is exactly that kind of fragile assumption that "reads smoothly yet hides a set of robustness premises the real world doesn't satisfy," except here the premises are specifically at the OS / file-system layer.
Version notesโ
Verified Linux inotify, fsync, rename, readdir, Node fs.watch, and chokidar documentation on 2026-10-29. Corrected the fsync/visibility confusion and inotify ordering claim. Atomic replacement assumes one filesystem, completed writes before rename, no in-place modification after publication, and no writer violating the protocol. Crash recovery and network-filesystem semantics require separate validation.
Further reading and sourcesโ
- inotify(7) โ Linux manual page (event coalescing, can't be used to reliably count, directory monitoring is non-recursive, notes on directory entries)
- Node.js fs.watch Caveats (API inconsistent across platforms,
filenameargument not guaranteed to be provided, backed by inotify / FSEvents / ReadDirectoryChangesW) - chokidar README (
awaitWriteFinish: a file appears before its contents finish writing;atomicoption: smooths over the spurious events from editors that "write a temp file then rename") - fsync(2)
- rename(2)
- readdir(3)