dotnet / dotnet/aspnetcore

ConnectionAbortedException ---> Reading is already in progress

Open
#52,324 31 comments 0 reactions 0 assignees View on GitHub
area-networking feature-kestrel
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

Describe the bug
We're frequently getting the exception below in .net 8.0.

Microsoft.AspNetCore.Connections.ConnectionAbortedException: The connection was aborted by the application.
---> System.InvalidOperationException: Reading is already in progress.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.OnConsumeAsync()
--- End of inner exception stack trace ---

Further technical details
ASP.NET Core 8.0
Kestrel on Windows Server 2019

![image](https://github.com/dotnet/aspnetcore/assets/4941173/ef40c1a5-32d0-465e-a782-3e6414d8ad68)

https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Core/src/Internal/Http/Http1MessageBody.cs

protected override Task OnConsumeAsync()
{
try
{
while (TryReadInternal(out var readResult))
{
AdvanceTo(readResult.Buffer.End);

if (readResult.IsCompleted)
{
return Task.CompletedTask;
}
}
}
catch (BadHttpRequestException ex)
{
// At this point, the response has already been written, so this won't result in a 4XX response;
// however, we still need to stop the request processing loop and log.
_context.SetBadRequestState(ex);
return Task.CompletedTask;
}
catch (InvalidOperationException ex)
{
var connectionAbortedException = new ConnectionAbortedException(CoreStrings.ConnectionAbortedByApplication, ex);
_context.ReportApplicationError(connectionAbortedException);

// Have to abort the connection because we can't finish draining the request
_context.StopProcessingNextRequest();
return Task.CompletedTask;
}

return OnConsumeAsyncAwaited();
}

One suggestion should be to implement TryMapGet to be called instead of app.MapGet on the server side.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.