Defensive cleanup in IIS request processing logic#66622
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens IIS in-process request teardown by ensuring critical IO/pipeline completion logic runs even if an exception is thrown during late-stage request/response processing, reducing the risk of native callbacks interacting with already-collected managed state.
Changes:
- Wraps late-stage response completion / stream stopping / end-of-request production in a
try/finallyso the pipe/AsyncIO completion sequence always runs. - Moves the “complete writer/reader + drain write/read tasks + AsyncIO.Complete” sequence into the
finallyblock with additional explanatory comments.
Comment on lines
+101
to
+125
| finally | ||
| { | ||
| // If the request was aborted and no response was sent, we use status code 499 for logging | ||
| StatusCode = ClientDisconnected ? StatusCodes.Status499ClientClosedRequest : 0; | ||
| success = false; | ||
| } | ||
| // Defensive finally in case any of the above code throws an exception | ||
|
|
||
| // Complete response writer and request reader pipe sides | ||
| _bodyOutput.Complete(); | ||
| _bodyInputPipe?.Reader.Complete(); | ||
| // Important cleanup, this ensures native is done with async completions and we can cleanup resources | ||
| // If this somehow didn't run and native was still doing async completions, we could end up AVing | ||
| // when trying to access managed objects that were already collected. | ||
|
|
||
| // Allow writes to drain | ||
| if (_writeBodyTask != null) | ||
| { | ||
| await _writeBodyTask; | ||
| } | ||
| // Complete response writer and request reader pipe sides | ||
| _bodyOutput.Complete(); | ||
| _bodyInputPipe?.Reader.Complete(); | ||
|
|
||
| // Cancel all remaining IO, there might be reads pending if not entire request body was sent by client | ||
| AsyncIO?.Complete(); | ||
| // Allow writes to drain | ||
| if (_writeBodyTask != null) | ||
| { | ||
| await _writeBodyTask; | ||
| } | ||
|
|
||
| if (_readBodyTask != null) | ||
| { | ||
| await _readBodyTask; | ||
| // Cancel all remaining IO, there might be reads pending if not entire request body was sent by client | ||
| AsyncIO?.Complete(); | ||
|
|
||
| if (_readBodyTask != null) | ||
| { | ||
| await _readBodyTask; | ||
| } |
Comment on lines
+105
to
+107
| // Important cleanup, this ensures native is done with async completions and we can cleanup resources | ||
| // If this somehow didn't run and native was still doing async completions, we could end up AVing | ||
| // when trying to access managed objects that were already collected. |
halter73
approved these changes
May 8, 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.
While doing some random investigations and playing around with throwing from random places, I noticed that if
was somehow skipped it could cause AVs. So adding a
try {} finally {}to ensure that specific cleanup code isn't skipped.