Skip to content

Dispose container when an IStartable throws during Build (#1392)#1485

Merged
tillig merged 2 commits into
developfrom
feature/issue-1392
Jun 15, 2026
Merged

Dispose container when an IStartable throws during Build (#1392)#1485
tillig merged 2 commits into
developfrom
feature/issue-1392

Conversation

@tillig

@tillig tillig commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #1392. When an IStartable.Start() threw during ContainerBuilder.Build(), the partially-initialized container was abandoned without being disposed. Any IDisposable services instantiated to satisfy the startable (and its dependencies) leaked, because the root lifetime scope's Disposer was tracking them but Dispose() was never called.

Fix

In ContainerBuilder.Build(), the block that starts startable components and runs build callbacks is now wrapped in a try/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 both IStartable.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 an IStartable whose Start() throws, asserts Build() throws, and asserts the dependency was disposed.

No public API changes. Zero warnings/errors; full Autofac.Test and Autofac.Specification.Test suites pass on net8.0 and net10.0.

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

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.79%. Comparing base (f8d4095) to head (4440aa6).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instantiated services not disposed on exception during IStartable

1 participant