[SES-001] Host stop waits 30 s on open streams then kills every worker instead of closing gracefully #247

Closed
opened 2026-12-02 16:07:35 -05:00 by dohertj2 · 1 comment
Owner

SES-001: Host stop waits 30 s on open streams, then kills every worker

  • Severity: High
  • Category: Bug
  • Confidence: Confirmed (host ordering reproduced on .NET 10 with a minimal app; the gateway consequence follows from the code)
  • Locations:
    • Sessions/SessionServiceCollectionExtensions.cs (AddHostedService<SessionShutdownHostedService>)
    • Sessions/SessionShutdownHostedService.cs:29-39
    • Sessions/SessionManager.cs:113 (the ApplicationStopping hook only sets a flag), :498-582
    • Sessions/GatewaySession.cs:1867 (_closeLock.WaitAsync(cancellationToken))
    • Program.cs (app.Run()), GatewayApplication.cs:41-57
    • docs/Grpc.md:327, docs/Sessions.md:397 (describe the opposite order)

Description

The session drain runs from SessionShutdownHostedService.StopAsync. With WebApplication, Kestrel's own hosted service is registered last, so it stops first. Kestrel's stop waits for in-flight requests, and an open StreamEvents call is an in-flight request that only ends when its session closes. Nothing ends it at ApplicationStopping: SessionManager.MarkStopping only blocks new opens.

So the sequence on any stop with at least one active stream is:

  1. Kestrel waits the full host shutdown timeout (30 s by default; the repo sets no HostOptions.ShutdownTimeout).
  2. Kestrel aborts the connections. The streams end with a transport abort, not the clean OK that docs/Grpc.md:327 describes.
  3. SessionShutdownHostedService.StopAsync is then called with a token that is already cancelled.
  4. ShutdownAsync passes that token to CloseSessionCoreAsync. GatewaySession.CloseAsync throws at _closeLock.WaitAsync(cancellationToken) before the worker is asked to shut down.
  5. Every session falls to the kill fallback (SessionManager.cs:548-570), logging one "Graceful shutdown failed" warning per session.

Evidence

A minimal .NET 10 WebApplication with one IHostedService and one open streaming request printed:

ApplicationStopping
stream ABORTED at 30.0s
Drain.StopAsync at 30.0s tokenCancelled=True

With no open request, the same hosted service's StopAsync ran at 0.0 s with a live token.

Failure scenario

OtOpcUa or ScadaBridge holds a StreamEvents stream (the normal production state). An operator stops the MxAccessGw service for a deploy. The stop hangs 30 s, every x86 worker is killed with no MXAccess Unregister/UnAdvise cleanup, and clients see an aborted stream. If the service wrapper's own stop timeout is shorter than 30 s, the gateway is terminated first and the workers are left to the orphan terminator on the next start.

Suggested fix

Start the drain from IHostApplicationLifetime.ApplicationStopping (for example, have MarkStopping kick off ShutdownAsync once and let SessionShutdownHostedService.StopAsync await that same task). Closing the sessions completes the subscriber channels, so the streams end with OK and Kestrel drains at once. Then correct docs/Grpc.md and docs/Sessions.md.

Test idea

A Kestrel-backed (not TestServer) integration test: open a session on the fake worker, start StreamEvents, call StopAsync on the host. Assert the stop finishes well under the shutdown timeout, the stream ends with OK, and the fake worker saw a graceful shutdown (ShutdownCount == 1, KillCount == 0).


Source: code-reviews/2026-12-02-perf-bugs/README.md and code-reviews/2026-12-02-perf-bugs/PLAN.md (commit f200bda6 on local branch docs/review-2026-12-02-perf-bugs, not yet pushed). Reviewed at 54367e74.

## SES-001: Host stop waits 30 s on open streams, then kills every worker - **Severity:** High - **Category:** Bug - **Confidence:** Confirmed (host ordering reproduced on .NET 10 with a minimal app; the gateway consequence follows from the code) - **Locations:** - `Sessions/SessionServiceCollectionExtensions.cs` (`AddHostedService<SessionShutdownHostedService>`) - `Sessions/SessionShutdownHostedService.cs:29-39` - `Sessions/SessionManager.cs:113` (the `ApplicationStopping` hook only sets a flag), `:498-582` - `Sessions/GatewaySession.cs:1867` (`_closeLock.WaitAsync(cancellationToken)`) - `Program.cs` (`app.Run()`), `GatewayApplication.cs:41-57` - `docs/Grpc.md:327`, `docs/Sessions.md:397` (describe the opposite order) ### Description The session drain runs from `SessionShutdownHostedService.StopAsync`. With `WebApplication`, Kestrel's own hosted service is registered last, so it stops first. Kestrel's stop waits for in-flight requests, and an open `StreamEvents` call is an in-flight request that only ends when its session closes. Nothing ends it at `ApplicationStopping`: `SessionManager.MarkStopping` only blocks new opens. So the sequence on any stop with at least one active stream is: 1. Kestrel waits the full host shutdown timeout (30 s by default; the repo sets no `HostOptions.ShutdownTimeout`). 2. Kestrel aborts the connections. The streams end with a transport abort, not the clean `OK` that `docs/Grpc.md:327` describes. 3. `SessionShutdownHostedService.StopAsync` is then called with a token that is already cancelled. 4. `ShutdownAsync` passes that token to `CloseSessionCoreAsync`. `GatewaySession.CloseAsync` throws at `_closeLock.WaitAsync(cancellationToken)` before the worker is asked to shut down. 5. Every session falls to the kill fallback (`SessionManager.cs:548-570`), logging one "Graceful shutdown failed" warning per session. ### Evidence A minimal .NET 10 `WebApplication` with one `IHostedService` and one open streaming request printed: ``` ApplicationStopping stream ABORTED at 30.0s Drain.StopAsync at 30.0s tokenCancelled=True ``` With no open request, the same hosted service's `StopAsync` ran at 0.0 s with a live token. ### Failure scenario OtOpcUa or ScadaBridge holds a `StreamEvents` stream (the normal production state). An operator stops the `MxAccessGw` service for a deploy. The stop hangs 30 s, every x86 worker is killed with no MXAccess `Unregister`/`UnAdvise` cleanup, and clients see an aborted stream. If the service wrapper's own stop timeout is shorter than 30 s, the gateway is terminated first and the workers are left to the orphan terminator on the next start. ### Suggested fix Start the drain from `IHostApplicationLifetime.ApplicationStopping` (for example, have `MarkStopping` kick off `ShutdownAsync` once and let `SessionShutdownHostedService.StopAsync` await that same task). Closing the sessions completes the subscriber channels, so the streams end with `OK` and Kestrel drains at once. Then correct `docs/Grpc.md` and `docs/Sessions.md`. ### Test idea A Kestrel-backed (not `TestServer`) integration test: open a session on the fake worker, start `StreamEvents`, call `StopAsync` on the host. Assert the stop finishes well under the shutdown timeout, the stream ends with `OK`, and the fake worker saw a graceful shutdown (`ShutdownCount == 1`, `KillCount == 0`). --- Source: `code-reviews/2026-12-02-perf-bugs/README.md` and `code-reviews/2026-12-02-perf-bugs/PLAN.md` (commit `f200bda6` on local branch `docs/review-2026-12-02-perf-bugs`, not yet pushed). Reviewed at `54367e74`.
dohertj2 added the area:gatewaypriority:p1type:bugtype:review-finding labels 2026-12-02 16:07:35 -05:00
Author
Owner

Merged to main at 68043411 (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA.

Session drain starts on ApplicationStopping; workers close gracefully, streams end OK; kill stays as a bounded fallback.

Merged to `main` at `68043411` (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA. Session drain starts on `ApplicationStopping`; workers close gracefully, streams end OK; kill stays as a bounded fallback.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#247