Skip to content

Commit

Permalink
Do not drain HttpContentReadStream if the connection is disposed (#57287
Browse files Browse the repository at this point in the history
)

Fixes #56159.
  • Loading branch information
antonfirsov authored Aug 16, 2021
1 parent e81efc8 commit a3612a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private enum ParsingState : byte
Done
}

public override bool NeedsDrain => (_connection != null);
public override bool NeedsDrain => CanReadFromConnection;

public override async ValueTask<bool> DrainAsync(int maxDrainBytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private ReadOnlyMemory<byte> ReadFromConnectionBuffer(int maxBytesToRead)
return connectionBuffer.Slice(0, bytesToConsume);
}

public override bool NeedsDrain => (_connection != null);
public override bool NeedsDrain => CanReadFromConnection;

public override async ValueTask<bool> DrainAsync(int maxDrainBytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public HttpContentReadStream(HttpConnection connection) : base(connection)

protected bool IsDisposed => _disposed == 1;

protected bool CanReadFromConnection
{
get
{
// _connection == null typically means that we have finished reading the response.
// Cancellation may lead to a state where a disposed _connection is not null.
HttpConnection? connection = _connection;
return connection != null && connection._disposed != Status_Disposed;
}
}

public virtual ValueTask<bool> DrainAsync(int maxDrainBytes)
{
Debug.Fail($"DrainAsync should not be called for this response stream: {GetType()}");
Expand Down

0 comments on commit a3612a4

Please sign in to comment.