[SES-001] Host stop waits 30 s on open streams then kills every worker instead of closing gracefully #247
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
SES-001: Host stop waits 30 s on open streams, then kills every worker
Sessions/SessionServiceCollectionExtensions.cs(AddHostedService<SessionShutdownHostedService>)Sessions/SessionShutdownHostedService.cs:29-39Sessions/SessionManager.cs:113(theApplicationStoppinghook only sets a flag),:498-582Sessions/GatewaySession.cs:1867(_closeLock.WaitAsync(cancellationToken))Program.cs(app.Run()),GatewayApplication.cs:41-57docs/Grpc.md:327,docs/Sessions.md:397(describe the opposite order)Description
The session drain runs from
SessionShutdownHostedService.StopAsync. WithWebApplication, Kestrel's own hosted service is registered last, so it stops first. Kestrel's stop waits for in-flight requests, and an openStreamEventscall is an in-flight request that only ends when its session closes. Nothing ends it atApplicationStopping:SessionManager.MarkStoppingonly blocks new opens.So the sequence on any stop with at least one active stream is:
HostOptions.ShutdownTimeout).OKthatdocs/Grpc.md:327describes.SessionShutdownHostedService.StopAsyncis then called with a token that is already cancelled.ShutdownAsyncpasses that token toCloseSessionCoreAsync.GatewaySession.CloseAsyncthrows at_closeLock.WaitAsync(cancellationToken)before the worker is asked to shut down.SessionManager.cs:548-570), logging one "Graceful shutdown failed" warning per session.Evidence
A minimal .NET 10
WebApplicationwith oneIHostedServiceand one open streaming request printed:With no open request, the same hosted service's
StopAsyncran at 0.0 s with a live token.Failure scenario
OtOpcUa or ScadaBridge holds a
StreamEventsstream (the normal production state). An operator stops theMxAccessGwservice for a deploy. The stop hangs 30 s, every x86 worker is killed with no MXAccessUnregister/UnAdvisecleanup, 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, haveMarkStoppingkick offShutdownAsynconce and letSessionShutdownHostedService.StopAsyncawait that same task). Closing the sessions completes the subscriber channels, so the streams end withOKand Kestrel drains at once. Then correctdocs/Grpc.mdanddocs/Sessions.md.Test idea
A Kestrel-backed (not
TestServer) integration test: open a session on the fake worker, startStreamEvents, callStopAsyncon the host. Assert the stop finishes well under the shutdown timeout, the stream ends withOK, and the fake worker saw a graceful shutdown (ShutdownCount == 1,KillCount == 0).Source:
code-reviews/2026-12-02-perf-bugs/README.mdandcode-reviews/2026-12-02-perf-bugs/PLAN.md(commitf200bda6on local branchdocs/review-2026-12-02-perf-bugs, not yet pushed). Reviewed at54367e74.Merged to
mainat68043411(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.