Skip to content

fix(runtime): honor membership and load cancellation lifetimes - #11237

Merged
ReubenBond merged 27 commits into
dotnet:mainfrom
ReubenBond:rb-fix-runtime-cancellation-lifetimes
Sep 11, 2026
Merged

ReubenBond merged 27 commits into
dotnet:mainfrom
ReubenBond:rb-fix-runtime-cancellation-lifetimes

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Problem

Membership and deployment-load operations have distinct caller, background, shared-refresh, and shutdown lifetimes. Several paths discarded caller tokens or coupled shutdown publication to already-canceled background work. Membership providers also exposed tokenless operations, limiting cancellation to caller-side waits.

Solution

  • Add Async-suffixed, cancellation-aware IMembershipTable methods and mark the original tokenless methods obsolete. Preserve the original signatures and compatibility bridges; default implementations support external tokenless providers with cancellable waits and terminal fault observation.
  • Implement cancellation across ADO.NET, Azure Table, Cosmos DB, DynamoDB, Consul, Firestore, Redis, Cassandra, ZooKeeper, and the in-memory/system-target providers. Forward tokens into native backend APIs where supported. For tokenless SDK operations, bound waits, observe late faults, preserve connection ownership, and stop canceled paging, retry, and batch sequences before further I/O. Preserve parallel batch deletion and await all started batches to their actual completion.
  • Preserve successful backend results when cancellation arrives after the work completes. Selectors, buffered-row processing, result mapping, and completed initialization retain the operation's actual outcome.
  • Update runtime and test callsites to the cancellation-aware Async APIs. Keep shared refreshes alive until manager disposal, observe them once at creation, and let callers cancel their own waits. Settle cleanup acknowledgments when queued work stops, is rejected, or is evicted from the bounded queue.
  • Publish terminal statuses with a bounded live cleanup token and preserve the status actually committed to the membership snapshot. Cancel and finish a graceful status attempt before starting forced-stop publication. Keep ordinary provider errors recoverable during stop, and schedule callbacks so their cancellation and cleanup logic executes.
  • Use grain-timer lifetime tokens for deployment-load RPCs, preserve initial publication for nonpositive refresh intervals, and snapshot subscribers so each receives notification before subscriber failures are surfaced.

Wire and provider compatibility

The new membership RPC methods claim the original operation aliases using the existing source generator. Original generated request types remain available for calls through obsolete tokenless methods. Cancellation travels separately from the unchanged application-argument payload. Each receiver and backend retain responsibility for how underlying work responds to cancellation; cancellation can race with a committed write.

The regenerated API surfaces accompany the implementation. Focused regressions exercise real generated membership proxies with current and baseline-style serializer metadata, including legacy and Async calls, identical application payloads, and invocation outcomes. Other regressions cover legacy/default/native provider behavior, completed-result preservation, caller/shared/background ownership, cleanup queue pressure, shutdown status ordering, client ownership, parallel-batch cancellation, and load publication. Provider-authoring guidance documents these guarantees.

Relationship

This is the first replacement slice from #10236, based on main at 05ba80021d7c41e633d59f46844f2f9ba6d15c7d, expanded by review to migrate every membership provider. It can ship independently of dissemination and content-addressed manifest retrieval. Deployment-load mutation retains its existing scheduler-owned path and membership-oracle eligibility checks.

Microsoft Reviewers: Open in CodeFlow

Copilot AI lite review requested due to automatic review settings September 10, 2026 21:05

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

There is a build-blocking compile issue in IMembershipGossiper.cs due to a missing System.Threading import for CancellationToken.

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

Review tier: Lite
Findings: 1 High severity

New issues introduced by this change (1)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​IMembershipGossiper.cs — IMembershipGossiper is missing System.Threading import
What changed in this PR

This PR tightens cancellation/lifetime handling across Orleans runtime membership and deployment-load publishing paths so caller-provided tokens are honored (without unintentionally canceling shared/background operations) and shutdown/stop sequences preserve required functionality.

Changes:

  • Propagate cancellation tokens through membership operations (refresh, gossip, probes, suspect/kill processing) with bounded waits while ensuring late task faults remain observed.
  • Update deployment-load publishing to use a grain timer (with timer lifetime tokens) and to snapshot subscriber lists so notification failures/unsubscribes don’t prevent delivery to remaining subscribers.
  • Add a cancellable RunOrQueueTask<TResult> scheduler overload and introduce focused regression tests for these ownership/cancellation boundaries.
File Description
test/​Orleans.Core.Tests/​Runtime/​DeploymentLoadPublisherTests.cs Adds regression tests for deployment-load publication cancellation, timers, and subscriber behavior.
test/​Orleans.Core.Tests/​Membership/​MembershipTableManagerTests.cs Updates existing tests and adds new coverage for refresh/update cancellation and shutdown/maintenance lifetimes.
test/​Orleans.Core.Tests/​Membership/​MembershipGossiperTests.cs Adds tests validating cancellation propagation for gossip/probe/indirect-probe RPCs.
test/​Orleans.Core.Tests/​Membership/​MembershipAgentTests.cs Adds shutdown/heartbeat-stop regressions to ensure terminal status publication uses live bounded tokens.
test/​Orleans.Core.Tests/​Membership/​ClusterHealthMonitorTests.cs Updates calls to pass cancellation tokens through updated membership APIs.
src/​Orleans.Runtime/​Scheduler/​SchedulerExtensions.cs Adds cancellable RunOrQueueTask<TResult> overload used by indirect probe paths.
src/​Orleans.Runtime/​Placement/​DeploymentLoadPublisher.cs Switches to grain timer publication, forwards cancellation, snapshots subscribers, and preserves initial publication behavior.
src/​Orleans.Runtime/​MembershipService/​RemoteSiloProber.cs Forwards caller cancellation directly into probe system-target calls.
src/​Orleans.Runtime/​MembershipService/​MembershipTableManager.cs Introduces distinct lifetimes (maintenance vs manager lifetime), adds bounded-wait wrappers, and settles suspect/kill acknowledgements on maintenance exit.
src/​Orleans.Runtime/​MembershipService/​MembershipSystemTarget.cs Threads cancellation through gossip/probe RPC entry points and bounds waits while observing late faults.
src/​Orleans.Runtime/​MembershipService/​MembershipGossiper.cs Extends gossip API to accept/validate cancellation tokens and forwards them.
src/​Orleans.Runtime/​MembershipService/​MembershipAgent.cs Ensures shutdown status publication uses bounded “cleanup” lifetime and avoids canceling terminal publication due to heartbeat cancellation.
src/​Orleans.Runtime/​MembershipService/​IMembershipGossiper.cs Updates the interface signature to accept cancellation tokens (but currently missing a required import).

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

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

🔵 Needs a closer look

It changes core runtime lifecycle and membership cancellation/timeout semantics across multiple subsystems, which warrants final human validation despite the added targeted tests.

Review tier: Lite
Findings: 1 High severity

Pre-existing issues (1)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​IMembershipGossiper.cs — IMembershipGossiper is missing System.Threading import View comment

Copilot AI review requested due to automatic review settings September 10, 2026 21:20

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

🔵 Needs a closer look

The changes affect core cluster membership, probing, gossip, and shutdown lifetimes where subtle cancellation/timing regressions can have high operational impact and warrants final human verification.

Review tier: Lite
Findings: 1 High severity

Pre-existing issues (1)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​IMembershipGossiper.cs — IMembershipGossiper is missing System.Threading import View comment

Copilot AI review requested due to automatic review settings September 10, 2026 21:28

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

Unresolved cleanup acknowledgment loss, stale generated API output, missing cleanup-token forwarding, and a path-restricted workflow condition require fixes.

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

Review tier: Lite
Findings: 3 High severity · 1 Medium severity

New issues introduced by this change (3)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​MembershipTableManager.cs — Acknowledged cleanup requests can be dropped
High severity src/​Orleans.Runtime/​MembershipService/​MembershipTableManager.cs — Retry requeue can discard cleanup acknowledgments
Medium severity src/​Orleans.Core/​SystemTargetInterfaces/​IMembershipTable.cs — Regenerate and commit the Core API surface
Pre-existing issues (1)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​IMembershipGossiper.cs — IMembershipGossiper is missing System.Threading import View comment

Copilot AI review requested due to automatic review settings September 10, 2026 21:36

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

Five unresolved findings remain, including one critical cleanup-acknowledgment issue.

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

Review tier: Lite
Findings: 4 High severity · 1 Medium severity

New issues introduced by this change (1)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​MembershipTableManager.cs — Draining after channel completion can fault on a live token
Pre-existing issues (4)
Severity Finding
High severity src/​Orleans.Runtime/​MembershipService/​MembershipTableManager.cs — Retry requeue can discard cleanup acknowledgments View comment
High severity src/​Orleans.Runtime/​MembershipService/​MembershipTableManager.cs — Acknowledged cleanup requests can be dropped View comment
High severity src/​Orleans.Runtime/​MembershipService/​IMembershipGossiper.cs — IMembershipGossiper is missing System.Threading import View comment
Medium severity src/​Orleans.Core/​SystemTargetInterfaces/​IMembershipTable.cs — Regenerate and commit the Core API surface View comment

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

🔵 Needs a closer look

Unresolved moderate findings remain across provider waits, lifecycle timeouts and cleanup, scheduler cancellation, and refresh coordination.

Review tier: Lite
Findings: None

Issues resolved since last review (1)
Severity Finding
Medium severity src/​AdoNet/​Shared/​Storage/​RelationalStorageExtensions.cs — The per-record callback receives a token but does not use it, so cancellation during enumeration… View resolved comment
Previously missed findings (2)

In code that hasn't changed since last review

src/Orleans.Runtime/MembershipService/MembershipTableManager.cs:136

  • pendingRefresh is read and assigned without synchronization, but Refresh is reachable concurrently from periodic maintenance, membership RPC/gossip, health, and service paths. Two callers can both observe a null or completed value and start separate RefreshInternal operations, causing duplicate backend reads/cleanup and allowing the later assignment to hide one task from subsequent callers. Guard single-flight task creation with synchronization (while keeping caller-specific WaitAsync outside the guard).
    src/Orleans.Runtime/Scheduler/SchedulerExtensions.cs:69
  • After queuing this work item, the method awaits workItem.Task without applying cancellationToken to that wait. If cancellation arrives while the target scheduler is busy or stopped, callers such as ProbeRemoteSiloIndirectly remain pending until the queued item runs (and may never complete); the token is only observed inside the queued delegate. Observe the work-item task and use WaitAsync(cancellationToken) for the caller's wait.

Copilot AI review requested due to automatic review settings September 11, 2026 06:00
Copilot stopped reviewing on behalf of ReubenBond due to an error September 11, 2026 06:20

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.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review tier: Lite
Findings: 1 Low severity

New issues introduced by this change (1)
Severity Finding
Low severity src/​Cassandra/​Orleans.Clustering.Cassandra/​CassandraClusteringTable.cs — ReadRowAsync returns MembershipTableData, which is non-nullable here, so `if (existingRow is…

Copilot AI review requested due to automatic review settings September 11, 2026 06: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

🟡 Changes recommended

Unresolved critical and moderate findings remain in cancellation, scheduling, proxy generation, refresh, and shutdown paths.

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

Review tier: Lite
Findings: 1 High severity · 3 Medium severity

New issues introduced by this change (4)
Severity Finding
High severity src/​Orleans.CodeGenerator/​ProxyGenerator.cs — Avoid matching a second Async suffix
Medium severity src/​Orleans.Runtime/​MembershipService/​MembershipSystemTarget.cs — Probe waits on the non-cancelable scheduler overload
Medium severity src/​Orleans.Runtime/​Placement/​DeploymentLoadPublisher.cs — Startup timer registration cannot cancel its queued wait
Medium severity src/​Orleans.Runtime/​Scheduler/​SchedulerExtensions.cs — Queued scheduler waits ignore caller cancellation
Issues resolved since last review (1)
Severity Finding
Low severity src/​Cassandra/​Orleans.Clustering.Cassandra/​CassandraClusteringTable.cs — ReadRowAsync returns MembershipTableData, which is non-nullable here, so `if (existingRow is… View resolved comment

Copilot AI review requested due to automatic review settings September 11, 2026 18:17
Copilot stopped reviewing on behalf of ReubenBond due to an error September 11, 2026 18:38

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.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review tier: Lite
Findings: 3 Medium severity · 1 Low severity

New issues introduced by this change (4)
Severity Finding
Medium severity src/​AWS/​Orleans.Clustering.DynamoDB/​Membership/​DynamoDBMembershipTable.cs — Batch deletes are now awaited sequentially. This reduces throughput when deleting many membership… View comment
Medium severity src/​AdoNet/​Shared/​Storage/​RelationalStorage.cs — Using CancellationToken.None for Task.Run means pre-canceled operations will still queue work… View comment
Medium severity src/​Azure/​Orleans.Clustering.AzureStorage/​OrleansSiloInstanceManager.cs — The large-batch delete path changed from parallel deletes (Task.WhenAll) to sequential awaits.… View comment
Low severity src/​Google/​Orleans.Clustering.Firestore/​FirestoreMembershipTable.cs — Cleanup now performs chunk deletions sequentially whereas Task.WhenAll(...) previously allowed… View comment
Issues resolved since last review (4)
Severity Finding
Medium severity src/​Orleans.Runtime/​Scheduler/​SchedulerExtensions.cs — Queued scheduler waits ignore caller cancellation View resolved comment
Medium severity src/​Orleans.Runtime/​Placement/​DeploymentLoadPublisher.cs — Startup timer registration cannot cancel its queued wait View resolved comment
Medium severity src/​Orleans.Runtime/​MembershipService/​MembershipSystemTarget.cs — Probe waits on the non-cancelable scheduler overload View resolved comment
High severity src/​Orleans.CodeGenerator/​ProxyGenerator.cs — Avoid matching a second Async suffix View resolved comment

Copilot AI review requested due to automatic review settings September 11, 2026 18:42

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

🔵 Needs a closer look

Three moderate review findings remain unresolved.

Review tier: Lite
Findings: None

Issues resolved since last review (4)
Severity Finding
Low severity src/​Google/​Orleans.Clustering.Firestore/​FirestoreMembershipTable.cs — Cleanup now performs chunk deletions sequentially whereas Task.WhenAll(...) previously allowed… View resolved comment
Medium severity src/​Azure/​Orleans.Clustering.AzureStorage/​OrleansSiloInstanceManager.cs — The large-batch delete path changed from parallel deletes (Task.WhenAll) to sequential awaits.… View resolved comment
Medium severity src/​AdoNet/​Shared/​Storage/​RelationalStorage.cs — Using CancellationToken.None for Task.Run means pre-canceled operations will still queue work… View resolved comment
Medium severity src/​AWS/​Orleans.Clustering.DynamoDB/​Membership/​DynamoDBMembershipTable.cs — Batch deletes are now awaited sequentially. This reduces throughput when deleting many membership… View resolved comment
Previously missed findings (1)

In code that hasn't changed since last review

src/Orleans.Runtime/MembershipService/MembershipSystemTarget.cs:204

  • This catch is reachable when the provider throws an OperationCanceledException carrying a linked or backend token: the preceding filter requires exact token equality, so caller cancellation can fall through here, be logged as a refresh failure, and complete the RPC successfully. Filter on cancellationToken.IsCancellationRequested instead so caller cancellation is propagated regardless of which token the provider reports.

Copilot AI review requested due to automatic review settings September 11, 2026 18:58
Copilot stopped reviewing on behalf of ReubenBond due to an error September 11, 2026 19:18

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.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review tier: Lite
Findings: 1 Low severity

New issues introduced by this change (1)
Severity Finding
Low severity src/​AWS/​Orleans.Clustering.DynamoDB/​Membership/​DynamoDBMembershipTable.cs — The async lambda in the Select creates an extra state machine and an unnecessary double-await.… View comment

await Task.WhenAll(tasks);
// Capture synchronous cancellation as a task so every started batch remains owned by WhenAll.
await Task.WhenAll(toDelete.BatchIEnumerable(MAX_BATCH_SIZE)
.Select(async batch => await storage.DeleteEntriesAsync(this.options.TableName, batch, cancellationToken)));
@ReubenBond
ReubenBond marked this pull request as ready for review September 11, 2026 21:05
@ReubenBond
ReubenBond merged commit 76394f1 into dotnet:main Sep 11, 2026
73 checks passed
@ReubenBond
ReubenBond deleted the rb-fix-runtime-cancellation-lifetimes branch September 11, 2026 21:05
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 12, 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