Skip to content

Restore the nullable walker state when the analysis of a lambda throws - #86277

Merged
jjonescz merged 3 commits into
dotnet:mainfrom
meziantou:fix-nullablewalker-lambda-variables-double-free
Nov 2, 2026
Merged

jjonescz merged 3 commits into
dotnet:mainfrom
meziantou:fix-nullablewalker-lambda-variables-double-free

Conversation

@meziantou

@meziantou meziantou commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #86276

AnalyzeLocalFunctionOrLambda restored _variables (and the other fields it saves) after its try/finally instead of in the finally. When the analysis of the body throws, _variables still references the variables of the nested function. For instance, the stack guard throws on deeply nested code, and NullableWalker.Analyze catches the exception and reports CS8078. Free() then frees those variables twice: once from _nestedFunctionVariables, and once from the _variables chain.

Variables.Free() returns pooled dictionaries and builders to static pools, so they end up twice in their pools. From then on, two Variables instances of any later compilation in the process can share the same collections. The nullable analysis of unrelated code then fails with a NullReferenceException in GetDefaultState, an ArgumentException or IndexOutOfRangeException in Variables.Populate, or Collection was modified when methods are compiled in parallel. The crashes don't reproduce in a fresh process.

The fix moves the restore into the finally block, after ExitWalker, 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

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

Copy link
Copy Markdown
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.

@dotnet-policy-service dotnet-policy-service Bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Nov 1, 2026
…nPerfTests.cs

Co-authored-by: Cyrus Najmabadi <cyrus.najmabadi@gmail.com>
@meziantou
meziantou marked this pull request as ready for review November 1, 2026 19:07
@meziantou
meziantou requested a review from a team as a code owner November 1, 2026 19:07
Copilot AI balanced review requested due to automatic review settings November 1, 2026 19:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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

@jjonescz
jjonescz merged commit eb07b17 into dotnet:main Nov 2, 2026
22 checks passed
@jjonescz

jjonescz commented Nov 2, 2026

Copy link
Copy Markdown
Member

Thanks for the contribution, @meziantou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullableWalker returns pooled Variables collections twice when lambda analysis throws, corrupting nullable analysis process-wide

6 participants