Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c3fddaa

Browse files
stephentoubdanmoseley
authored andcommitted
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.
1 parent a0a1e56 commit c3fddaa

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/System.Private.CoreLib/shared/System/IO/StreamReader.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ private int ReadBuffer(Span<char> userBuffer, out bool readToUserBuffer)
852852

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

889889
_charPos = tmpCharPos = i + 1;
890890

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

914914
return sb.ToString();
915915
}
@@ -943,7 +943,7 @@ private async Task<string> ReadToEndAsyncInternal()
943943
int tmpCharPos = _charPos;
944944
sb.Append(_charBuffer, tmpCharPos, _charLen - tmpCharPos);
945945
_charPos = _charLen; // We consumed these characters
946-
await ReadBufferAsync().ConfigureAwait(false);
946+
await ReadBufferAsync(CancellationToken.None).ConfigureAwait(false);
947947
} while (_charLen > 0);
948948

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

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

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

10041004
internal override async ValueTask<int> ReadAsyncInternal(Memory<char> buffer, CancellationToken cancellationToken)
10051005
{
1006-
if (_charPos == _charLen && (await ReadBufferAsync().ConfigureAwait(false)) == 0)
1006+
if (_charPos == _charLen && (await ReadBufferAsync(cancellationToken).ConfigureAwait(false)) == 0)
10071007
{
10081008
return 0;
10091009
}
@@ -1233,7 +1233,7 @@ public override ValueTask<int> ReadBlockAsync(Memory<char> buffer, CancellationT
12331233
return new ValueTask<int>(t);
12341234
}
12351235

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

12561256
if (len == 0)
@@ -1272,7 +1272,7 @@ private async ValueTask<int> ReadBufferAsync()
12721272
else
12731273
{
12741274
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?");
1275-
_byteLen = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer)).ConfigureAwait(false);
1275+
_byteLen = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer), cancellationToken).ConfigureAwait(false);
12761276
Debug.Assert(_byteLen >= 0, "Stream.Read returned a negative number! Bug in stream class.");
12771277

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

0 commit comments

Comments
 (0)