Repository navigation
fix(analyzers): enforce disposable field ownership - #11167
Conversation
There was a problem hiding this comment.
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
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
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, andTransactionRecoveryEventObserver(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.
| private Task OnStop(CancellationToken _) | ||
| { | ||
| _timer?.Dispose(); | ||
| Interlocked.Exchange(ref _timer, null)?.Dispose(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public void Dispose() => Interlocked.Exchange(ref _timer, null)?.Dispose(); |

Problem
CA1001 and CA2213 remain advisory across several otherwise-clean provider and test-kit projects, leaving disposable ownership contracts unenforced.
Solution
GrainProfiler,MemoryGrainStorageWithLatency, andTransactionRecoveryEventObserver.IDisposablecontract.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.Kubernetesremains 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