Skip to content

Catch and discard JSDisconnectedException in IJSObjectReference.DisposeAsync for WebView#66259

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-ijsobjectreference-dispose-exception
Draft

Catch and discard JSDisconnectedException in IJSObjectReference.DisposeAsync for WebView#66259
Copilot wants to merge 3 commits intomainfrom
copilot/fix-ijsobjectreference-dispose-exception

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

When components dispose IJSObjectReference instances during DisposeAsync, a JSDisconnectedException can be thrown if the JS runtime is no longer available (circuit disconnected, WebView navigated away, or WebView disposed). This is a spurious error — the JS side is already gone, so server-side cleanup of the reference is unnecessary.

Description

JSObjectReference.DisposeAsync already catches and discards JSDisconnectedException. This PR extends the same pattern to Blazor WebView:

  • WebViewJSRuntime: Added _disconnected flag + internal MarkAsDisconnected() method. When set, BeginInvokeJS throws JSDisconnectedException immediately (same pattern as RemoteJSRuntime.MarkPermanentlyDisconnected()).

  • PageContext.DisposeAsync: Calls JSRuntime.MarkAsDisconnected() before Renderer.DisposeAsync(), preventing JS interop calls from hanging or throwing during component teardown when the page has navigated away or the WebView is being disposed.

  • Tests: Two new tests verify that the JS runtime is marked disconnected on WebView disposal and on page navigation.

// PageContext.DisposeAsync — before this change, DisposeAsync on a JSObjectReference
// would hang or propagate exceptions when the WebView was already gone
public async ValueTask DisposeAsync()
{
    JSRuntime.MarkAsDisconnected(); // new
    await Renderer.DisposeAsync();
    await _serviceScope.DisposeAsync();
}

Copilot AI and others added 2 commits April 10, 2026 07:29
Copilot AI changed the title [WIP] Fix IJSObjectReference.Dispose to handle JSDisconnectedException Catch and discard JSDisconnectedException in IJSObjectReference.DisposeAsync for WebView Apr 10, 2026
Copilot AI requested a review from javiercn April 10, 2026 07:33
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change IJSObjectReference.Dispose specifically so that it deals with catching and discarding JSDisconnectedException internally

2 participants