From c3fddaa79b1ae1de2656327179fdc863ece4a5af Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 31 Oct 2019 13:06:30 -0400 Subject: [PATCH] Fix StreamReader to pass cancellation token into ReadBufferAsync (#27464) (#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. --- .../shared/System/IO/StreamReader.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs b/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs index e6196f4cee6d..9da9ec1e8d2c 100644 --- a/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs +++ b/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs @@ -852,7 +852,7 @@ private int ReadBuffer(Span userBuffer, out bool readToUserBuffer) private async Task ReadLineAsyncInternal() { - if (_charPos == _charLen && (await ReadBufferAsync().ConfigureAwait(false)) == 0) + if (_charPos == _charLen && (await ReadBufferAsync(CancellationToken.None).ConfigureAwait(false)) == 0) { return null; } @@ -888,7 +888,7 @@ private int ReadBuffer(Span 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') @@ -909,7 +909,7 @@ private int ReadBuffer(Span 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(); } @@ -943,7 +943,7 @@ private async Task 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(); @@ -976,7 +976,7 @@ public override Task ReadAsync(char[] buffer, int index, int count) ThrowIfDisposed(); CheckAsyncTaskInProgress(); - Task task = ReadAsyncInternal(new Memory(buffer, index, count), default).AsTask(); + Task task = ReadAsyncInternal(new Memory(buffer, index, count), CancellationToken.None).AsTask(); _asyncReadTask = task; return task; @@ -1003,7 +1003,7 @@ public override ValueTask ReadAsync(Memory buffer, CancellationToken internal override async ValueTask ReadAsyncInternal(Memory buffer, CancellationToken cancellationToken) { - if (_charPos == _charLen && (await ReadBufferAsync().ConfigureAwait(false)) == 0) + if (_charPos == _charLen && (await ReadBufferAsync(cancellationToken).ConfigureAwait(false)) == 0) { return 0; } @@ -1233,7 +1233,7 @@ public override ValueTask ReadBlockAsync(Memory buffer, CancellationT return new ValueTask(t); } - private async ValueTask ReadBufferAsync() + private async ValueTask ReadBufferAsync(CancellationToken cancellationToken) { _charLen = 0; _charPos = 0; @@ -1250,7 +1250,7 @@ private async ValueTask 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(tmpByteBuffer, tmpBytePos, tmpByteBuffer.Length - tmpBytePos)).ConfigureAwait(false); + int len = await tmpStream.ReadAsync(new Memory(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) @@ -1272,7 +1272,7 @@ private async ValueTask 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(tmpByteBuffer)).ConfigureAwait(false); + _byteLen = await tmpStream.ReadAsync(new Memory(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