Repository navigation
[13.x] Fix once() returning a cached value for a different object - #62121
Merged
Merged
Conversation
vasikmansuri
left a comment
There was a problem hiding this comment.
Tested this locally (PHP 8.4.15):
testIsNotMemoizedWhenObjectIdIsReusedfails on current13.x(5c0486f): from the 5th iteration on, users get another user's cached name. With this PR, all ofOnceTestpasses.- The identity check matches the hash:
objects()covers the same top-level captured objects thathashFromTrace()keys byspl_object_id(), and skipsHasOnceHashobjects. Objects nested inside arrays go throughserialize(), so they aren't affected by id reuse.
One data point on cost. In a micro-benchmark of a cached once(fn () => $user->name) hit (200k calls, 3 runs each), a hit takes ~1.12 µs with this PR versus ~0.83 µs on 13.x, about +0.3 µs per call. I tried building objects() once in tryFromTrace() to avoid the extra ReflectionClosure, and it made no measurable difference, so most of the cost is in the WeakReference round trip and the comparison. That seems a fair price for not silently returning another object's value, but I'm mentioning it since once() is often called in hot paths.
albertcht
pushed a commit
to hypervel/support
that referenced
this pull request
Nov 1, 2026
Port laravel/framework#62121 from 1de9acf2054cb360659200135e5c246320c7f330 at 13.x target 5d7797528723107cd4f495326838a7d7d67fbb20. Adopt upstream weak-reference validation while retaining coroutine-local caches, typed object identity and top-level trace support. Reuse the existing closure reflection and remove obsolete worker-global object tokens and their cleanup registration. Validation: OnceTest passes (27 tests, 43 assertions, one existing skip); related helper/model/cleanup tests pass (361 tests, 1366 assertions), as do recursion and model coroutine tests (26 tests, 147 assertions). Full source/type analysis and formatting pass.
albertcht
pushed a commit
to hypervel/testing
that referenced
this pull request
Nov 1, 2026
Port laravel/framework#62121 from 1de9acf2054cb360659200135e5c246320c7f330 at 13.x target 5d7797528723107cd4f495326838a7d7d67fbb20. Adopt upstream weak-reference validation while retaining coroutine-local caches, typed object identity and top-level trace support. Reuse the existing closure reflection and remove obsolete worker-global object tokens and their cleanup registration. Validation: OnceTest passes (27 tests, 43 assertions, one existing skip); related helper/model/cleanup tests pass (361 tests, 1366 assertions), as do recursion and model coroutine tests (26 tests, 147 assertions). Full source/type analysis and formatting pass.
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.
once()builds its cache key from the variables the closure captures, and objects are keyed byspl_object_id(). PHP reuses an object id as soon as the object is freed, so a new object can get the cached result of an old one.For example, a service that caches a value per user:
Each previous user is freed during the loop and a later one gets its id, so user 3 gets the discount of user 1, user 4 gets the one of user 2, and so on. Nothing fails, it just returns the wrong value. The value object example from #56009 behaves the same way:
getTagFromId(new PositiveInteger(2))returns the tag of1.This PR keeps a weak reference to the captured objects next to the cached value, and only returns the cached value when they're still the same objects. Objects that implement
HasOnceHashand closures that only use$thisor scalars work the same as before.