Azure / azure-signalr

Azure SignalR Service SDK for .NET

Home Page:https://aka.ms/signalr-service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Azure SignalR on Blazor with Hub causing too many connections

Ericvf opened this issue · comments

commented

Describe the bug

When adding a Hub to our Blazor page, we see a lot of connections being made. It happens as soon as we use the ConnectionHub to Build a connection and Start it. After a period of time the AppService will no longer work due to this exception:

"An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full."

To Reproduce

  1. Create a new server-side blazor app and setup like this example:
    https://learn.microsoft.com/en-us/aspnet/core/blazor/tutorials/signalr-blazor?view=aspnetcore-7.0&tabs=visual-studio-code&pivots=server

  2. Install the Microsoft.Azure.SignalR package and add AddAzureSignalR in your application builder. Also setup the connectionstring in appsettings.json

  3. Deploy to Azure

  4. Observe how in mere seconds the Live Trace Tool creates hundred of connections.

You can see from the screenshot below that our "/signalrhub" is producing ConnectionStarted and ConnectionEnded events per 40ms. It quickly fills up the Live Trace tool to thousands of messages.

image

  1. For reference, this is what we have in the Razor page:
    private async Task InitializeSignalR()
    {
        if (hubConnection == null)
        {
            hubConnection = new HubConnectionBuilder()
                .WithUrl(Navigation.ToAbsoluteUri("/signalrhub"))
                .WithAutomaticReconnect()
                .Build();

             // Not even a handler here yet
            await hubConnection.StartAsync();
        }
    }

Further technical details

  • Our application is Dotnet7 with all the latest package versions.
commented

We have been able to get the application stable by re-ordering some of the middleware. Strangely.
The number of messages we see in the Live Trace is still the same. Can anyone confirm that this is expected? What exactly does the routing of the client connection mean and why does it reconnect that many times?

Maybe @davidfowl knows?

"/signalrhub" is producing ConnectionStarted and ConnectionEnded events per 40ms

No it's abnormal. It sounds like your hubconnection object got refreshed every 40ms. Where is this InitializeSignalR logic and where is this hubConnection object? Might the hubConnection object is in some component and got refreshed?

commented

Our app currently is very simple and only has one page. The HubConnection is created in the OnInitialize of the page:

    HubConnection hubConnection;

    protected override async Task OnInitializedAsync()
    {
        await InitializeSignalR();
    }

    async Task InitializeSignalR()
    {
        if (hubConnection == null)
        {
            hubConnection = new HubConnectionBuilder()
                .WithUrl(Navigation.ToAbsoluteUri("/signalrhub"))
                .WithAutomaticReconnect()
                .Build();

            hubConnection.On<SignalModel>("AddSignal", OnAddSignal);

            if (hubConnection.State == HubConnectionState.Disconnected)
            {
                await hubConnection.StartAsync();
            }
        }
    }

I can confirm that this code is not being called repeatedly.

Its been a while since I looked at the Live Trace so I just opened it, and the problem is gone!? I'm not sure what could have caused this behavior and why it is gone now. Might have been an issue wit the Azure SignalR service? We didn't change anything in our app.