Skip to content

Fixed circuit disconnects caused by navigation during slow custom CircuitHandler execution - #68633

Open
BrundhaVelusamy wants to merge 41 commits into
dotnet:mainfrom
BrundhaVelusamy:59657_CircuitHandler
Open

BrundhaVelusamy wants to merge 41 commits into
dotnet:mainfrom
BrundhaVelusamy:59657_CircuitHandler

Conversation

@BrundhaVelusamy

@BrundhaVelusamy BrundhaVelusamy commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Bug Description

When a custom CircuitHandler has latency in OnCircuitOpenedAsync/OnConnectionUpAsync (e.g., await Task.Delay(...)), navigating to a different page before the handler completes can permanently break the circuit, surfacing to the user as "Circuit has been shut down due to error".

Root cause: while the initial UpdateRootComponents batch is suspended awaiting the slow handler, the renderer's dispatcher yields and a second batch — triggered by the navigation — runs concurrently. This second batch can send Update/Remove operations for SSR component IDs that the first (still in-flight) batch hasn't registered yet, and the existing WebRootComponentManager.UpdateRootComponentAsync/RemoveRootComponent throw InvalidOperationException for unregistered IDs. That exception propagates up and fires CircuitHost.UnhandledException, tearing down the circuit even though the client is otherwise healthy.

Description of code changes

CircuitHost.cs

  • Added a per-circuit FIFO task chain for root-component update batches.
  • UpdateRootComponents reserves each incoming batch behind the previously received batch under _rootComponentUpdateLock.
  • UpdateRootComponentsCore waits for the preceding batch before dispatching the current batch.
  • Extracted the dispatcher-owned processing into UpdateRootComponentsOnDispatcher.
  • Circuit handler execution, state restoration, root-component operations, and acknowledgement now complete before the next batch starts.
  • A separate completion task is resolved in finally, preventing an unexpected failure from permanently blocking later batches.
  • Completed batches are acknowledged using CancellationToken.None, allowing acknowledgement through the current client connection after reconnection.

This prevents navigation batches from overtaking an initial batch delayed by OnCircuitOpenedAsync or OnConnectionUpAsync, while preserving every ordered root-component delta.

Output

Before changes

59657_BeforeFix.mp4

After changes

59657_AfterFix.mp4

Fixes #59657

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 19, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @BrundhaVelusamy. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@Youssef1313 Youssef1313 added the area-blazor Includes: Blazor, Razor Components label Sep 2, 2026
@BrundhaVelusamy
BrundhaVelusamy marked this pull request as ready for review September 21, 2026 07:19
@BrundhaVelusamy
BrundhaVelusamy requested a review from a team as a code owner September 21, 2026 07:19
Copilot AI lite review requested due to automatic review settings September 21, 2026 07:19

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

🟢 Approval recommended

The remaining request for broader Selenium coverage is a non-blocking nit.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

Fixes circuit disconnects caused by navigation overtaking delayed circuit-handler execution by serializing root-component update batches per circuit.

Changes:

  • Added FIFO batching with disposal and overflow handling.
  • Ensured ordered completion and acknowledgements.
  • Added unit coverage for ordering and lifecycle behavior.
File Summary
src/​Components/​Server/​test/​Circuits/​CircuitHostTest.cs Adds regression and queue lifecycle coverage.
src/​Components/​Server/​src/​Circuits/​CircuitHost.cs Implements serialized root-component update processing.

@PureWeen

Copy link
Copy Markdown
Member

/review

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 1, 2026
@PureWeen

Copy link
Copy Markdown
Member

/review

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.

🟡 Changes recommended

A critical active finding shows the FIFO barrier is released before the current batch acknowledgement completes, allowing acknowledgements to arrive out of order.

1 open issue
3 resolved since last review

Review effort: Lite

@PureWeen

Copy link
Copy Markdown
Member

The local G/S/O panel completed review of 1fda1601bb1ecbd93c00e682f8c9b809b01763bc with no new blocking verified findings or intent questions. agent-reviewed-needs-changes remains because the current-head CCR thread is unresolved; that is not an additional local-panel finding.

Informational, non-blocking observations

Posted for informational purposes so the author can confirm the verifier didn't discard or leave unresolved a real finding; this item does not block agent-reviewed-approved.

  • [UNRESOLVED] Test-gate cancellation: src/Components/test/testassets/Components.TestServer/CircuitHandlerTestGate.cs:80 does not pass the supplied cancellation token to its wait. Retention until release or timeout is source-established, but the binding cancellation contract and exact browser-abort sequence were not established. The verifier dropped the candidate as unestablished.
  • [UNRESOLVED] Browser-test ingress timing: src/Components/test/E2ETest/ServerRenderingTests/InteractivityTest.cs:356 releases initialization after observing DOM state, without explicitly joining receipt of the second server update. Whether this permits every assertion to pass with the ordering fix reverted remains unverified; the verifier downgraded it for missing browser/transport evidence.

Routine discarded check: No lock cycle or material effect was established for inline callbacks under the update lock at CircuitHost.cs:783. Already-covered feedback and author-answered questions are not repeated.

Untested coverage gaps: Actual browser-to-hub overlap and overflow through the real client/hub were not runtime-validated. These are evidence limits, not additional proven defects.

Validation boundary: All three local arms reviewed frozen source only; none ran target builds, tests, or browser validation. Hosted /review completed with an explicit no-findings noop.

Human-directed and GitHub Copilot-generated.

@BrundhaVelusamy

Copy link
Copy Markdown
Contributor Author

The local G/S/O panel completed review of 1fda1601bb1ecbd93c00e682f8c9b809b01763bc with no new blocking verified findings or intent questions. agent-reviewed-needs-changes remains because the current-head CCR thread is unresolved; that is not an additional local-panel finding.

Informational, non-blocking observations

Posted for informational purposes so the author can confirm the verifier didn't discard or leave unresolved a real finding; this item does not block agent-reviewed-approved.

  • [UNRESOLVED] Test-gate cancellation: src/Components/test/testassets/Components.TestServer/CircuitHandlerTestGate.cs:80 does not pass the supplied cancellation token to its wait. Retention until release or timeout is source-established, but the binding cancellation contract and exact browser-abort sequence were not established. The verifier dropped the candidate as unestablished.
  • [UNRESOLVED] Browser-test ingress timing: src/Components/test/E2ETest/ServerRenderingTests/InteractivityTest.cs:356 releases initialization after observing DOM state, without explicitly joining receipt of the second server update. Whether this permits every assertion to pass with the ordering fix reverted remains unverified; the verifier downgraded it for missing browser/transport evidence.

Routine discarded check: No lock cycle or material effect was established for inline callbacks under the update lock at CircuitHost.cs:783. Already-covered feedback and author-answered questions are not repeated.

Untested coverage gaps: Actual browser-to-hub overlap and overflow through the real client/hub were not runtime-validated. These are evidence limits, not additional proven defects.

Validation boundary: All three local arms reviewed frozen source only; none ran target builds, tests, or browser validation. Hosted /review completed with an explicit no-findings noop.

Human-directed and GitHub Copilot-generated.

@PureWeen, Thanks for the additional review. I checked the gate cancellation and E2E synchronization observations. The gate has a bounded wait and is released during cleanup, and the focused managed tests deterministically cover both operation sequencing and removal before initial-render quiescence. I don’t see evidence requiring another production change or additional E2E coverage.

@PureWeen

Copy link
Copy Markdown
Member

/review

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.

🔵 Needs a closer look

A critical concurrency flaw remains in CircuitHost.cs: successor root-component batches can run after an initial lifecycle/render failure because the predecessor barrier is released too early, and no regression test verifies this failure path.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@PureWeen

Copy link
Copy Markdown
Member

Copilot's local review panel found no issues to raise at this commit.

G, S, and O completed their independent reviews of dfaa54ea9289d2e3c98af2b1d5b42a77392e812d with no new blocking findings or intent questions. Previously raised agent threads are resolved or outdated.

Informational, non-blocking checks

Posted for informational purposes so the author can confirm the verifier didn't discard or leave unresolved a real finding; this item does not block agent-reviewed-approved.

No candidate verifier was invoked this round because no new provisional finding remained.

  • [DISCARDED] Acknowledgment payload/cancellation and root-registration changes: CircuitHost.cs:873 selects the same payload overload/default token as before; WebRootComponentManager.cs:56 still registers the root before awaiting rendering.
  • [DISCARDED] Supporting implementation checks: queue cancellation/disposal (CircuitHost.cs:205,885), WebAssembly startup failure handling (WebAssemblyHost.cs:255), per-batch allocations and restore ordering (CircuitHost.cs:796,988), logging (CircuitHost.cs:1227), and the Selenium click mechanism (InteractivityTest.cs:343) did not establish a new material defect. Previously discussed checks and author-answered questions are not repeated.

Untested coverage gaps: The arms noted no focused tests for later-batch synchronous Add failures or CaptureActivationTask. Browser/SignalR ingress overlap, overflow/teardown, and contention/disposal scheduling were not runtime-validated. These are evidence limits, not additional proven defects.

Validation boundary: All three local arms reviewed frozen source only; none ran PR builds, tests, or browser checks. Hosted /review completed with an explicit no-findings noop.

Human-directed and GitHub Copilot-generated.

@BrundhaVelusamy
BrundhaVelusamy requested review from a team and wtgodbe as code owners November 23, 2026 05:49
@kotlarmilos

Copy link
Copy Markdown
Member

Please keep this focused on the root-component ordering race. The ten-batch limit introduces a new circuit-termination policy beyond the reported bug. Please address that policy separately and keep the bug fix minimal, without leaving queued work unbounded.

@BrundhaVelusamy

Copy link
Copy Markdown
Contributor Author

Please keep this focused on the root-component ordering race. The ten-batch limit introduces a new circuit-termination policy beyond the reported bug. Please address that policy separately and keep the bug fix minimal, without leaving queued work unbounded.

@kotlarmilos, Thanks for the feedback. Some of the earlier changes were included based on review feedback received from Copilot, GitHub Actions, and PureWeen. I have now updated the commit to keep the fix focused on root-component ordering while also bounding admission.

The changes are:

  1. Removed the ten-batch overflow and circuit-termination policy, including overflow tracking, error reporting, logging, and rejection-specific tests.
  2. Added per-circuit bounded admission for root-component update batches. Ten batches may be admitted at a time; subsequent hub calls wait for an available slot instead of adding more accepted work behind a blocked predecessor.
  3. Updated ComponentHub.UpdateRootComponents to await admission, while the admitted update continues through the existing fire-and-forget processing path.
  4. Circuit disposal cancels admission waiters, and any state associated with work that was not admitted is cleared.
  5. Preserved the FIFO operations-applied barrier. Later batches wait until earlier root registrations, updates, or removals have been applied, without waiting for initial-render quiescence or acknowledgements.
  6. Kept the existing activation-failure behavior by ensuring root-component creation failures remain observable through the existing batch error handling path.

The regression coverage now includes delayed circuit-handler ordering, bounded admission, removal before initial-render quiescence, disposal of queued updates, and acknowledgement after connection cancellation. The focused CircuitHostTest suite passes with 62 tests passed, 0 failed, and 0 skipped.

This branch was previously deployed

1 inactive (outdated) deployment
copilot-pat-pool — 0a9e333e Deployed Aug 19, 2026 by dotnet-policy-service[bot] via pat_pool #924
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-reviewed-approved area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Custom CircuitHandler latency breaks WS connection on navigation

5 participants