Skip to content

Fix CA2213 false positive with System.Threading.Lock - #57137

Open
Kielek wants to merge 1 commit into
dotnet:mainfrom
Kielek:codex/fix-ca2213-lock-disposal-tracking
Open

Kielek wants to merge 1 commit into
dotnet:mainfrom
Kielek:codex/fix-ca2213-lock-disposal-tracking

Conversation

@Kielek

@Kielek Kielek commented Nov 16, 2026

Copy link
Copy Markdown

Fixes #57136.

CA2213 can report a field as undisposed even when its Dispose() method is called unconditionally before a helper call involving System.Threading.Lock.

When analyzing the helper, the first field reference reinitializes its disposal state, overwriting the caller’s Disposed state. Preserve existing analysis state and initialize only previously untracked locations.

Adds four regression cases covering nullable-enabled code with System.Threading.Lock and object gates, including controls that verify missing disposal still produces CA2213.

Testing

  • CA2213 and CA2000 tests: 350 passed, 1 existing skip.
  • Verified both original repro variants build with zero warnings and errors on SDK 11.0.100 using the patched analyzer.

Keep existing disposal state when a helper first references a field instead of reinitializing it as undisposed. Add regression coverage for nullable-enabled native Lock and Monitor call chains, including missing-disposal controls.

Fixes dotnet#57136
@Kielek
Kielek requested a review from a team as a code owner November 16, 2026 07:15
Copilot AI balanced review requested due to automatic review settings November 16, 2026 07:15
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@Kielek

Kielek commented Nov 16, 2026

Copy link
Copy Markdown
Author

If you accept change, it will be great to propagate also to .NET11 and .NET10.
I am trying to switch https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation to Lock but it produces linked warnings.

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

No active review issues are reported for the change preserving disposal state during helper-field discovery.

No open issues 🎉

What changed in this PR

Addresses #57136, a CA2213 false positive when a disposable field is disposed before a helper call involving System.Threading.Lock. The shared disposal analysis now preserves existing location state instead of resetting it when discovering a field in a helper.

Changes:

  • Initialize disposal state only for previously untracked locations.
  • Add four nullable-enabled regression cases covering System.Threading.Lock and object gates, with controls verifying that missing disposal still reports CA2213.
File Description
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​Runtime/​DisposableFieldsShouldBeDisposedTests.cs Adds parameterized regression coverage for disposal before helper calls within try/finally.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​src/​Utilities/​FlowAnalysis/​FlowAnalysis/​Analysis/​DisposeAnalysis/​DisposeAnalysis.DisposeDataFlowOperationVisitor.cs Preserves existing disposal state during helper field discovery.

Review effort: Balanced

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA2213 false positive with System.Threading.Lock in a disposal call chain

3 participants