Repository navigation
Preserve ExceptionDispatchInfo.SetCurrentStackTrace across throw - #660
Merged
Merged
Conversation
This was referenced Oct 12, 2026
This was referenced Oct 13, 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.
Bug
The
ExceptionDispatchInfo.SetCurrentStackTracepolyfill wrote the captured trace toException._stackTraceString. The runtime resets that field when the exception is thrown, so the captured trace was lost as soon as the exception was thrown or surfaced through anawait, which is the whole point of the API (e.g.TaskCompletionSource.SetException(ExceptionDispatchInfo.SetCurrentStackTrace(ex))).Fix
Store the trace the way the BCL does: in
_remoteStackTraceString, followed by the "End of stack trace from previous location" marker. The runtime preserves that field across throws and prepends it toException.StackTrace. If the field cannot be found, the previous_stackTraceStringbehaviour is kept as a fallback.Test
ExceptionDispatchInfoSetCurrentStackTrace_SurvivesThrowand_SurvivesAwaitcapture the trace in a namedNoInliningmethod, throw / await the exception, and assert the method name is still inStackTrace. Both failed on net48 before the fix; they pass on net462 / net48 (polyfill) and net8.0 / net10.0 (BCL).