Friday 9 June 2017

What are the atomicity guarantees of `fs.appendFile()`?

The documentation for fs.appendFile() is vague with regard to atomicity guarantees.

I am specifically wondering whether it is supposed to be possible for data to be either interleaved or dropped (e.g. due to writes to overlapping areas) if two or more calls to it are made on the same file, without waiting for the callback in between. For example, take this code:

fs.writeFileSync('blort.bin', '');
for (let i = 0; i < 10; i++) {
  const data = Buffer.alloc(65536, i);
  fs.appendFile('blort.bin', data, () => {});
}

Assuming there are no filesystem errors, would Node make the following guarantees after all of the append operations are done?:

  • The file is exactly 640k in size.
  • The file consists of 10 chunks of data, each uniformly containing a single byte.

I'm also curious if there's any serialization guarantee (though I assume not). That is, are the appends guaranteed to be executed in order?



via danfuzz

No comments:

Post a Comment