Skip to content

[13.x] Fix once() returning a cached value for a different object - #62121

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:once-object-id-reuse
Oct 22, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:once-object-id-reuse

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

once() builds its cache key from the variables the closure captures, and objects are keyed by spl_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:

class Billing
{
    public function discountFor(User $user)
    {
        return once(fn () => $this->calculateDiscount($user));
    }
}

foreach (User::lazy() as $user) {
    $billing->discountFor($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 of 1.

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 HasOnceHash and closures that only use $this or scalars work the same as before.

@vasikmansuri vasikmansuri left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this locally (PHP 8.4.15):

  • testIsNotMemoizedWhenObjectIdIsReused fails on current 13.x (5c0486f): from the 5th iteration on, users get another user's cached name. With this PR, all of OnceTest passes.
  • The identity check matches the hash: objects() covers the same top-level captured objects that hashFromTrace() keys by spl_object_id(), and skips HasOnceHash objects. Objects nested inside arrays go through serialize(), 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.

@taylorotwell
taylorotwell merged commit 1de9acf into laravel:13.x Oct 22, 2026
62 checks passed
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.
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.

3 participants