Skip to content

fix(analyzers): enforce disposable field ownership - #11167

Merged
ReubenBond merged 1 commit into
dotnet:mainfrom
ReubenBond:rb-disposable-field-ownership
Sep 6, 2026
Merged

ReubenBond merged 1 commit into
dotnet:mainfrom
ReubenBond:rb-disposable-field-ownership

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Problem

CA1001 and CA2213 remain advisory across several otherwise-clean provider and test-kit projects, leaving disposable ownership contracts unenforced.

Solution

  • Enable CA1001 and CA2213 for the complete Google and Redis provider families, including Google linked shared sources, plus Dashboard, Persistence.Memory, Serialization.TestKit, Transactions.TestKit.Base, and Transactions.
  • Dispose resources owned by GrainProfiler, MemoryGrainStorageWithLatency, and TransactionRecoveryEventObserver.
  • Keep dashboard no-op logger scopes process-wide instead of treating them as per-provider owned resources.
  • Document the precise external/lifetime ownership of the Firestore lifecycle semaphore, Redis adapter semaphore, serializer session pool, and activation cancellation source with narrow suppressions.
  • Add focused coverage for profiler lifecycle cleanup, fixture-borrowed serializer services, and armed/reached transaction phase-gate cleanup.
  • Regenerate the Persistence.Memory public API surface for its IDisposable contract.

Rationale

The changes make ownership explicit without disposing caller- or container-owned resources, preserve semaphores and cancellation tokens for all outstanding operations, and ensure reached transaction gates remain owned until blocked emitters are released. Orleans.Hosting.Kubernetes remains deferred because lifecycle cancellation can currently return before monitoring tasks finish; disposing its client, semaphore, or cancellation source in that state could race active monitoring work.

Microsoft Reviewers: Open in CodeFlow

Copilot AI lite review requested due to automatic review settings September 6, 2026 09:11

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.

Copilot review overview

🟡 Changes recommended

GrainProfiler.OnStart can overwrite _timer without disposing an existing instance, which can leak timers if start is invoked more than once or races with disposal.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 Medium severity

New issues introduced by this change (1)
Severity Finding
Medium severity src/​Dashboard/​Orleans.Dashboard/​Metrics/​GrainProfiler.cs — OnStart assigns a new Timer to _timer without disposing any existing instance, so if the lifecycle…
What changed in this PR

This PR tightens disposable-ownership contracts across several Orleans provider and test-kit projects by elevating CA1001/CA2213 enforcement, adding narrowly-justified suppressions where lifetimes are external, and updating a few types to correctly dispose owned resources. It also adds targeted tests to validate lifecycle cleanup/idempotency behavior and updates the generated public API surface for Orleans.Persistence.Memory.

Changes:

  • Enable CA1001/CA2213 warnings for Google/Redis provider families and several additional src/ components via .editorconfig.
  • Implement/adjust disposal behavior for GrainProfiler, MemoryGrainStorageWithLatency, and TransactionRecoveryEventObserver (plus suppressions where ownership is external).
  • Add focused tests validating disposal semantics for dashboard profiling, serializer fixture borrowing, and transaction recovery phase-gate cleanup.
File Description
test/​Transactions/​Orleans.Transactions.Tests/​TransactionRecoveryEventObserverDisposalTests.cs New tests ensuring observer disposal releases phase gates and is idempotent.
test/​Orleans.Serialization.UnitTests/​FieldCodecTesterBorrowedOwnershipTests.cs New test verifying fixture-backed testers don’t dispose shared serializer services.
test/​Orleans.Dashboard.Tests/​Orleans.Dashboard.UnitTests/​GrainProfilerDisposableOwnershipTests.cs New tests validating GrainProfiler start/stop/dispose idempotency.
src/​Redis/​Orleans.Streaming.Redis/​Streams/​RedisStreamAdapter.cs Adds CA1001 suppression documenting shared semaphore lifetime.
src/​Orleans.Transactions/​State/​ActivationLifetime.cs Adds CA1001 suppression documenting activation-owned cancellation source lifetime.
src/​Orleans.Transactions.TestKit.Base/​TestRunners/​TransactionRecoveryEventObserver.cs Tracks/disposes reached phase gates to ensure disposal unblocks emitters.
src/​Orleans.Serialization.TestKit/​FieldCodecTester.cs Adds CA2213 suppression clarifying service-provider/fixture ownership of session pool.
src/​Orleans.Persistence.Memory/​Storage/​MemoryStorageWithLatency.cs Makes the latency wrapper disposable and disposes its owned inner storage.
src/​Google/​Orleans.Reminders.Firestore/​FirestoreReminderTable.cs Adds CA1001 suppression documenting semaphore lifetime.
src/​Dashboard/​Orleans.Dashboard/​Metrics/​GrainProfiler.cs Implements IDisposable and disposes timer via atomic exchange on stop/dispose.
src/​Dashboard/​Orleans.Dashboard/​Implementation/​DashboardLogger.cs Makes the no-op scope process-wide (static) to avoid per-instance disposable ownership.
src/​api/​Orleans.Persistence.Memory/​Orleans.Persistence.Memory.cs Regenerated API surface reflecting MemoryGrainStorageWithLatency : IDisposable.
.editorconfig Elevates CA1001/CA2213 to warnings for targeted src/ subtrees.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 53 to +59
private Task OnStop(CancellationToken _)
{
_timer?.Dispose();
Interlocked.Exchange(ref _timer, null)?.Dispose();
return Task.CompletedTask;
}

public void Dispose() => Interlocked.Exchange(ref _timer, null)?.Dispose();
@ReubenBond
ReubenBond merged commit 0b0d7d1 into dotnet:main Sep 6, 2026
71 checks passed
@ReubenBond
ReubenBond deleted the rb-disposable-field-ownership branch September 6, 2026 15:13
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants