Skip to content

Suppress JSDisconnectedException in IJSObjectReference.DisposeAsync - #64773

Merged
javiercn merged 3 commits into
mainfrom
copilot/fix-jsobjectreference-dispose
Dec 17, 2025
Merged

javiercn merged 3 commits into
mainfrom
copilot/fix-jsobjectreference-dispose

Conversation

Copilot AI commented Dec 15, 2025 •

Copy link
Copy Markdown
Contributor

Suppress JSDisconnectedException in IJSObjectReference.DisposeAsync

  • 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.

Catch and discard JSDisconnectedException during JS object disposal

Description

When a circuit disconnects (user navigates away, closes tab, or full-page reload), components disposing IJSObjectReference instances throw JSDisconnectedException. This forces developers to wrap disposal in try-catch blocks.

Changes:

  • Modified JSObjectReference.DisposeAsync() to catch and suppress JSDisconnectedException
  • Added tests verifying exception suppression on single and multiple dispose calls

Rationale:

If JS is disconnected, the JS-side object is already gone. Server-side disposal would fail regardless, so the exception provides no value and only creates noise.

Before:

public async ValueTask DisposeAsync()
{
    if (_jsModule is not null)
    {
        try
        {
            await _jsModule.DisposeAsync();
        }
        catch (JSDisconnectedException)
        {
            // Must manually catch in every component
        }
    }
}

After:

public async ValueTask DisposeAsync()
{
    if (_jsModule is not null)
    {
        await _jsModule.DisposeAsync(); // No try-catch needed
    }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Change IJSObjectReference.Dispose specifically so that it deals with catching and discarding JSDisconnectedException internally</issue_title>
<issue_description>I'm getting a warning and exception when trying to dispose a JavaScript module reference in a component's DisposeAsync method when the component is using the server interactive render mode.

Repro steps:

Expected result: No exception
Actual result: JSDisconnectedException

warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
      Unhandled exception rendering component: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.
      Microsoft.JSInterop.JSDisconnectedException: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.
         at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
         at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
         at Microsoft.JSInterop.Implementation.JSObjectReference.DisposeAsync()
         at BestForYouRecipes.IngredientsListEditor.DisposeAsync() in C:\Users\daroth\Documents\GitHub\danroth27\BestForYouRecipes\BestForYouRecipes.Client\IngredientsListEditor.razor:line 136
         at Microsoft.AspNetCore.Components.RenderTree.Renderer.<>c__DisplayClass83_0.<<Dispose>g__HandleAsyncExceptions|1>d.MoveNext()

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'yAE5LTi2-XXk6e9JbmT_P1RmxksmTriTYTGkrzPgH2I'.
      Microsoft.JSInterop.JSDisconnectedException: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.
         at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
         at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
         at Microsoft.JSInterop.Implementation.JSObjectReference.DisposeAsync()
         at BestForYouRecipes.IngredientsListEditor.DisposeAsync() in C:\Users\daroth\Documents\GitHub\danroth27\BestForYouRecipes\BestForYouRecipes.Client\IngredientsListEditor.razor:line 136
         at Microsoft.AspNetCore.Components.RenderTree.Renderer.<>c__DisplayClass83_0.<<Dispose>g__HandleAsyncExceptions|1>d.MoveNext()
```</issue_description>

<agent_instructions>One thing we could do is change IJSObjectReference.Dispose specifically so that it deals with catching and discarding JSDisconnectedException internally. It would be safe to do since if JS is disconnected, then there's no reason why the server should be concerned about disposing IJSObjectReference instances.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@SteveSandersonMS</author><body>
> Expected result: No exception
> Actual result: JSDisconnectedException

Why do you expect no exception? The component is trying to do a JS interop call after the browser connection has been closed, so it should throw this exception. You do need to catch it. This isn't specific to .NET 8 - the same thing would happen on .NET 7 if a user closes their browser tab or navigates to an external site.

I see that the usability here is quite poor though. One thing we could do is change `IJSObjectReference.Dispose` specifically so that it deals with catching and discarding `JSDisconnectedException` internally. It would be safe to do since if JS is disconnected, then there's no reason why the server should be concerned about disposing `IJSObjectReference` instances.</bod...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes dotnet/aspnetcore#49418

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for you](https://github.com/dotnet/aspnetcore/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 15, 2025 14:01
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix IJSObjectReference.Dispose to handle JSDisconnectedException Suppress JSDisconnectedException in IJSObjectReference.DisposeAsync Dec 15, 2025
Copilot AI requested a review from javiercn December 15, 2025 14:06
@javiercn
javiercn marked this pull request as ready for review December 17, 2025 14:02
@javiercn
javiercn requested a review from a team as a code owner December 17, 2025 14:02
Copilot AI review requested due to automatic review settings December 17, 2025 14:02

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.

Pull request overview

This PR enhances the developer experience when working with IJSObjectReference disposal in Blazor Server scenarios by automatically suppressing JSDisconnectedException during disposal operations. The change addresses a common pain point where developers had to manually wrap disposal calls in try-catch blocks to handle circuit disconnections.

Key Changes:

  • Modified JSObjectReference.DisposeAsync() to catch and suppress JSDisconnectedException internally
  • Added comprehensive test coverage for both single and multiple disposal attempts with disconnected JS runtime

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/JSInterop/Microsoft.JSInterop/src/Implementation/JSObjectReference.cs Added try-catch block in DisposeAsync() to suppress JSDisconnectedException during disposal
src/JSInterop/Microsoft.JSInterop/test/JSObjectReferenceTest.cs Added two test cases and test infrastructure to verify exception suppression behavior

@javiercn
javiercn merged commit 63b69a2 into main Dec 17, 2025
30 of 31 checks passed
@javiercn
javiercn deleted the copilot/fix-jsobjectreference-dispose branch December 17, 2025 14:23
@dotnet-policy-service dotnet-policy-service Bot added this to the 11.0-preview1 milestone Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants