Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions Zend/tests/fibers/gc-cycle-callback.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_fibers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading