Skip to content

fix(test): keep observer cancellation targets alive - #10897

Merged
ReubenBond merged 2 commits into
dotnet:mainfrom
ReubenBond:rb-fix-flaky-interleaving-cancellation
Aug 28, 2026
Merged

ReubenBond merged 2 commits into
dotnet:mainfrom
ReubenBond:rb-fix-flaky-interleaving-cancellation

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

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 expected OperationCanceledException.

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

Copilot AI lite review requested due to automatic review settings August 28, 2026 08:24

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

Review tier: Lite
Findings: 1 Medium severity

New issues introduced by this change (1)
Severity Finding
Medium severity 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 cancelImmediately path, the observers are not otherwise used before the cancellation assertions. If Assert.ThrowsAnyAsync fails and throws, the current cleanup block (including GC.KeepAlive) is skipped, which undermines the lifetime guarantee this test relies on. Consider moving the unsubscribe/delete/KeepAlive logic into a finally so 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, placing GC.KeepAlive(observer) after Assert.ThrowsAnyAsync means 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 a try and move cleanup + KeepAlive into a finally to 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.

Copilot AI review requested due to automatic review settings August 28, 2026 08:52

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

Review tier: Lite
Findings: None

Issues resolved since last review (1)
Severity Finding
Medium severity test/​Orleans.Runtime.Tests/​CancellationTests/​ObserverCancellationTokenTests.cs — GC.KeepAlive(observer) is currently only executed after the cancellation assertion and cleanup.… View resolved comment

@ReubenBond
ReubenBond merged commit 5da028b into dotnet:main Aug 28, 2026
74 checks passed
@ReubenBond
ReubenBond deleted the rb-fix-flaky-interleaving-cancellation branch August 28, 2026 15:49
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 28, 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.

Flaky InterleavingObserverTaskCancellation on macOS net8.0

2 participants