-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Breaking change] FileStream.Position is updated AFTER ReadAsync|WriteAsync completes #50858
Comments
Tagging subscribers to this area: @carlossanlop Issue Details
In order to allow for 100% asynchronous file IO with
#48813 has introduced locking in the This means, that byte[] bytes = new byte[10_000];
string path = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize: 4096, useAsync: true))
{
Task[] writes = new Task[3];
writes[0] = fs.WriteAsync(bytes, 0, bytes.Length);
Console.WriteLine(fs.Position);
writes[1] = fs.WriteAsync(bytes, 0, bytes.Length);
Console.WriteLine(fs.Position);
writes[2] = fs.WriteAsync(bytes, 0, bytes.Length);
Console.WriteLine(fs.Position);
await Task.WhenAll(writes);
Console.WriteLine(fs.Position);
} Pre-change behavior:
Post-change behavior:
To enable the .NET 5 behaviour, users can specify an {
"configProperties": {
"System.IO.UseNet5CompatFileStream": true
}
}
set DOTNET_SYSTEM_IO_USENET5COMPATFILESTREAM=1 @dotnet/compat @stephentoub @jozkee @carlossanlop @jeffhandley
|
Aside; if there is a breaking change on
|
This was the 2nd breaking change (out of 2 so far) that we have introduced and I've filled a separate issue for it: #50860 |
What is the behavior of synchronously issuing multiple The issue description sounds like the caller will have to maintain its own position if he does any seeking between interleaved calls, is that right? And that completing calls will corrupt the position (leave them in an arbitrary state depending on the ordering of user calls vs. async calls completing) - even in a single threaded application? I would have expected |
@adamsitnik since #27643 and #16341 have been fixed, is there anything else we should address before closing this issue? Since this is a breaking change that needs to be documented, I opened an issue in dotnet/docs: dotnet/docs#23858 |
Closing (dotnet/docs#24060) |
Any word on my question/concerns above? |
@weltkante I am not sure if I understand, could you provide a code sample? |
Ah ok I thought this was something considered in the design because the issue title explicitly spells it out, so I was asking for clarification on that design. If The original design of updating If that wasn't considered yet I'll try to write some code to show what I mean. I assume I should start that investigation in a separate issue (if I find the behavior is as broken as it sounds)? |
Never mind, I've read through the linked issues and realize that having multiple outstanding requests has been moved into This is NOT updated in the documentation of FileStream.Read/WriteAsync, only as breaking change? Also without guiding to the new |
FileStream
has never been thread-safe, but until .NET 6 we have tried to somehow support multiple concurrent calls to its async methods (ReadAsync
&WriteAsync
).We were doing that only on Windows and according to my knowledge, this was not documented anywhere.
In order to allow for 100% asynchronous file IO with
FileStream
and fix the following issues:#48813 has introduced locking in the
FileStream
buffering logic. Now when buffering is enabled (bufferSize
passed toFileStream
ctor is> 1
) everyReadAsync
&WriteAsync
operation is serialized.This means, that
FileStream.Position
is updated AFTER ReadAsync|WriteAsync completes. Not after the operation is started (the old, windows-specific behavior).Pre-change behavior:
Post-change behavior:
To enable the .NET 5 behavior, users can specify an
AppContext
switch or an environment variable:@dotnet/compat @stephentoub @jozkee @carlossanlop @jeffhandley
The text was updated successfully, but these errors were encountered: