From 5d0be1504e1dd5e2b8064b6a87cdf1023c7d4afa Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 27 Oct 2019 16:48:18 -0400 Subject: [PATCH] Fix StreamReader to pass cancellation token into ReadBufferAsync (#27464) 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 a126840361e3..5ce2d117a99b 100644 --- a/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs +++ b/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs @@ -840,7 +840,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; } @@ -876,7 +876,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') @@ -894,7 +894,7 @@ private int ReadBuffer(Span userBuffer, out bool readToUserBuffer) i = tmpCharLen - tmpCharPos; 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(); } @@ -928,7 +928,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(); @@ -961,7 +961,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; @@ -988,7 +988,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; } @@ -1218,7 +1218,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; @@ -1235,7 +1235,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) @@ -1257,7 +1257,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