Skip to content

Avoid re-throwing exception in bad request handling path#65221

Merged
adityamandaleeka merged 1 commit intodotnet:mainfrom
adityamandaleeka:bad_req_avoid_rethrow
Jan 28, 2026
Merged

Avoid re-throwing exception in bad request handling path#65221
adityamandaleeka merged 1 commit intodotnet:mainfrom
adityamandaleeka:bad_req_avoid_rethrow

Conversation

@adityamandaleeka
Copy link
Copy Markdown
Member

@adityamandaleeka adityamandaleeka commented Jan 25, 2026

Avoid re-throwing BadHttpRequestException in TryParseRequest. Instead of propagating the exception through async state machines, handle the bad request state directly at the catch site.

The re-thrown exception was never observable to user code. It was caught internally by ProcessRequestsAsync() and used only to call SetBadRequestState(ex) - the same call we now make directly.

This change preserves the external behavior:

  • OnBadRequest() still called (logging, diagnostics)
  • SetBadRequestState() still called (metrics, state management)
  • TryProduceInvalidRequestResponse() still runs (HTTP 400 response)
  • Connection still closed properly

User middleware and application code cannot catch this exception because it occurs during request line/header parsing, before the request is dispatched to the application pipeline.

In local testing, this shows an ~7-8% improvement in bad request RPS.

Copilot AI review requested due to automatic review settings January 25, 2026 17:07
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Jan 25, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes bad request handling in HTTP/1.1 connections by avoiding unnecessary exception re-throwing. Instead of propagating BadHttpRequestException through async state machines, the exception is caught and the bad request state is handled directly at the catch site.

Changes:

  • Modified exception handling in TryParseRequest to call SetBadRequestState directly instead of re-throwing
  • Set endConnection = true and return true to properly exit the request processing loop
  • Added explanatory comment describing the optimization

@adityamandaleeka
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

Comment on lines +741 to +743
SetBadRequestState(ex);
endConnection = true;
return true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other places in this method that call KestrelBadHttpRequestException.Throw. Should they throw vs handle exception here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a subsequent PR coming that will change the exception paths here more substantially. I got this change out early since it was super small and strictly an improvement.

@adityamandaleeka
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@adityamandaleeka adityamandaleeka merged commit 44c0e73 into dotnet:main Jan 28, 2026
31 of 32 checks passed
@dotnet-policy-service dotnet-policy-service Bot added this to the 11.0-preview1 milestone Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Perf

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants