fix(sessions): start the host session drain at ApplicationStopping, before Kestrel's drain (SES-001, #247)
This commit is contained in:
+5
-3
@@ -109,7 +109,7 @@ The named pipe is created with `maxNumberOfServerInstances: 1` so a second worke
|
||||
|
||||
### SessionShutdownHostedService
|
||||
|
||||
`SessionShutdownHostedService` is an `IHostedService` whose only job is to call `ISessionManager.ShutdownAsync` from `StopAsync`. It catches `OperationCanceledException` triggered by the host shutdown timeout and logs a warning so that an over-running shutdown does not surface as an unhandled exception.
|
||||
`SessionShutdownHostedService` is an `IHostedService` whose only job is to call `ISessionManager.ShutdownAsync` from `StopAsync`. The drain itself has normally already run by then: it starts at `ApplicationStopping` (see [Shutdown Coordination](#shutdown-coordination)), and `ShutdownAsync` awaits that same drain. It catches `OperationCanceledException` triggered by the host shutdown timeout and logs a warning so that an over-running shutdown does not surface as an unhandled exception.
|
||||
|
||||
### SessionOpenRequest
|
||||
|
||||
@@ -354,7 +354,9 @@ Only if both the graceful shutdown and the kill fall-back fail are the original
|
||||
|
||||
## Shutdown Coordination
|
||||
|
||||
`SessionShutdownHostedService.StopAsync` calls `SessionManager.ShutdownAsync`, which closes every registered session with `GatewayShutdownReason`. Sessions are drained with the same bounded fan-out the lease sweep uses (`MaxParallelSessionCloses`), because a one-at-a-time drain of a full registry at a worst-case worker shutdown timeout each outruns any host stop-timeout and leaves the tail to the orphan killer. Each iteration catches its own exceptions — *every* exception, including from the fallback — calls `KillWorkerAsync` on an uncancellable token, and removes the session, so that neither one stuck worker nor one failing teardown can block or abort the rest of the host's drain:
|
||||
The drain starts at the host's `IHostApplicationLifetime.ApplicationStopping` signal, not at `SessionShutdownHostedService.StopAsync`. `WebApplication` registers Kestrel's hosted service last, so it stops first, and Kestrel's stop waits for in-flight requests. An open `StreamEvents` call is an in-flight request that ends only when its session closes. When the drain ran from `StopAsync`, every stop with an active stream waited out the whole host shutdown timeout (30 s by default); Kestrel then aborted the streams, and the drain ran on an already-cancelled token, so every worker fell to the kill fallback with no MXAccess cleanup. Starting at `ApplicationStopping`, the drain closes the sessions while Kestrel is still draining: each close completes the subscriber channels, the streams end with `OK`, and Kestrel's drain finishes at once. The manager starts the drain exactly once (`BeginShutdown`), and `ShutdownAsync` — called by `SessionShutdownHostedService.StopAsync`, or directly by a host without a lifetime — awaits that same task. The stop token passed to `ShutdownAsync` cancels the graceful closes still in flight, which turns them into the kill fallback; it never abandons the drain.
|
||||
|
||||
The drain closes every registered session with `GatewayShutdownReason`. Sessions are drained with the same bounded fan-out the lease sweep uses (`MaxParallelSessionCloses`), because a one-at-a-time drain of a full registry at a worst-case worker shutdown timeout each outruns any host stop-timeout and leaves the tail to the orphan killer. Each iteration catches its own exceptions — *every* exception, including from the fallback — calls `KillWorkerAsync` on an uncancellable token, and removes the session, so that neither one stuck worker nor one failing teardown can block or abort the rest of the host's drain:
|
||||
|
||||
```csharp
|
||||
MarkStopping();
|
||||
@@ -422,7 +424,7 @@ public static IServiceCollection AddGatewaySessions(this IServiceCollection serv
|
||||
}
|
||||
```
|
||||
|
||||
The registry must be a singleton because its `ConcurrentDictionary` is the source of truth for session state across the gRPC service, the lease sweeper, the dashboard, and the shutdown hosted service. Registering `SessionShutdownHostedService` last ensures it is constructed after `ISessionManager` and therefore drains sessions during host stop.
|
||||
The registry must be a singleton because its `ConcurrentDictionary` is the source of truth for session state across the gRPC service, the lease sweeper, the dashboard, and the shutdown hosted service. Registering `SessionShutdownHostedService` last ensures it is constructed after `ISessionManager` and therefore awaits the session drain during host stop. Constructing `SessionManager` is also what subscribes the drain to `ApplicationStopping`.
|
||||
|
||||
## Related Documentation
|
||||
|
||||
|
||||
Reference in New Issue
Block a user