diff --git a/NEWS b/NEWS index 7dc75833bd9a..a8f177137e2e 100644 --- a/NEWS +++ b/NEWS @@ -36,6 +36,8 @@ PHP NEWS . Fixed AST export of by-reference property hooks. (Jorg Sowa) . Fixed AST export of property hook parameter lists and attributes. (Jorg Sowa) + . Fixed bug GH-25540 (Objects holding a suspended Fiber with a callback bound + to the same object are not collected). (akasakariko) . Fixed object_properties_load() adding integer keys to property tables, which made the properties unreadable and clone hit an assertion. (Nicolas Grekas) diff --git a/Zend/tests/fibers/gc-cycle-callback.phpt b/Zend/tests/fibers/gc-cycle-callback.phpt index 76ba9d7b9ca0..8ad7bcce6f30 100644 --- a/Zend/tests/fibers/gc-cycle-callback.phpt +++ b/Zend/tests/fibers/gc-cycle-callback.phpt @@ -24,8 +24,25 @@ var_dump('COLLECT CYCLES'); gc_collect_cycles(); var_dump('DONE'); +$owner = new class { + public Fiber $fiber; + + public function __construct() { + $this->fiber = new Fiber(function () { + Fiber::suspend(); + return $this; + }); + } +}; +$owner->fiber->start(); +$weak = WeakReference::create($owner); +unset($owner); +gc_collect_cycles(); +var_dump($weak->get() === null); + ?> --EXPECT-- string(14) "COLLECT CYCLES" string(4) "DTOR" string(4) "DONE" +bool(true) diff --git a/Zend/zend_API.h b/Zend/zend_API.h index ba67948c12b3..9a4d43c3d872 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -906,6 +906,9 @@ static zend_always_inline void zend_get_gc_buffer_add_fcc(zend_get_gc_buffer *gc * If the call or call setup throws, EG(exception) will be set and the retval * will be UNDEF. Otherwise, the retval will be a non-UNDEF value. */ ZEND_API zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache); +/* The caller owns a reference to pinned_object until the call returns. */ +zend_result zend_call_function_with_pinned_object( + zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_object *pinned_object); /* Call the FCI/FCC pair while setting the call return value to the passed zval*. */ static zend_always_inline zend_result zend_call_function_with_return_value( diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index aa6b4db7f3b0..0a2d0933375d 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -833,6 +833,13 @@ zend_result _call_user_function_impl(zval *object, zval *function_name, zval *re /* }}} */ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) /* {{{ */ +{ + return zend_call_function_with_pinned_object(fci, fci_cache, NULL); +} +/* }}} */ + +zend_result zend_call_function_with_pinned_object( + zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_object *pinned_object) /* {{{ */ { zend_execute_data *call; zend_fcall_info_cache fci_cache_local; @@ -895,8 +902,10 @@ zend_result zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_ } else { object_or_called_scope = fci_cache->object; call_info = ZEND_CALL_TOP_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_HAS_THIS; - pinned_this = fci_cache->object; - GC_ADDREF(pinned_this); + if (fci_cache->object != pinned_object) { + pinned_this = fci_cache->object; + GC_ADDREF(pinned_this); + } } if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_DEPRECATED)) { diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c index be797008c520..e325860fa537 100644 --- a/Zend/zend_fibers.c +++ b/Zend/zend_fibers.c @@ -635,7 +635,9 @@ static ZEND_STACK_ALIGNED void zend_fiber_execute(zend_fiber_transfer *transfer) fiber->fci.retval = &fiber->result; - zend_call_function(&fiber->fci, &fiber->fci_cache); + /* The fiber already owns and reports fci.object to the GC. A second + * reference on the C stack would hide the cycle while suspended. */ + zend_call_function_with_pinned_object(&fiber->fci, &fiber->fci_cache, fiber->fci.object); /* Cleanup callback and unset field to prevent GC / duplicate dtor issues. */ zend_fiber_release_callable(fiber);