Skip to content

Commit a3612a4

Browse files
authored
Do not drain HttpContentReadStream if the connection is disposed (#57287)
Fixes #56159.
1 parent e81efc8 commit a3612a4

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ChunkedEncodingReadStream.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ private enum ParsingState : byte
425425
Done
426426
}
427427

428-
public override bool NeedsDrain => (_connection != null);
428+
public override bool NeedsDrain => CanReadFromConnection;
429429

430430
public override async ValueTask<bool> DrainAsync(int maxDrainBytes)
431431
{

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthReadStream.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private ReadOnlyMemory<byte> ReadFromConnectionBuffer(int maxBytesToRead)
190190
return connectionBuffer.Slice(0, bytesToConsume);
191191
}
192192

193-
public override bool NeedsDrain => (_connection != null);
193+
public override bool NeedsDrain => CanReadFromConnection;
194194

195195
public override async ValueTask<bool> DrainAsync(int maxDrainBytes)
196196
{

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpContentReadStream.cs

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public HttpContentReadStream(HttpConnection connection) : base(connection)
2828

2929
protected bool IsDisposed => _disposed == 1;
3030

31+
protected bool CanReadFromConnection
32+
{
33+
get
34+
{
35+
// _connection == null typically means that we have finished reading the response.
36+
// Cancellation may lead to a state where a disposed _connection is not null.
37+
HttpConnection? connection = _connection;
38+
return connection != null && connection._disposed != Status_Disposed;
39+
}
40+
}
41+
3142
public virtual ValueTask<bool> DrainAsync(int maxDrainBytes)
3243
{
3344
Debug.Fail($"DrainAsync should not be called for this response stream: {GetType()}");

0 commit comments

Comments
 (0)