[13.x] Prevent duplicate scoped instance registrations - #61251
Merged
taylorotwell merged 1 commit intoAug 20, 2026
Merged
taylorotwell merged 1 commit into
taylorotwell merged 1 commit into
Conversation
`Container::scoped()` appends to `$scopedInstances` unconditionally, so an abstract registered more than once is recorded more than once and every later `forgetScopedInstances()` walks the duplicates. In a long-lived process this is unbounded. Removing a scoped binding and letting it be registered again is the ordinary way to rebuild one, because `Container::offsetUnset()` clears `bindings`, `instances` and `resolved` but not `scopedInstances`, so `scopedIf()` sees the binding gone and re-registers through `scoped()`. Each cycle adds another entry that is never removed. `Container::resolve()` already guards the equivalent append for the `Scoped` attribute, added in laravel#56334. This applies the same guard on the main path. Deduplicating is behaviour-preserving: `forgetScopedInstances()` unsets an instance by abstract, so unsetting the same abstract twice was already a no-op.
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.
Container::scoped()appends to$scopedInstancesunconditionally, so registering the same abstract more than once records it more than once.scopedIf()avoids this while the abstract stays bound, but not once the binding is removed:offsetUnset()clearsbindings,instancesandresolvedand leavesscopedInstancesalone, soscopedIf()sees the binding as gone and re-registers throughscoped(). Anywhere that happens repeatedly in a long-lived process, the array grows without bound and everyforgetScopedInstances()walks the duplicates.resolve()already guards the equivalent append for the#[Scoped]attribute, added in #56334. This applies the same guard toscoped(). Behaviour is unchanged, sinceforgetScopedInstances()unsets an instance by abstract and unsetting the same one twice was already a no-op.