dotnet / dotnet/aspnetcore

SignalR connections are shutdown directly during graceful shutdowns

Open
#25,069 26 comments 8 reactions 1 assignee Claimed by @BrennanConroy View on GitHub
affected-most area-signalr enhancement Needs: Design Priority:1 severity-minor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

**Context:**
Hosting an application in a kubernetes environment can sometimes cause the service (with active connections) to be shutdown as autoscaling happens. We have implemented graceful shutdown behaviour for various parts of the application but I see no such feature/possibility for SignalR servers. This causes problems when using the [streaming features](https://docs.microsoft.com/en-us/aspnet/core/signalr/streaming?view=aspnetcore-3.1) in SignalR. The Hub interface does offer you a `OnDisconnectedAsync` but as it turns out this is fired after the connection has been closed (by the server itself).

**Things Tried**
I've stepped into the source code with a debugger to find out most is handled in privates/internal classes. Should a complete new implementation be written of a [HubConnectionHandler](https://github.com/aspnet/SignalR/blob/master/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionHandler.cs) that can deal with IApplicationLifetime? Or am I missing something like an injectable class that can deal with this. Or is there an existing SignalR lifetime available?
_Also; Im not sure if the HubFilters that seem to be coming out in a later release would help us with our problem._

**Question/Idea:**
We would like to await stream completion (with a max timeout) before the SignalR connection is cut off. Is it possible to interact with the [IApplicationLifetime](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iapplicationlifetime?view=aspnetcore-3.1) or await completion before SignalR [connections are actually closed](https://github.com/aspnet/SignalR/blob/master/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionHandler.cs#L152)?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.