Repository navigation
Conversation
AnalyzeLocalFunctionOrLambda restored _variables after its try/finally. When the analysis of the body threw (for instance, the stack guard that reports CS8078), _variables still referenced the variables of the nested function, and Free() returned them twice to their pools. The analysis of unrelated compilations in the same process then shared the same pooled collections and crashed. Fixes dotnet#86276
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
…nPerfTests.cs Co-authored-by: Cyrus Najmabadi <cyrus.najmabadi@gmail.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The nullable-walker state-restoration change has no active review issues.
No open issues 🎉
What changed in this PR
This PR addresses #86276 by restoring C# nullable-walker state when lambda or local-function analysis throws, preventing pooled collections from being returned twice and corrupting later compilations.
Changes:
- Moves saved-state restoration into
finally, after snapshot creation. - Adds a Release-only regression test that triggers the stack guard inside a lambda, then checks unrelated compilations containing lambdas.
| File | Description |
|---|---|
src/Compilers/CSharp/Test/Semantic/Semantics/OverloadResolutionPerfTests.cs |
Adds coverage for stack-guard failure followed by unrelated lambda compilations. |
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs |
Restores the enclosing walker state during exception unwinding. |
Review effort: Balanced
RikkiGibson
approved these changes
Nov 1, 2026
jjonescz
approved these changes
Nov 2, 2026
Member
|
Thanks for the contribution, @meziantou |
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.
Fixes #86276
AnalyzeLocalFunctionOrLambdarestored_variables(and the other fields it saves) after itstry/finallyinstead of in thefinally. When the analysis of the body throws,_variablesstill references the variables of the nested function. For instance, the stack guard throws on deeply nested code, andNullableWalker.Analyzecatches the exception and reports CS8078.Free()then frees those variables twice: once from_nestedFunctionVariables, and once from the_variableschain.Variables.Free()returns pooled dictionaries and builders to static pools, so they end up twice in their pools. From then on, twoVariablesinstances of any later compilation in the process can share the same collections. The nullable analysis of unrelated code then fails with aNullReferenceExceptioninGetDefaultState, anArgumentExceptionorIndexOutOfRangeExceptioninVariables.Populate, orCollection was modifiedwhen methods are compiled in parallel. The crashes don't reproduce in a fresh process.The fix moves the restore into the
finallyblock, afterExitWalker, which still needs the variables of the nested function to create its snapshot.The test analyzes a lambda whose body hits the stack guard, then compiles unrelated code that uses lambdas. Like the other stack guard tests, it only runs in Release: in Debug, the stack guard path fails the nullable analysis verification and the pooled object validation.
Microsoft Reviewers: Open in CodeFlow