Skip to content

.NET 6 Blazor server app not Disposing razor pages on browser tab close #39370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
jeffraska opened this issue Jan 7, 2022 · 6 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-server ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@jeffraska
Copy link

jeffraska commented Jan 7, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When IDisposable is implemented in razor page and browser tab is closed by user, the Dispose method is called after few minutes and not immediately after tab close.
The very first thing I'd mention is that disposing of razor pages worked correctly in .NET 5 so apparently it's a bug in .NET 6 (or ASP.NET Core Runtime). Problem started to appear after I've upgraded my project to .NET 6.

This problem was discused previously in https://github.com/dotnet/aspnetcore/issues/38147 but solution in answer does not solve this problem - .NET knows about tab being closed but just do not call Dispose.

Expected Behavior

When browser tab is closed .NET should dispose all razor pages that existed in this session.

Steps To Reproduce

  1. Create Blazor server app according to https://dotnet.microsoft.com/en-us/learn/aspnet/blazor-tutorial/create
  2. Implement IDisposable in Counter.razor and set breakpoint in Dispose method
  3. Add HubFilter using builder.Services.Configure<HubOptions>(O => O.AddFilter<HubFilter>()); and set breakpoint in OnDisconnectedAsync
  4. Start app and navigate to /counter
  5. Close tab
  6. OnDisconnectedAsync is called but Dispose is not.

Exceptions (if any)

No response

.NET Version

6.0.101

Anything else?

No response

@jeffraska
Copy link
Author

I studied problem further and it looks like problem appear in Vivaldi and Firefox but not in Google Chrome. So it's somehow browser issue.

@TanayParikh TanayParikh added area-blazor Includes: Blazor, Razor Components feature-blazor-server labels Jan 7, 2022
@TanayParikh
Copy link
Contributor

Hello @jeffraska thanks for contacting us. Blazor server disconnects the circuit based on the page unload event.

let disconnectSent = false;
const cleanup = () => {
if (!disconnectSent) {
const data = new FormData();
const circuitId = circuit.circuitId!;
data.append('circuitId', circuitId);
disconnectSent = navigator.sendBeacon('_blazor/disconnect', data);
}
};
Blazor.disconnect = cleanup;
window.addEventListener('unload', cleanup, { capture: false, once: true });

I studied problem further and it looks like problem appear in Vivaldi and Firefox but not in Google Chrome. So it's somehow browser issue.

Hmm I see, thanks for pointing that out. While Vivaldi isn't a supported browser, Firefox is. I have a feeling this may be a Firefox browser quirk to do with when/if the unload event is called.

@TanayParikh
Copy link
Contributor

Hmm based on the usage notes for unload, it doesn't seem like that's the recommended way to do things anymore.

We may want to consider updating this to utilize the visibilitychange event, however it may not have full support in Safari (will need to do some testing).

@javiercn
Copy link
Member

javiercn commented Jan 10, 2022

Our notification mechanism is a best effort not a guaranteed outcome, since the browser doesn't guarantee that the event is fired. Even in that case, this is a "performance" optimization to eagerly terminate the circuit when we know it won't be used any longer.

In this scenario dispose gets called after around 3 minutes (by default) when we detect the connection has been lost and the client doesn't re-connect as part of disposing the entire circuit.

@javiercn
Copy link
Member

Created a separate issue #39409 to track the suggestions from @TanayParikh

@javiercn javiercn added ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question and removed investigate labels Jan 10, 2022
@ghost ghost added the Status: Resolved label Jan 10, 2022
@ghost
Copy link

ghost commented Jan 11, 2022

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Jan 11, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 10, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-server ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants