fix(container): detect nested circular dependencies - #2297
Open
osbre wants to merge 1 commit into
Open
Conversation
Benchmark ResultsComparison of Open to see the benchmark results
Generated by phpbench against commit 382115f |
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.
The container could segfault (SIGSEGV, exit 139) instead of throwing
CircularDependencyEncounteredwhen a circular dependency was separated by a nested$container->get()call.The main issue was that
DependencyChainwas being used both to track dependencies currently being resolved and to retain everything seen for error rendering. Those two use cases have different lifecycles, which caused both missed cycle detection and false positives.Reproduction
Without the
Unrelatedresolution, the cycle is detected correctly, which is why the existing tests didn't catch this case.Root cause
There were two separate issues:
Nested
get()calls reset the dependency chain.stopChain()unconditionally set$this->chain = null, so an innerget(Unrelated::class)cleared the ancestry of the outer resolution. The outer resolution then started with a fresh chain and eventually recursed until the stack overflowed.Regular constructor autowiring doesn't hit this because it goes through
resolve()rather thanget(), which is why the usualA → B → Acase already worked.Dependencies were never removed from the chain.
add()effectively treated "already in the chain" as "circular", even when that dependency had already finished resolving.Once the first issue was fixed, this exposed a false positive for
ConsoleArgumentBag: the same already-resolved singleton could legitimately be reached twice on one path - once directly byConsoleApplicationInitializerand once throughExecuteConsoleCommand::__construct.Fix
The dependency chain is now treated as a properly balanced stack.
DependencyChain::pop(), with everyadd()now paired with afinallyblock.get()andinvoke*()restore the caller's chain instead of clearing it, so nested resolutions keep the parent's ancestry.add()on already-resolved singletons. A singleton only entersresolvedSingletonsafter construction has completed, so it cannot be part of an in-flight cycle.Tempest's existing per-parameter
$this->clone()already keeps dependency branches isolated, so diamond dependencies continue to work correctly. Because each chain is branch-local and pushes are strictly nested,pop()can safely use a simplearray_pop().Performance
Removing entries by dependency name would cost roughly 5-6%, since it has to reconstruct a
Dependencyand redo reflection work. Relying on the LIFO property of the chain avoids that, and dropping the unnecessary singleton push keeps the whole change at parity:The singleton speedup comes from removing the dead bookkeeping that was already happening before this PR.
Tests
Added two regression tests to
CircularDependencyExceptionTest:circular_dependency_after_a_nested_resolution_test- covers the nested-resolution segfault and now correctly throwsCircularDependencyEncountered.the_same_dependency_resolved_twice_in_one_chain_is_not_circular_test- verifies that one chain resolving the same dependency twice isn't treated as a cycle. It resolves from a single initializer on purpose: constructor parameters each get their own clone of the chain, so they can't catch a missingpop().Notes
pop()runs from afinally, including when resolution fails. This is safe currently because exception messages are rendered in the exception constructor, before the dependency chain starts unwinding.If we ever move to lazy message construction, this coupling could cause the rendered chain to lose entries. I'm happy to add a debug-mode assertion that the popped entry is the one we expect, if you'd prefer that extra guard.