Skip to content

Fix GH-25380: Do not cache stateless closures with heap runtime cache - #25545

Open
prateekbhujel wants to merge 1 commit into
php:PHP-8.6from
prateekbhujel:fix/gh25380-stateless-closure-heap-rt-cache
Open

prateekbhujel wants to merge 1 commit into
php:PHP-8.6from
prateekbhujel:fix/gh25380-stateless-closure-heap-rt-cache

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

Closes GH-25380

ZEND_DECLARE_LAMBDA_FUNCTION caches stateless closures in EX(run_time_cache) via CACHE_POLYMORPHIC_PTR and pushes a reference onto EG(lambda_cache), which is only released at request shutdown.

When the declaring function (EX(func)) has ZEND_ACC_HEAP_RT_CACHE (via Closure::bind()/bindTo() with a scope change, Closure::call(), or closures inside trait methods reused across multiple classes), its runtime cache is efree()d when the outer closure or call() finishes. Subsequent calls allocate a fresh zeroed runtime cache, miss CACHED_POLYMORPHIC_PTR, and push another unreachable closure onto EG(lambda_cache) for the rest of the request.

Skip stateless closure caching when EX(func)->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE is set. Because the check sits on the cache-miss write path (and heap runtime caches are zero-initialized), normal arena-cached ZEND_DECLARE_LAMBDA_FUNCTION cache hits pay no extra cost.

Checked sibling caches as well: ZEND_CALLABLE_CONVERT (EG(callable_convert_cache)) and ZEND_CALLABLE_CONVERT_PARTIAL (EG(partial_function_application_cache)) deduplicate in request HashTables and already stay bounded across repeated Closure::bind() calls.

Verification

  • Zend/tests/closures/: 139 passed, 1 skipped (zend_test)
  • Zend/tests/closures/gh25380.phpt: passed with both opcache.enable_cli=0 and opcache.enable_cli=1
  • ext/opcache/tests/: 1027 passed, 46 skipped, 2 expected fail
  • Zend/tests/: 5562 passed, 178 skipped, 1 expected fail (new_oom.phpt only failed locally due to an unquoted space in the local workspace directory path)

ZEND_DECLARE_LAMBDA_FUNCTION caches closures in the declaring op_array's
runtime cache and pushes a reference onto EG(lambda_cache), which is only
released at request shutdown. When the declaring function has
ZEND_ACC_HEAP_RT_CACHE (Closure::bind/bindTo with scope changes,
Closure::call, or closures inside trait methods reused across classes),
its runtime cache is freed with the closure or call, leaving unreachable
closures accumulating in EG(lambda_cache) for the rest of the request.

Skip stateless closure caching when EX(func) uses ZEND_ACC_HEAP_RT_CACHE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant