runtime: Call to "File.AppendAllText()" function may not always work and may sometimes corrupt the file.

Description

On a Raspberry Pi running Linux, calls to File.AppendAllText() not always append the text and sometime may corrupt the file. This would happen if the Raspberry Pi is rebooted or turned off and on.

Reproduction Steps

  • Create a .NET Core Console app that calls the File.AppendAllText() function. Only append a small amount of text (like a single word).
  • Run the Console app on a Linux machine (in my case is a Raspberry Pi running Linux).
  • Wait until the Console app completes executing.
  • Reboot or turn the computer on / off.

Again, in case it matters this is happening on a Raspberry Pi running Linux, I don not have access to any other Linux computers at this time.

Expected behavior

I would expect the File.AppendAllText() call to do whatever it needs to do to successfully append the text to the file.

Actual behavior

After the computer finishes rebooting, you may see the file with the appended text (that is good) or you may see the file empty or corrupter (that is bad). On my tests, most times, the file will be empty.

Regression?

No response

Known Workarounds

No response

Configuration

.NET Core 3.1

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 29 (14 by maintainers)

Most upvoted comments

If calling fsync was always a good thing, the OS would do it automatically when code closes the file. It does not do that, since defeating the disk cache can reduce IO performance.

Or at the very least, an overload should be provided that accepts a parameter forcing data to be flushed to disk via sync/syncfs.

As far as I know, there’s nothing special about AppendAllText() in this regard, so it would be an overload on every IO API, and potentially every API that indirectly does IO as well. As @tmds says, you probably should look beyond .NET for a solution here, or possibly PInvoke to fsync.

Thanks for the issue, I think this is answered so I’ll close now.

This is happening with over 10 Raspberry Pis, purchased at different times from different vendors and different board revisions. This is a problem with all Raspberry Pis, not just a single device.

That may be. But the symptoms you describe sound very much outside the control of .NET. That’s why I’m asking you to try other languages / frameworks.