Repository navigation
fix(test): keep observer cancellation targets alive - #10897
Conversation
There was a problem hiding this comment.
Copilot review overview
Review tier: Lite
Findings: 1
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
test/Orleans.Runtime.Tests/CancellationTests/ObserverCancellationTokenTests.cs — GC.KeepAlive(observer) is currently only executed after the cancellation assertion and cleanup.… |
What changed in this PR
This PR targets flakiness in ObserverCancellationTokenTests caused by Orleans holding observer targets via weak references, allowing the observer implementation to be garbage-collected too early (especially on immediate-cancellation paths), which can prevent cancellation from reaching the observer and make the test pass unexpectedly.
Changes:
- Add
GC.KeepAlive(...)calls to keep observer implementations strongly reachable through subscription, cancellation, unsubscribe, and object reference cleanup. - Extend the lifetime guarantee to the multi-observer test case by keeping each observer alive.
| File | Description |
|---|---|
| test/Orleans.Runtime.Tests/CancellationTests/ObserverCancellationTokenTests.cs | Adds GC.KeepAlive calls intended to prevent observer target GC during cancellation test execution. |
Suppressed comments (2)
test/Orleans.Runtime.Tests/CancellationTests/ObserverCancellationTokenTests.cs:251
- In the
cancelImmediatelypath, the observers are not otherwise used before the cancellation assertions. IfAssert.ThrowsAnyAsyncfails and throws, the current cleanup block (includingGC.KeepAlive) is skipped, which undermines the lifetime guarantee this test relies on. Consider moving the unsubscribe/delete/KeepAlive logic into afinallyso it runs regardless of assertion outcome and forces the observer targets to remain live through the awaited operations.
foreach (var g in grains)
{
fixture.GrainFactory.DeleteObjectReference<ILongRunningObserver>(g.Reference);
GC.KeepAlive(g.Observer);
}
test/Orleans.Runtime.Tests/CancellationTests/ObserverCancellationTokenTests.cs:330
- As with
ObserverTaskCancellation, placingGC.KeepAlive(observer)afterAssert.ThrowsAnyAsyncmeans it will never run when the assertion fails (the exact flaky mode this change is trying to prevent), so the observer can still be collected early in the immediate-cancellation path. Wrap the assertion+optional wait in atryand move cleanup +KeepAliveinto afinallyto guarantee execution.
await grain.Unsubscribe(reference);
fixture.GrainFactory.DeleteObjectReference<ILongRunningObserver>(reference);
GC.KeepAlive(observer);
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Copilot review overview
Review tier: Lite
Findings: None
Issues resolved since last review (1)
| Severity | Finding |
|---|---|
test/Orleans.Runtime.Tests/CancellationTests/ObserverCancellationTokenTests.cs — GC.KeepAlive(observer) is currently only executed after the cancellation assertion and cleanup.… View resolved comment |

Problem
Observer cancellation tests create local object references whose implementations are held weakly by the runtime. In immediate-cancellation paths, the JIT can end the implementation lifetime after
CreateObjectReference, allowing collection before interleaving dispatch. Cancellation then never reaches the observer and the call can complete without the expectedOperationCanceledException.PR #10790 established the required lifetime guarantee for the related cancellation suites, but did not cover
ObserverCancellationTokenTests.Solution
Keep each observer implementation strongly reachable until after its Orleans object reference is deleted, including every observer in the multi-observer case. This preserves the local target through request dispatch, cancellation, unsubscribe, and reference cleanup.
Fixes #10847
Microsoft Reviewers: Open in CodeFlow