Skip to content
Draft
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
42 changes: 42 additions & 0 deletions Zend/tests/fibers/gc-generator-in-suspended-fiber.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
GC collects a cycle through a generator parked in Fiber::suspend() on a fiber stack
--FILE--
<?php

class C {
public function __destruct() {
echo __METHOD__, "\n";
}
}

function gen() {
$c = new C();
$self = Fiber::getCurrent(); // cycle: fiber -> frames -> $self -> fiber
yield 1;
Fiber::suspend("from gen");
yield 2;
}

$fiber = new Fiber(function () {
$g = gen();
$g->current();
$g->next(); // parks in Fiber::suspend() inside the generator
echo "unreachable\n";
});

$fiber->start();
print "1\n";

// Still referenced: nothing to collect.
gc_collect_cycles();
print "2\n";

$fiber = null;
gc_collect_cycles();
print "3\n";
?>
--EXPECT--
1
2
C::__destruct
3
9 changes: 8 additions & 1 deletion Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "zend_API.h"
#include "zend_exceptions.h"
#include "zend_builtin_functions.h"
#include "zend_async_API.h"
#include "zend_ini.h"
#include "zend_vm.h"
#include "zend_dtrace.h"
Expand Down Expand Up @@ -831,6 +832,7 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
executor_globals->current_fiber_context = NULL;
executor_globals->main_fiber_context = NULL;
executor_globals->active_fiber = NULL;
memset(&executor_globals->shutdown_context, 0, sizeof(executor_globals->shutdown_context));
#ifdef ZEND_WIN32
zend_get_windows_version_info(&executor_globals->windows_version_info);
#endif
Expand Down Expand Up @@ -1979,8 +1981,13 @@ ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handl
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
zend_user_exception_handler();
}

if (EG(exception)) {
ret = zend_exception_error(EG(exception), E_ERROR);
if (ZEND_ASYNC_CURRENT_COROUTINE == NULL) {
ret = zend_exception_error(EG(exception), E_ERROR);
} else {
ret = FAILURE;
}
}
}
zend_destroy_static_vars(op_array);
Expand Down
Loading