You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Connection failed: HubConnection.connectionClosed(Error: Server returned an error on close: Connection closed with an error.) called while in state Disconnecting.
#1905
Open
agustinsilvano opened this issue
Jan 31, 2024
· 4 comments
I'm encountering an issue with my application that leverages SignalR for server-to-client communication using WebSockets. The client is a Next.js application (Azure AppService), and the backend is an ASP.NET Core API 7.0 (Azure AppService). While everything works flawlessly in my local environment with a local API (including local WebSocket usage), the problem arises when integrating the Azure SignalR Service. The problem is also present when using my local environment but using Azure SignalR Service as a service to perform the WS connection.
Issue Description
The issue manifests as approximately 30% of the connections failing during the initial connection with the server. The negotiation with my API is successful, returning the URL of the Azure SignalR Service. Subsequently, this negotiation also succeeds, and the handshake is completed. However, the problematic connections receive the message Close message received from the server., leading to a subsequent error when attempting stopConnection(undefined).
When this error is present during the connection, it prevents the establishment of a connection to the server. Consequently, notifications emitted through SignalR are not sent to the "failed" client. Additionally, the retry policy is ineffective, as the reconnection message is not present. Even by commenting out my code (that performs the disconnect operation) does not avoid this disconnection issue which indicates that the disconnection is handled internally by SignalR. Attempts to manage the _connectionState property of the HubConnection guided by the error, particularly when the status is Disconnecting, have not yielded any positive changes.
Client logs
Configured the LogLevel to Debug while establishing the connection to the Hub, and got:
Logs on failed connection:
Logs on successful connection:
Here is a log file that includes both cases. The messages MESSAGE: GeneratingRecommendation and MESSAGE: GeneratedRecommendation are the notifications received on the client side through the hub. The message ###Connection started: undefined is a console.log inside of the then() method of the promise returned by the connection.start() method of the @microsoft/signalr module.
API logs
We have configured logging with Serilog and the log.txt file doesn't show any clue of the issue.
Logs on successful connection:
The difference is that the Connection started is present in one case and not in the other.
Azure Analytic logs
The last thing that I did was to enable the Diagnostic Logs on the Azure SignalR Service to get more context about the error and only can see two errors on the failed connections.
Connection ended. Reason:
Connection ended. Reason: Application server closed the connection.
Couldn't get any clue of it.
Metrics from Diagnose and solve problems tool show ClosedByAppServer as a prominent metric:
We (@GersonDias and me) finally figured out our issue. It was a misconfigured client that set the ClientKeepAliveInterval to 30 seconds (so it sends a ping every 30 seconds), while the server had all default settings (ClientTimeoutInterval == 30 seconds), which means the client didn't send the pings often enough and the server killed the connection.
Hope that might help someone in the future finding this issue.
Describe the bug
Introduction
I'm encountering an issue with my application that leverages SignalR for server-to-client communication using WebSockets. The client is a Next.js application (Azure AppService), and the backend is an ASP.NET Core API 7.0 (Azure AppService). While everything works flawlessly in my local environment with a local API (including local WebSocket usage), the problem arises when integrating the Azure SignalR Service. The problem is also present when using my local environment but using Azure SignalR Service as a service to perform the WS connection.
Issue Description
The issue manifests as approximately 30% of the connections failing during the initial connection with the server. The negotiation with my API is successful, returning the URL of the Azure SignalR Service. Subsequently, this negotiation also succeeds, and the handshake is completed. However, the problematic connections receive the message
Close message received from the server.
, leading to a subsequent error when attemptingstopConnection(undefined)
.When this error is present during the connection, it prevents the establishment of a connection to the server. Consequently, notifications emitted through SignalR are not sent to the "failed" client. Additionally, the retry policy is ineffective, as the reconnection message is not present. Even by commenting out my code (that performs the disconnect operation) does not avoid this disconnection issue which indicates that the disconnection is handled internally by SignalR. Attempts to manage the
_connectionState
property of the HubConnection guided by the error, particularly when the status is Disconnecting, have not yielded any positive changes.Client logs
Configured the LogLevel to
Debug
while establishing the connection to the Hub, and got:Logs on failed connection:
Logs on successful connection:
Here is a log file that includes both cases. The messages
MESSAGE: GeneratingRecommendation
andMESSAGE: GeneratedRecommendation
are the notifications received on the client side through the hub. The message###Connection started: undefined
is aconsole.log
inside of thethen()
method of the promise returned by theconnection.start()
method of the@microsoft/signalr
module.API logs
We have configured logging with Serilog and the log.txt file doesn't show any clue of the issue.
Logs on successful connection:
Logs on failed connection:
The difference is that the
Connection started
is present in one case and not in the other.Azure Analytic logs
The last thing that I did was to enable the Diagnostic Logs on the Azure SignalR Service to get more context about the error and only can see two errors on the failed connections.
Connection ended. Reason:
Connection ended. Reason: Application server closed the connection.
Couldn't get any clue of it.
Metrics from
Diagnose and solve problems
tool showClosedByAppServer
as a prominent metric:To Reproduce
Client Side
SocketService.js
Usage
API Side
The SignalR registration on the ConfigureServices looks like:
The Hub code can not be published(but this is working ok using my local resources), but we are overriding the
OnConnectedAsync
:ClaimsPrincipal
to use the token information as HttpContext information.OnConnectedAsync
.Also, we are overriding the
OnDisconnectedAsync
but we are justContext.UserIdentifier
to have info about it.OnDisconnectedAsync
.Further technical details
Microsoft.Azure.SignalR Version="1.22.0"
Microsoft.AspNetCore.SignalR Version="7.0.0"
@microsoft/signalr Version="7.0.12"
The text was updated successfully, but these errors were encountered: