Skip to content

Fix orphaned RemoteJSDataStream when sendJSDataStream initiation throws - #69871

Merged
kotlarmilos merged 18 commits into
dotnet:mainfrom
irfanajaffer:69838_RemoteJsFix
Nov 6, 2026
Merged

kotlarmilos merged 18 commits into
dotnet:mainfrom
irfanajaffer:69838_RemoteJsFix

Conversation

@irfanajaffer

Copy link
Copy Markdown
Contributor

Description

Fixes an issue where a failed sendJSDataStream initiation leaves a RemoteJSDataStream orphaned and can subsequently terminate the Blazor Server circuit.

When RemoteJSDataStream.CreateRemoteJSDataStreamAsync creates a new stream instance, it registers the stream with the runtime before invoking the JavaScript-side Blazor._internal.sendJSDataStream initialization. If that JS interop call throws, the stream instance remains registered and is never cleaned up. The orphaned stream can later trigger timeout/disposal logic that raises an unhandled exception and ends the circuit unexpectedly.

This change ensures that failures during stream initiation are handled correctly by cleaning up the partially-created stream before rethrowing the original exception.

Root Cause

CreateRemoteJSDataStreamAsync creates and registers a RemoteJSDataStream instance before starting the JS-side transfer:

var remoteJSDataStream = new RemoteJSDataStream(...);

await runtime.InvokeVoidAsync(
    "Blazor._internal.sendJSDataStream",
    jsStreamReference,
    streamId,
    chunkSize);

If InvokeVoidAsync("Blazor._internal.sendJSDataStream", ...) throws, execution exits immediately and the created stream remains tracked in RemoteJSRuntime.RemoteJSDataStreamInstances. Since the stream was never fully initialized nor disposed, its timeout/disposal path can later raise an unhandled exception, causing the circuit to terminate.

The fix wraps the initiation call in a try/catch block and explicitly disposes the stream when initiation fails:

try
{
    await runtime.InvokeVoidAsync(
        "Blazor._internal.sendJSDataStream",
        jsStreamReference,
        streamId,
        chunkSize);

    return remoteJSDataStream;
}
catch
{
    await remoteJSDataStream.CompletePipeAndDisposeStream();
    throw;
}

This ensures:

  • The stream is removed from the runtime's active stream collection.
  • Associated resources are released immediately.
  • No orphaned stream remains after a failed initiation.
  • Circuit-ending unhandled exceptions are prevented while preserving the original failure behavior.

After Fix

  • Failed sendJSDataStream initialization correctly cleans up the created RemoteJSDataStream.
  • No stale entries remain in RemoteJSRuntime.RemoteJSDataStreamInstances.
  • The original exception continues to propagate to the caller.
  • Stream timeout/disposal logic no longer triggers unhandled exceptions from abandoned streams.
  • Blazor Server circuits remain stable when stream initialization fails.

Testing

Added regression coverage for stream initiation failures:

  • Added CreateRemoteJSDataStreamAsync_DisposesStream_WhenSendJSDataStreamThrows.
  • Verifies that the created stream is disposed when sendJSDataStream initialization throws.
  • Confirms the original exception is rethrown to the caller.
  • Validates that the runtime no longer tracks the stream after cleanup.
  • Ensures cleanup occurs for both synchronous and asynchronous failure scenarios.

Validation Performed

Before Fix

Before.fix.-.69838.mp4

After Fix

Aftre.fix.-.69838.mp4

Fixes #69838

@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Oct 16, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 16, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@irfanajaffer
irfanajaffer marked this pull request as ready for review October 16, 2026 14:35
@irfanajaffer
irfanajaffer requested a review from a team as a code owner October 16, 2026 14:35
Copilot AI balanced review requested due to automatic review settings October 16, 2026 14:35

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

The required frozen review evidence was not supplied, leaving approval checks incomplete.

0 open findings

What changed in this PR

Addresses #69838 by cleaning up Blazor Server streams when JavaScript transfer initiation fails.

Changes:

  • Disposes the partially created stream before rethrowing the initiation exception.
  • Adds synchronous and asynchronous failure coverage for cleanup, exception preservation, and timeout handling.
File Description
src/​Components/​Server/​test/​Circuits/​RemoteJSDataStreamTest.cs Adds initiation-failure regression tests.
src/​Components/​Server/​src/​Circuits/​RemoteJSDataStream.cs Cleans up streams when initiation throws.

🧠 Review effort: Balanced


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

@kotlarmilos kotlarmilos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the test, not needed

@PureWeen

Copy link
Copy Markdown
Member

Project status update: the latest maintainer review asks to drop the test, and there has not been an author update after that review. I moved the PR and its linked issue to In progress on the Blazor Syncfusion project. Please address the review feedback, then move it back to In review when it is ready for another review.

@irfanajaffer

Copy link
Copy Markdown
Contributor Author

/azp run aspnetcore-components-e2e

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 69871 in repo dotnet/aspnetcore

@PureWeen

PureWeen commented Nov 3, 2026

Copy link
Copy Markdown
Member

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

@kotlarmilos
kotlarmilos merged commit c4d66d1 into dotnet:main Nov 6, 2026
39 checks passed
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 5%

No docs needed: the single changed file adds try/catch cleanup of a partially created RemoteJSDataStream. It's an internal fix, not insufficient evidence.

Documentation surfaces considered

  • Conceptual article: Not required — Internal bug fix with no public API or documented behavior change.
  • Migration guidance: Not required — No action needed by users upgrading.
  • Breaking change: Not required — No compatibility impact; original exception is still rethrown.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 405bb089 Deployed Nov 6, 2026 by kotlarmilos via conclusion #211
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor Server: RemoteJSDataStream is orphaned and ends the circuit when sendJSDataStream initiation throws

4 participants