Skip to content
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

[FileStream] Flush(flushToDisk: true) should always flush to disk #77384

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/libraries/System.IO.FileSystem/tests/FileStream/Flush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ public void FlushThrowsForDisposedStream(bool? flushToDisk)
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.Dispose();

Assert.Throws<ObjectDisposedException>(() => Flush(fs, flushToDisk));
}
}

[Theory]
[InlineData(null)]
[InlineData(false)]
[InlineData(true)]
public void FlushThrowsForDisposedHandle_EmptyBuffer(bool? flushToDisk)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.SafeFileHandle.Dispose();

Assert.Throws<ObjectDisposedException>(() => Flush(fs, flushToDisk));
}
}

[Theory]
[InlineData(null)]
[InlineData(false)]
[InlineData(true)]
public void FlushThrowsForDisposedHandle_NonEmptyBuffer(bool? flushToDisk)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.WriteByte(1); // fills the internal buffer

fs.SafeFileHandle.Dispose();

Assert.Throws<ObjectDisposedException>(() => Flush(fs, flushToDisk));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,42 +761,29 @@ public override void SetLength(long value)

internal override void Flush(bool flushToDisk)
{
EnsureNotClosed();
Debug.Assert(!_strategy.IsClosed, "FileStream responsibility");
Debug.Assert((_readPos == 0 && _readLen == 0 && _writePos >= 0) || (_writePos == 0 && _readPos <= _readLen),
"We're either reading or writing, but not both.");

// Has write data in the buffer:
if (_writePos > 0)
{
// EnsureNotClosed does not guarantee that the Stream has not been closed
// an example could be a call to fileStream.SafeFileHandle.Dispose()
// so to avoid getting exception here, we just ensure that we can Write before doing it
if (_strategy.CanWrite)
{
FlushWrite();
Debug.Assert(_writePos == 0 && _readPos == 0 && _readLen == 0);
return;
}
FlushWrite();
}

// Has read data in the buffer:
if (_readPos < _readLen)
else if (_readLen > 0)
{
// If the underlying strategy is not seekable AND we have something in the read buffer, then FlushRead would throw.
// We can either throw away the buffer resulting in data loss (!) or ignore the Flush.
// (We cannot throw because it would be a breaking change.) We opt into ignoring the Flush in that situation.
// We cannot throw because it would be a breaking change. We opt into ignoring the Flush in that situation.
if (_strategy.CanSeek)
{
FlushRead();
}

// If the Stream was seekable, then we should have called FlushRead which resets _readPos & _readLen.
Debug.Assert(_writePos == 0 && (!_strategy.CanSeek || (_readPos == 0 && _readLen == 0)));
return;
}

// We had no data in the buffer, but we still need to tell the underlying strategy to flush.
// We still need to tell the underlying strategy to flush. It's NOP for !flushToDisk or !CanWrite.
_strategy.Flush(flushToDisk);

_writePos = _readPos = _readLen = 0;
// If the Stream was seekable, then we should have called FlushRead which resets _readPos & _readLen.
Debug.Assert(_writePos == 0 && (!_strategy.CanSeek || (_readPos == 0 && _readLen == 0)));
}

public override Task FlushAsync(CancellationToken cancellationToken)
Expand Down