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

Cannot access disposed IServiceProvider on shutdown

eranikid opened this issue · comments

Describe the bug

When being shut down, an ASP.NET Core app using Azure SignalR generates several errors:

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'IServiceProvider'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateScope()
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnDisconnectedAsync(HubConnectionContext connection, Exception exception)
   at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.HubOnDisconnectedAsync(HubConnectionContext connection, Exception exception)
   at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.RunHubAsync(HubConnectionContext connection)
   at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.OnConnectedAsync(ConnectionContext connection)
   at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.OnConnectedAsync(ConnectionContext connection)
   at Microsoft.Azure.SignalR.ServiceConnection.ProcessApplicationTaskAsyncCore(ClientConnectionContext connection)
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.GetReadAsyncResult()
   at Microsoft.Azure.SignalR.ServiceConnection.ProcessOutgoingMessagesAsync(ClientConnectionContext connection, CancellationToken token)

Error count is less than 10% of the connection count at the time of shutdown, and appears to fluctuate slightly.
These do not appear to have any negative impact, except hindering our zero-error policy.

I am not sure whether this issue lies in baseline Microsoft.AspNetCore.SignalR, or in this library, and I would be grateful if you'd help me find out and route my issue to appropriate place. I would gladly provide any additional debug information to help.

Further technical details

  • Your Azure SignalR SDK version - 1.21.7
  • ASP.NET Core 6.0
  • SignalR Client SDK is Kotlin version @ 5.0.6

When the application shuts down and disposes IServiceProvider, HubConnectionHandler.RunHubAsync is still running and runs into _dispatcher.OnDisconnectedAsync, even with #1876, we will see error log ErrorDispatchingHubEvent from https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/server/Core/src/HubConnectionHandler.cs#L229C12-L229C41.

Hi @BrennanConroy, do you know how aspnetcore signalr avoid such issue when shutting down the application? In theory, it could also happen when not using Azure SignalR?

It happens without ASRS as well. People haven't really complained about it though, so we haven't prioritized fixing it.

So the issue is in baseline Microsoft.AspNetCore.SignalR, it will be fixed someday in the future, until then our best bet is demoting the error down to warning, is that right? If so, just close the issue.

We fixed the error thrown from Azure SignalR SDK, but the fix for error thrown from inside the hub dispatcher depends on Microsoft.AspNetCore.SignalR. I am closing the issue here.