D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21627 - macOS: std.stdio.File.sync does not guarantee to be written to disk
Summary: macOS: std.stdio.File.sync does not guarantee to be written to disk
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Mac OS X
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-02-10 14:41 UTC by Hiroki Noda
Modified: 2021-03-01 14:31 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Hiroki Noda 2021-02-10 14:41:52 UTC
The documentation(https://dlang.org/phobos/std_stdio.html#.File.sync) says:

> Forces any data buffered by the OS to be written to disk.
> Call flush before calling this function to flush the C FILE buffers first.

Accoriding to macOS's manapge, however, fsync(2) does not guarantee to be written to disk.
https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html

> Note that while fsync() will flush all data from the host to the drive
> (i.e. the "permanent storage device"), the drive itself may not physically
> write the data to the platters for quite some time and it may be
> written in an out-of-order sequence.
>
> Specifically, if the drive loses power or the OS crashes, the application
> may find that only some or none of their data was written.  The disk
> drive may also re-order the data so that later writes may be present,
> while earlier writes are not.
>
> This is not a theoretical edge case.  This scenario is easily reproduced
> with real world workloads and drive power failures.

The manpage also says that the F_FULLFSYNC fcntl would be useful in this case.

> For applications that require tighter guarantees about the integrity of
> their data, Mac OS X provides the F_FULLFSYNC fcntl.  The F_FULLFSYNC
> fcntl asks the drive to flush all buffered data to permanent storage.

I think std.stdio.File.sync should use F_FULLSYNC fcntl.
Comment 1 Dlang Bot 2021-02-12 10:25:20 UTC
@kubo39 created dlang/phobos pull request #7789 "Fix Issue 21627 - macOS: std.stdio.File.sync does not guarantee to be…" fixing this issue:

- Fix Issue 21627 - macOS: std.stdio.File.sync does not guarantee to be written to disk

https://github.com/dlang/phobos/pull/7789
Comment 2 Dlang Bot 2021-03-01 14:31:37 UTC
dlang/phobos pull request #7789 "Fix Issue 21627 - macOS: std.stdio.File.sync does not guarantee to be…" was merged into master:

- f0f199066e1a7ce2451edf5813f2bf0b9ed5dc40 by Hiroki Noda:
  Fix Issue 21627 - macOS: std.stdio.File.sync does not guarantee to be written to disk

https://github.com/dlang/phobos/pull/7789