Skip to content

WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed#66337

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-jsdisconnectedexception-blazorwebview
Draft

WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed#66337
Copilot wants to merge 4 commits intomainfrom
copilot/fix-jsdisconnectedexception-blazorwebview

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Parity with RemoteJSRuntime: throw JSDisconnectedException instead of JSException when components invoke JS interop during disposal of a reloading WebView page.

Description

When a BlazorWebView page reloads, the old PageContext is disposed while components implementing IAsyncDisposable may still invoke JS interop for cleanup. These calls hit the reloaded page's JS context where old object IDs no longer exist, producing unhandled JSException. RemoteJSRuntime (Blazor Server) already handles this by throwing JSDisconnectedException when the circuit is disposed — WebViewJSRuntime had no equivalent guard.

Changes

  • WebViewJSRuntime — Add _isDisposed flag with MarkAsDisconnected(). BeginInvokeJS throws JSDisconnectedException when disposed; EndInvokeDotNet/SendByteArray silently no-op.
  • PageContext.DisposeAsync — Call JSRuntime.MarkAsDisconnected() before Renderer.DisposeAsync(), matching the CircuitHost.DisposeAsync pattern.
  • IpcReceiver — Ignore incoming messages for a disposed page context to prevent stale messages from corrupting new page state.

JSObjectReference.DisposeAsync already catches JSDisconnectedException (#49418), so this completes the fix end-to-end.

Copilot AI and others added 3 commits April 16, 2026 11:55
…isposed

Add disconnection guard to WebViewJSRuntime, matching the existing behavior
in RemoteJSRuntime for Blazor Server circuits. When a BlazorWebView page
reloads, the old PageContext is disposed and components implementing
IAsyncDisposable that invoke JS interop during disposal will now see
JSDisconnectedException instead of an unhandled JSException.

Changes:
- WebViewJSRuntime: Add IsDisposed flag and MarkAsDisconnected() method;
  throw JSDisconnectedException in BeginInvokeJS; silently return in
  EndInvokeDotNet/SendByteArray when disposed.
- PageContext: Call JSRuntime.MarkAsDisconnected() before Renderer.DisposeAsync()
  (matching CircuitHost.DisposeAsync pattern).
- IpcReceiver: Ignore incoming messages for disposed page contexts.
- Add test verifying JSDisconnectedException during component dispose on reload.

Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/e322fe08-acb0-476a-82e1-724f17da8728

Co-authored-by: oroztocil <79744616+oroztocil@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix WebViewJSRuntime to throw JSDisconnectedException on PageContext disposal WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed Apr 16, 2026
Copilot AI requested a review from oroztocil April 16, 2026 11:58
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.

WebViewJSRuntime should throw JSDisconnectedException when PageContext is disposed (parity with RemoteJSRuntime)

2 participants