[php-src] Issue #25380: Stateless closure cache: unbounded memory growth when the declaring closure has a heap runtime cache (bind/call/traits)

From: Date: Tue, 10 Nov 2026 22:31:31 +0000
Subject: [php-src] Issue #25380: Stateless closure cache: unbounded memory growth when the declaring closure has a heap runtime cache (bind/call/traits)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-254117@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/25380 Author: jorgsowa ### Description ZEND_DECLARE_LAMBDA_FUNCTION caches a stateless (static fn) closure in the declaring function's runtime cache and pushes it onto EG(lambda_cache), which is only released at shutdown. When the declaring function is a closure that gets a fresh ZEND_ACC_HEAP_RT_CACHE on every creation/call (rebinding to another scope via Closure::bind()/bindTo(), Closure::call(), or a closure in a trait method used by more than one class), every call misses the cache and pushes another closure, so memory grows linearly for the whole request. This hurts long-running workers (e.g. Macroable-style Closure::bind($macro, null, static::class)). The following code: ```php <?php class A {} $outer = function () { return (static fn() => 1)(); }; $m = memory_get_usage(); for ($i = 0; $i < 100000; $i++) { Closure::bind($outer, null, A::class)(); } echo memory_get_usage() - $m, "\n"; ``` Resulted in this output: ``` 40243200 ``` But I expected this output instead (as in PHP 8.5.12): ``` 0 ``` Trait variant, no explicit binding: ```php <?php trait T { public function m($k) { $f = function () use ($k) { return (static fn($x) => $x + 1)($k); }; return $f(); } } class U1 { use T; } class U2 { use T; } (new U1)->m(0); $u2 = new U2; $m = memory_get_usage(); for ($i = 0; $i < 100000; $i++) { $u2->m($i); } echo memory_get_usage() - $m, "\n"; ``` 8.6: 41843072, 8.5.12: 0. $outer->bindTo(null, A::class)() and $outer->call($obj) grow the same way. The debug build reports no leak because EG(lambda_cache) is cleaned at shutdown. Introduced by f47cd216e95 (stateless closure cache). Reproduces with opcache off, on, and with tracing JIT. ### PHP Version ```plain PHP 8.6.1-dev (cli) (NTS DEBUG), PHP-8.6 @ dfc3219f341 ``` ### Operating System macOS (arm64)

« previous php.bugs (#254117) next »