This repository was archived by the owner on Jan 23, 2023. It is now read-only.
[release/3.1] Fix StreamReader to pass cancellation token into ReadBufferAsync (#27464) #27501
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Port #27464 to release/3.1
Description
When StreamReader.ReadAsync is called and the reader's buffer is initially empty, it calls a helper method that in turn interacts with the underlying stream. That helper method wasn't defined to take a CancellationToken, so ReadAsync doesn't pass along the one supplied by the caller. That means for this first read on the underlying stream, the supplied CancellationToken is ignored.
Customer Impact
Cancellation ends up being unreliable.
Regression?
No. It's been like this since the overload was added in .NET Core 2.1.
Testing
New tests were added. Previously tests were only validating the case where the token has cancellation requested prior to the invocation, which triggers a dedicated path that handles this correctly.
Risk
Low. It's just passing along a CancellationToken.
cc: @danmosemsft