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

Fix the buffering bug that blocks SDK #51151

Merged
merged 2 commits into from
Apr 13, 2021

Conversation

adamsitnik
Copy link
Member

@adamsitnik adamsitnik commented Apr 12, 2021

Fixes #51141

I was able to identify the issue (after figuring out which process out of many should be debugged in the SDK repo):

obraz

Write a failing test:

obraz

And provide a fix;

obraz

Some details: it's possible that _writePos == _bufferSize and in such case the next call to any of the Write methods is supposed to flush the write buffer.

@ghost
Copy link

ghost commented Apr 12, 2021

Tagging subscribers to this area: @carlossanlop
See info in area-owners.md if you want to be subscribed.

Issue Details

I was able to identify the issue (after figuring out which process out of many should be debugged in the SDK repo):

obraz

Write a failing test:

obraz

And provide a fix;

obraz

Some details: it's possible that _writePos == _bufferSize and in such case the next call to any of the Write methods is supposed to flush the write buffer.

Author: adamsitnik
Assignees: -
Labels:

area-System.IO

Milestone: 6.0.0

stream.WriteByte(0);
writtenBytes.Add(0);

byte[] bytes = new byte[BufferSize - 1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth putting something other than zeros in here? So it's distinct from the first byte you wrote

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really matter. What matters is that we are attempting to write an 11th character on the stream when the buffer can only hold 10 characters.

Copy link
Member

@jozkee jozkee Apr 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and the problem was that the internal buffer is not being reset/flushed when WriteByte is called when this buffer is already full.

Copy link
Member

@jozkee jozkee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adamsitnik!

@@ -602,7 +602,7 @@ private void WriteByteSlow(byte value)
ClearReadBufferBeforeWrite();
EnsureBufferAllocated();
}
else if (_writePos == _bufferSize - 1)
else
{
FlushWrite();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this method should only be called when either _writePos == 0 or _writePos == _bufferSize, right?
Consider adding the assertion, but not in this PR since we want to merge it ASAP.

Suggested change
FlushWrite();
Debug.Assert(_writePos == _bufferSize);
FlushWrite();

@carlossanlop carlossanlop merged commit 4f3fd1a into dotnet:main Apr 13, 2021
@carlossanlop carlossanlop deleted the anotherBufferingBug branch April 13, 2021 06:56
@ghost ghost locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix test failures found in dotnet/sdk when opting-into the new FileStream strategy mode
5 participants