dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.44k stars 10.02k forks source link

The SignalR server is suspended and can no longer send messages to the client. #58828

Open zhenlei520 opened 1 day ago

zhenlei520 commented 1 day ago

I'm not sure if this is a bug. When using SignalR to push messages to the client, I encountered the following problems:

  1. The ConnectionId obtained by the SignalR server is inconsistent with the actual ConnectionId of the client

    ​ Scenario: The client encounters resource shortage, and the following error occurs after the client reconnects

  2. The SignalR service is suspended and can no longer send messages to the client.

    This often happens when the SignalR server sends a large amount of data to the client.

BrennanConroy commented 21 hours ago

The ConnectionId obtained by the SignalR server is inconsistent with the actual ConnectionId of the client

Can you explain?

Scenario: The client encounters resource shortage, and the following error occurs after the client reconnects

You didn't include an error.

The SignalR service is suspended and can no longer send messages to the client.

What do you mean suspended? And by service do you mean your server application?

zhenlei520 commented 18 hours ago

The ConnectionId obtained by the SignalR server is inconsistent with the actual ConnectionId of the client

We discovered this problem through logs. In the SignalR server log, we saw that the message was pushed to the client normally, but the corresponding client did not receive the corresponding message. Through the client reconnection log, we found that the corresponding ConnnectionId changed.

Scenario: The client encounters resource shortage, and the following error occurs after the client reconnects

The SignalR server pushes messages to the client in the BackgroundService. In the BackgroundService task, when the message is received in time, it sends the message to the corresponding client through SignalR, and this operation is in the try catch. However, when SignalR stops pushing messages, no error logs are found. What's even more strange is that after checking the client logs, it was found that during this period, there were still clients connected to the SignalR server, but it was unclear why the server push was blocked. It returned to normal after restarting the SignalR service. I don't know if this is related to the synchronous call of asynchronous methods, but I have never encountered such a problem when calling the push method with Parallel.ForEach before.

_hub.Clients.Client(data.ConnectionId).SendAsync(nameof(IRelationClient.PublishAsync), data.Dtos.ToArray()).Wait();

The SignalR service is suspended and can no longer send messages to the client.

What do you mean suspended? And by service do you mean your server application?

BrennanConroy commented 3 hours ago

Through the client reconnection log, we found that the corresponding ConnnectionId changed.

That's expected. Connection ID changes across new connections.

It sounds like you might be assuming that the ConnectionId persists and your code is written with that assumption. So _hub.Clients.Client(data.ConnectionId).SendAsync... will end up sending to a connection that no longer exists anymore, hence why you think things aren't working.