Repository navigation
Description
Description
When calling Flush(flushToDisk: true) on a FileStream that was created on an SMB network share after writing data and before Dispose, the files written to the network share are corrupt (truncated) with a high chance.
That issue was reported by customers and it was very hard to find, but in the end very easily reproducable.
Reproduction Steps
Use macOS, connect to a network share with Finder (in my case on a Synology DS224+), and then run the following code.
using System.Globalization;
using System.Security.Cryptography;
namespace flush_to_disk;
class Program
{
static void Main(string[] args)
{
var basePath = "/Volumes/abc";
var data = new byte[64 * 1024 * 1024]; // 64 MB
RandomNumberGenerator.Fill(data);
for (var i = 0; i < 16; i++)
{
var path = Path.Combine(basePath, i.ToString(CultureInfo.InvariantCulture));
using (var fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
fs.Write(data);
fs.Flush(flushToDisk: true);
}
}
}
}Expected behavior
16 files with a size of exactly 64 MB each are created.
Actual behavior
16 files with sizes all over the place are created.
drwx------ 1 stefan staff 16384 Feb 22 12:09 ./
drwxr-xr-x 4 root wheel 128 Feb 22 11:46 ../
-rwx------@ 1 stefan staff 8196 Feb 22 12:08 .DS_Store*
-rwx------ 1 stefan staff 67108864 Feb 22 12:08 0*
-rwx------ 1 stefan staff 67108864 Feb 22 12:08 1*
-rwx------ 1 stefan staff 58720256 Feb 22 12:09 10*
-rwx------ 1 stefan staff 58982400 Feb 22 12:09 11*
-rwx------ 1 stefan staff 59244544 Feb 22 12:09 12*
-rwx------ 1 stefan staff 58720256 Feb 22 12:09 13*
-rwx------ 1 stefan staff 58720256 Feb 22 12:09 14*
-rwx------ 1 stefan staff 59506688 Feb 22 12:09 15*
-rwx------ 1 stefan staff 60817408 Feb 22 12:08 2*
-rwx------ 1 stefan staff 60817408 Feb 22 12:08 3*
-rwx------ 1 stefan staff 60817408 Feb 22 12:08 4*
-rwx------ 1 stefan staff 58720256 Feb 22 12:08 5*
-rwx------ 1 stefan staff 60817408 Feb 22 12:09 6*
-rwx------ 1 stefan staff 60817408 Feb 22 12:09 7*
-rwx------ 1 stefan staff 59244544 Feb 22 12:09 8*
-rwx------ 1 stefan staff 58720256 Feb 22 12:09 9*
Regression?
I don't know.
Known Workarounds
Remove the line fs.Flush(flushToDisk: true); Then files with exactly 64 MB each are created.
Configuration
- .NET 10.0.3 (arm64)
- macOS Tahoe 26.3
- Synology DS224+
Other information
At first I thought that it could be a problem with macOS' SMB client, but then I found that the F_FULLFSYNC that .NET uses is actually used quite commonly by database engines - which are typically not written in .NET - and I suspect that anyone would have found this easily reproducable bug if it really was in macOS itself. So now I suspect that the error could actually be in .NET.
I was not able to reproduce this on Windows, but on Windows .NET's flush-to-disk implementation is different because F_FULLFSYNC doesn't even exist there.
Activity
[-]FileStream.Flush(flushToDisk: true) writes corrupt (truncated) files on macOS + SMB share[/-][+]FileStream.Flush(flushToDisk: true) causes corrupt (truncated) files to be written on macOS + SMB share[/+]sschultze commentedon Feb 22, 2026
I just let Gemini port this to version to Rust (with non-random data though), including the call to
libc::fcntl(fd, libc::F_FULLFSYNC)that .NET does.Finding: The Rust version did not produce the error. All files exactly 64 MB. From my point of view, this is a strong indication that the error is indeed in .NET.
Please note that I am not a Rust programmer, but the conversion looks pretty plausible to me.
For reference:
stephentoub commentedon Feb 22, 2026
Hopefully if you also have it port the bug being fixed in #124725 you're also able to repro again?
sschultze commentedon Feb 22, 2026
@stephentoub If, in "my" Rust code,
libc::fcntl(fd, libc::F_FULLFSYNC)would have failed, then I would have gotten an error. But I didn't, so I don't think that porting the code fixed in #124725 would make any difference.Also, why would an effectively ignored
F_FULLFSYNCfailure in the .NET code lead to truncated / corrupt files? From my naive point of view, the only bad thing that can happen is that there's no "full f-sync".But I see that there are error codes like
EAGAINorEBUSY. Perhaps differences between .NET's internals and the Rust version cause error codes like these, and some internal state corrupts, so that the close command is sent with unfinished data? Just guessing, I'm unfortunately not deep enough into the .NET internals.stephentoub commentedon Feb 22, 2026
Because the sync is what ensures data is actually flushed to disk. If the syscall returned an errno like AGAIN / EINTR suggesting a retry is needed, which is much more common with remote stores, it won't have been retried, so the system would think it successfully sync'd even though it didn't, and you end up with corruption (I assume by corruption you mean missing data).
sschultze commentedon Feb 22, 2026
@stephentoub Thank you. I totally understand that when there's a ignored
F_FULLFSYNCfailure, the data might still not actually have been flushed to disk. But then you end up with the same situation as whenFlush(flushToDisk: true)wasn't called at all - the data will be actually flushed to disk automatically after a few milliseconds. But, as I pointed out, in the latter case (no flush-to-disk) all files have the correct identical sizes, where with flush-to-disk, they don't.Is there probably a bug in the macOS SMB client that goes like this:
F_FULLFSYNCEAGAINand sets some flagAnother workaround
I just tried to add
Thread.Sleep(5000)before theFlush(flushToDisk: true)and ran my reproduction code again. This also "fixes" the problem, most likely because 5s is enough time to perform all the pending writes before theF_FULLFSYNC.sschultze commentedon Feb 22, 2026
I just checked out v10.0.3 and changed
SystemNative_FSyncto this (from the GitHub pull request #124725, but with an additionalfprintf):Then compiled with
./build.sh -subset libs.native -c Releaseand "integrated" the result withResult: The
DEBUG: Custom pal_io logic hit!string is written to the console 16x, so the code change is used.But unfortunately, the file sizes on the SMB share are still not correct. So the code change doesn't help in my case.
sschultze commentedon Feb 22, 2026
Additional experiment 1: No-op
Changed the function
SystemNative_FSyncto just do afprintfand return 0. Result: File sizes are all correct.Additional experiment 2: Only fsync
Changed it to not try
F_FULLFSYNC, but onlyfsync.Result: Same. File sizes are all correct.
PS: Never thought it would be that easy to compile my custom version. Really really cool!
tmds commentedon Feb 23, 2026
I think the rust reproducer (#124722 (comment)) was meant to behave like current .NET, but it didn't actually fail?
We've had issues with samba and how .NET uses file locks to implement
FileShareon non-Windows. Doesexport DOTNET_SYSTEM_IO_DISABLEFILELOCKING=1make a difference?sschultze commentedon Feb 23, 2026
@tmds Yes, you're right, the rust reproducer was meant to behave like .NET 10.0.3, but it didn't fail.
Just tested
DOTNET_SYSTEM_IO_DISABLEFILELOCKING=1 dotnet run. With this configuration, the resulting file sizes are indeed correct.Do you have any details on this issue with Samba? Can I expect
FileShare.Noneto work with Samba when I don't do theFlush(flushToDisk: true)?2 remaining items
sschultze commentedon Feb 24, 2026
@tmds @stephentoub @adamsitnik Here the final update on my investigation. I converted my code to direct libc calls in order to have better control (see below).
Findings
F_FULLFSYNCto a standardfsync"solves" the problem. So the problem is really theF_FULLFSYNC.LOCK_EXtoLOCK_SHhas no positive effect. Wrong file sizes too. So the lock type doesn't matter - both create a problem together withF_FULLFSYNC.Summary
The combination of
flockwith eitherLOCK_EXorLOCK_SH(combined withLOCK_NBlike in .NET)F_FULLFSYNChas a high chance of creating truncated files.
Code
tmds commentedon Feb 24, 2026
@sschultze thanks for investigating and figuring out what exactly triggers the problem!
It seems the F_FULLFSYNC causes pending writes to get discarded.
So a fix for .NET can be to not use F_FULLFSYNC when the file descriptor is for one of network file systems that we are already treating special in CanLockTheFile.
sschultze commentedon Feb 24, 2026
@tmds Thank you for taking care of that!
This seems like a plausible solution to the problem to me.
For my specific app I will disable file locking entirely. Since it isn't required for my use case, the potential for conflict seems to outweigh the benefits.
tmds commentedon Feb 24, 2026
Since your NAS runs Linux, the locking on the system is "advisory" which means it is only respected by those who care to check. So even when .NET would do the locking, non-.NET applications will very likely not be aware.
sschultze commentedon Feb 24, 2026
Yes, this is also what I found. In only brings some value in quite specific use cases.
adamsitnik commentedon Jul 15, 2026
I agree. Overall it seems to be macOS/SMB bug. But since it can have very bad consequences for our users, I think it's reasonable to detect macOS+SMB and just use
fsyncrather thanF_FULLFSYNC.Fix F_FULLFSYNC on network file systems on macOS
adamsitnik commentedon Jul 24, 2026
We have prepared the workaround fix (#130779), but was unable to reproduce the issue (#130779 (comment)).
Closing as won't fix. I think it would be best to provide a small C repro and report the bug in Samba directly. Thanks!