Dispose container when an IStartable throws during Build (#1392)#1485
Merged
Conversation
If StartStartableComponents or RunBuildCallbacks threw during Build(), the newly created Container was abandoned without Dispose() being called. Any IDisposable instances that had been resolved into the root lifetime scope to satisfy those operations were then leaked. Wrap both calls in a try/catch that disposes the container and rethrows on the failure path; on the success path the caller owns the container as before. Also add a regression test that registers a DisposableDependency as a transitive dependency of a throwing IStartable, asserts Build() throws, and asserts the dependency was disposed.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1485 +/- ##
===========================================
+ Coverage 77.69% 77.79% +0.10%
===========================================
Files 217 217
Lines 5829 5833 +4
Branches 1253 1253
===========================================
+ Hits 4529 4538 +9
+ Misses 764 759 -5
Partials 536 536 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1392. When an
IStartable.Start()threw duringContainerBuilder.Build(), the partially-initialized container was abandoned without being disposed. AnyIDisposableservices instantiated to satisfy the startable (and its dependencies) leaked, because the root lifetime scope'sDisposerwas tracking them butDispose()was never called.Fix
In
ContainerBuilder.Build(), the block that starts startable components and runs build callbacks is now wrapped in atry/catch. On exception, the container is disposed before the exception propagates, then rethrown. On the success path the caller still owns the container as before. The fix covers bothIStartable.Start()and build-callback failures, since both run in the window where the container is owned but not yet returned.Tests
Adds
Startable_WhenStartableThrows_DependenciesAreDisposed: registers a disposable dependency plus anIStartablewhoseStart()throws, assertsBuild()throws, and asserts the dependency was disposed.No public API changes. Zero warnings/errors; full
Autofac.TestandAutofac.Specification.Testsuites pass on net8.0 and net10.0.