Avoid re-throwing exception in bad request handling path#65221
Merged
adityamandaleeka merged 1 commit intodotnet:mainfrom Jan 28, 2026
Merged
Avoid re-throwing exception in bad request handling path#65221adityamandaleeka merged 1 commit intodotnet:mainfrom
adityamandaleeka merged 1 commit intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
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
TryParseRequestto callSetBadRequestStatedirectly instead of re-throwing - Set
endConnection = trueand returntrueto properly exit the request processing loop - Added explanatory comment describing the optimization
Member
Author
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
JamesNK
reviewed
Jan 27, 2026
Comment on lines
+741
to
+743
| SetBadRequestState(ex); | ||
| endConnection = true; | ||
| return true; |
Member
There was a problem hiding this comment.
There are other places in this method that call KestrelBadHttpRequestException.Throw. Should they throw vs handle exception here?
Member
Author
There was a problem hiding this comment.
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.
JamesNK
approved these changes
Jan 27, 2026
BrennanConroy
approved these changes
Jan 27, 2026
Member
Author
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
DeagleGross
approved these changes
Jan 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.