Skip to content

Commit

Permalink
Fix StreamReader to pass cancellation token into ReadBufferAsync (dot…
Browse files Browse the repository at this point in the history
…net#27464) (dotnet#27501)

ReadAsyncInternal is correctly passing its token into the two call sites where it delegates directly to the underlying stream, but in one place it calls through ReadBufferAsync, which is currently not defined to take a token.  Fix that, and pass the token through.
  • Loading branch information
stephentoub authored and danmoseley committed Oct 31, 2019
1 parent a0a1e56 commit c3fddaa
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/System.Private.CoreLib/shared/System/IO/StreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ private int ReadBuffer(Span<char> userBuffer, out bool readToUserBuffer)

private async Task<string?> ReadLineAsyncInternal()
{
if (_charPos == _charLen && (await ReadBufferAsync().ConfigureAwait(false)) == 0)
if (_charPos == _charLen && (await ReadBufferAsync(CancellationToken.None).ConfigureAwait(false)) == 0)
{
return null;
}
Expand Down Expand Up @@ -888,7 +888,7 @@ private int ReadBuffer(Span<char> userBuffer, out bool readToUserBuffer)

_charPos = tmpCharPos = i + 1;

if (ch == '\r' && (tmpCharPos < tmpCharLen || (await ReadBufferAsync().ConfigureAwait(false)) > 0))
if (ch == '\r' && (tmpCharPos < tmpCharLen || (await ReadBufferAsync(CancellationToken.None).ConfigureAwait(false)) > 0))
{
tmpCharPos = _charPos;
if (_charBuffer[tmpCharPos] == '\n')
Expand All @@ -909,7 +909,7 @@ private int ReadBuffer(Span<char> userBuffer, out bool readToUserBuffer)
sb = new StringBuilder(i + 80);
}
sb.Append(tmpCharBuffer, tmpCharPos, i);
} while (await ReadBufferAsync().ConfigureAwait(false) > 0);
} while (await ReadBufferAsync(CancellationToken.None).ConfigureAwait(false) > 0);

return sb.ToString();
}
Expand Down Expand Up @@ -943,7 +943,7 @@ private async Task<string> ReadToEndAsyncInternal()
int tmpCharPos = _charPos;
sb.Append(_charBuffer, tmpCharPos, _charLen - tmpCharPos);
_charPos = _charLen; // We consumed these characters
await ReadBufferAsync().ConfigureAwait(false);
await ReadBufferAsync(CancellationToken.None).ConfigureAwait(false);
} while (_charLen > 0);

return sb.ToString();
Expand Down Expand Up @@ -976,7 +976,7 @@ public override Task<int> ReadAsync(char[] buffer, int index, int count)
ThrowIfDisposed();
CheckAsyncTaskInProgress();

Task<int> task = ReadAsyncInternal(new Memory<char>(buffer, index, count), default).AsTask();
Task<int> task = ReadAsyncInternal(new Memory<char>(buffer, index, count), CancellationToken.None).AsTask();
_asyncReadTask = task;

return task;
Expand All @@ -1003,7 +1003,7 @@ public override ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken

internal override async ValueTask<int> ReadAsyncInternal(Memory<char> buffer, CancellationToken cancellationToken)
{
if (_charPos == _charLen && (await ReadBufferAsync().ConfigureAwait(false)) == 0)
if (_charPos == _charLen && (await ReadBufferAsync(cancellationToken).ConfigureAwait(false)) == 0)
{
return 0;
}
Expand Down Expand Up @@ -1233,7 +1233,7 @@ public override ValueTask<int> ReadBlockAsync(Memory<char> buffer, CancellationT
return new ValueTask<int>(t);
}

private async ValueTask<int> ReadBufferAsync()
private async ValueTask<int> ReadBufferAsync(CancellationToken cancellationToken)
{
_charLen = 0;
_charPos = 0;
Expand All @@ -1250,7 +1250,7 @@ private async ValueTask<int> ReadBufferAsync()
{
Debug.Assert(_bytePos <= _encoding.Preamble.Length, "possible bug in _compressPreamble. Are two threads using this StreamReader at the same time?");
int tmpBytePos = _bytePos;
int len = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer, tmpBytePos, tmpByteBuffer.Length - tmpBytePos)).ConfigureAwait(false);
int len = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer, tmpBytePos, tmpByteBuffer.Length - tmpBytePos), cancellationToken).ConfigureAwait(false);
Debug.Assert(len >= 0, "Stream.Read returned a negative number! This is a bug in your stream class.");

if (len == 0)
Expand All @@ -1272,7 +1272,7 @@ private async ValueTask<int> ReadBufferAsync()
else
{
Debug.Assert(_bytePos == 0, "_bytePos can be non zero only when we are trying to _checkPreamble. Are two threads using this StreamReader at the same time?");
_byteLen = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer)).ConfigureAwait(false);
_byteLen = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer), cancellationToken).ConfigureAwait(false);
Debug.Assert(_byteLen >= 0, "Stream.Read returned a negative number! Bug in stream class.");

if (_byteLen == 0) // We're at EOF
Expand Down

0 comments on commit c3fddaa

Please sign in to comment.