Skip to content

Fix disposable transient dependencies being disposed twice on container dispose (#600) - #679

Open
haraldrohan wants to merge 1 commit into
castleproject:masterfrom
haraldrohan:fix-600-double-dispose-on-container-dispose
Open

haraldrohan wants to merge 1 commit into
castleproject:masterfrom
haraldrohan:fix-600-double-dispose-on-container-dispose

Conversation

@haraldrohan

Copy link
Copy Markdown

Fixes #600

Problem

When a container is disposed, a disposable transient component that was resolved as a dependency of another transient component gets Dispose() called twice. This is independent of UsingFactoryMethod (the issue's repro uses it, but a plain registration behaves the same); it only needs a transient root with a disposable transient dependency.

Cause

LifecycledComponentsReleasePolicy.Dispose() takes a snapshot of all tracked burdens and releases each one. The root and its dependency are both tracked, and releasing the root's burden also releases the burdens of its dependencies. The dependency is then released a second time through its own entry in the snapshot.

An explicit container.Release(root) is not affected, because the dependency is removed from the policy's tracking when it is released as part of the root's graph.

Fix

While disposing, remember which burdens were already released as part of another burden's graph and skip them. The dictionary is still cleared up front and the tracked-instances counter is decremented as before, so nothing else changes.

Tests

Added to LifecycledComponentsReleasePolicyComponentTrackingTestCase:

  • disposable transient dependency of a transient root is disposed once on container dispose (failed before with 2 instead of 1)
  • same with UsingFactoryMethod, matching the issue (failed before with 2 instead of 1)
  • same dependency is disposed once when the root is released explicitly (already passed, guards existing behaviour)

Castle.Windsor.Tests (net6.0): 1253 passed, 0 failed. Castle.Windsor.Extensions.DependencyInjection.Tests (net6.0): 171 passed. I did not run the net462 target.

Added a CHANGELOG.md entry under a new Unreleased section.

The fix relies on a dependency being tracked before its parent, which always holds today and which Dispose() already assumes for its release order.

🤖 Generated with Claude Code

…er dispose (castleproject#600)

LifecycledComponentsReleasePolicy.Dispose released every tracked burden. A
transient root and its transient dependencies are all tracked, and releasing
the root also releases the burdens of its dependencies, so those dependencies
were released a second time through their own entry.

Skip burdens that were already released as part of another burden's graph.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

Dispose is called twice when using UsingFactoryMethod with LifestyleTransient

1 participant