Skip to content
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

Blazor is NOT loosing SignalR connection. Browser is! #40721

Closed
1 task done
HybridSolutions opened this issue Mar 15, 2022 · 15 comments
Closed
1 task done

Blazor is NOT loosing SignalR connection. Browser is! #40721

HybridSolutions opened this issue Mar 15, 2022 · 15 comments
Labels
area-blazor Includes: Blazor, Razor Components Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together feature-blazor-server ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@HybridSolutions
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Those who work with Blazor are fully aware of the SignalR connection failed issue and the adorable "Reload" message. In a recent project, I noticed something very interesting that I never saw mentioned by anyone.

Expected Behavior

No response

Steps To Reproduce

Test 1

  1. Opened two tabs in Edge browser, latest version, Windows 11, each with different websites using Blazor.
  2. Waited for 10 minutes. Both tabs went into "sleep" mode.
  3. Went back to each of the tabs and there it was, the no connection message with the reload option.

Test 2

  1. Opened two tabs in Edge browser, latest version, Windows 11, each with different websites using Blazor.
  2. For the second tab I opened Developer Tools (f12) and made sure the no cache option was on.
  3. Waited for 10 minutes. Tabs 1 went into "sleep" mode. Tab 2 did not because of the Developer Window being open.
  4. Went back to each of the tabs. Tab 1 had the "no connection/reload message". Tab 2 still had the website working perfectly.
  5. Tested again, but now leaving the browser untouched for more that 2h and tab 1 failed once again while tab 2 resisted and worked perfectly.

Test 3
Just did the same tests but reversed the websites in tab 1 and 2. Now the website in tab 1 is working fine every time and the one in tab 2, consistently fails after awhile.

Tested this more than 5 times and the result is exactly the same every time. Opening Developer Tools with the no cache option on, is somehow preventing SignalR connection to fail or at least is allowing it to reconnect every single time without a glitch. Are you aware of this?

Exceptions (if any)

No response

.NET Version

.NET 6 latest version

Anything else?

No response

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-server labels Mar 15, 2022
@TanayParikh TanayParikh added the Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together label Mar 15, 2022
@TanayParikh
Copy link
Contributor

@HybridSolutions if I follow correctly, you're pointing out that the Blazor Server SignalR connection remains connected (doesn't time out/disconnect) if the dev tools (with cache disabled) is left opened. Is that correct?

The fact that the tab with developer tools doesn't go to sleep isn't surprising, it's likely just an edge feature. The fact that having the dev tools open prevents the SignalR connection from going "on standby" is the part I'm unsure about. @BrennanConroy is this expected SignalR behavior?

@HybridSolutions
Copy link
Author

HybridSolutions commented Mar 15, 2022

@TanayParikh that's exactly what I'm saying. I only tried a couple of hours max but I suppose that is enough time as a test. I can make a video of my screen if you need to see it happening. I find this behaviour amazing because all the issues out there are always mentioning a problem on SignalR connection but after all there's no issue with it since it can hold wonderfully even longer than a regular session would.

You can test it for yourself too. Just go to a website like Blazorise that uses Blazor, and do my Test 1 and 2. I actually discovered this while debugging a Blazor web application.

@BrennanConroy
Copy link
Member

If the tab is "active" from the browsers point of view, then SignalR will continue to function. If the tab goes to sleep, then SignalR will stop working. This is the expected behavior.

@HybridSolutions
Copy link
Author

HybridSolutions commented Mar 15, 2022

@BrennanConroy I'm not saying it's not the expected behavior, but the problem that troubles so many developers about Blazor apps keep disconnecting after so little time and having to deal with the browser message is not a Blazor problem after all. It's because of the browser sleeping feature. Maybe Edge could receive an indication in the header that sleeping is not allowed 😁 That would solve the problem. Have to say that Microsoft created a problem that works against Blazor with that feature...

@BrennanConroy
Copy link
Member

If you want to "solve" the sleeping tab problem there are solutions you can implement to workaround it.
https://docs.microsoft.com/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&tabs=visual-studio#bsleep

Have to say that Microsoft created a problem that works against Blazor with that feature...

Tab sleeping isn't a Microsoft only feature btw.

@HybridSolutions
Copy link
Author

HybridSolutions commented Mar 15, 2022

If you want to "solve" the sleeping tab problem there are solutions you can implement to workaround it. https://docs.microsoft.com/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&tabs=visual-studio#bsleep

Have to say that Microsoft created a problem that works against Blazor with that feature...

Tab sleeping isn't a Microsoft only feature btw.

Yes I know that. Just kidding. Thanks for the link.

I'll give a look into it. It's important that this issue could be better documented, including perhaps warnings about this sleeping tabs Blazor "enemy", because there's a lot of complaints out there about lost connections after a very short amount of time on idle. Personally, for now, I developed a solution that automatically refreshes the current page if no connection is possible after a few attempts but I hope this is improved in Blazor in a near future.

Have to ask though, shouldn't blazor deal with those sleeping tabs behind the scenes, at least as an option? They simply kill any blazor app after a few minutes. Just asking...

@TanayParikh
Copy link
Contributor

but I hope this is improved in Blazor in a near future.

There are a couple of issues tracking work in this area: #9256, #27576

@HybridSolutions
Copy link
Author

@TanayParikh Thanks, I was already following those.

@konradbartecki
Copy link

Hey that's a good find, but on mobile browsers you have to assume they will always suspend the tab much more aggressively to preserve resources.

So if you ask me, fix should be done on a Blazor side anyway to gracefully restore connections, or have #32113 as a workaround where we just restart the page automatically.

@HybridSolutions
Copy link
Author

Hey that's a good find, but on mobile browsers you have to assume they will always suspend the tab much more aggressively to preserve resources.

So if you ask me, fix should be done on a Blazor side anyway to gracefully restore connections, or have #32113 as a workaround where we just restart the page automatically.

Yes you are right, mobile is another nightmare. I'm using something similar to this

Blazor.defaultReconnectionHandler.onReconnectionFailed = function (d) { document.location.reload(); }

but with connection retry before it reloads automatically. It works great for now.

@konradbartecki
Copy link

True but if I remember correctly in .NET 5 and earlier there's like a 3s delay before your connection actually fails, not sure if they fixed that in new .NET versions.

@HybridSolutions
Copy link
Author

HybridSolutions commented Mar 16, 2022

True but if I remember correctly in .NET 5 and earlier there's like a 3s delay before your connection actually fails, not sure if they fixed that in new .NET versions.

Using .NET 6 latest version and connection always fails to reconnect. It's a mystery why 🧐. Can't see the days of this issue to be over...

@konradbartecki
Copy link

konradbartecki commented Mar 16, 2022

I believe it is because after some time the state of the connection on a server side gets disposed, thus there is nothing to reconnect to, so it will always fail and there is no good way to handle that really.
If you gonna make the server keep the state for longer then you risk running out of RAM much faster, if you gonna save it to HDD for longer term storage then maybe you'll make yourself more susceptible to DoS and even then is restoring a state after a long time what your users expect?

@HybridSolutions
Copy link
Author

@konradbartecki I'm not sure what can be done, but something must happen because this behaviour it's not a good experience for users/clients. They just don't like that their websites and web apps keep showing these connection messages after returning from a coffee break. I'm sure .NET/Blazor team will come up with something 💪
At least is on the .NET 7 list and that is already good.... I guess...

@SteveSandersonMS
Copy link
Member

We think this is covered by #32113, so will close in favour of that.

@SteveSandersonMS SteveSandersonMS added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Mar 17, 2022
@ghost ghost added the Status: Resolved label Mar 17, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together feature-blazor-server ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

6 participants