unlink(2) is not atomic (kernel/file system bug)

Originator:thomas.munro
Number:rdar://46318934 Date Originated:2018-11-29
Status:Unknown Resolved:
Product:macOS Product Version:10.14
Classification: Reproducible:Yes
 
Summary: 
If you call open(2) and read(2) on a file that is concurrently being unlinked with unlink(2), sometimes you see the file in a strange state.  That doesn't seem to conform to POSIX semantics, and doesn't happen on other operating systems.

Steps to Reproduce:
Please see test program at https://github.com/macdice/unlinktest

Expected Results:
The test program should produce no output, because the file should either be seen as not existing (ENOENT) or existing and containing 11 bytes.

Actual Results:
On macOS it sometimes reports that it succeeded in opening the file but found it to be empty.

Version/Build:
macOS 10.14.1 build 18B75 (also tested on macOS 10.14 build 18A391).  Compiler is "Apple LLVM version 10.0.0 (clang-1000.11.45.5)", target is "x86_64-apple-darwin18.2.0", uname -a is "Darwin XXXXXX 18.2.0 Darwin Kernel Version 18.2.0: Fri Oct  5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64 x86_64".

Configuration:
Seen on a mid 2014 MBP and also (in the more complex example that triggered our investigation) a build farm server in the PostgreSQL build/test farm (not sure which model/configuration, sorry).  Using apfs, not tested on any other filesystem.

Comments

Still happens on 10.14.3.

By thomas.munro at Jan. 23, 2019, 2:22 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!