Skip to content

Fix assertion failure in generator dtor #16025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
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
53 changes: 53 additions & 0 deletions Zend/tests/gh15866.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
GH-15866: Core dumped in Zend/zend_generators.c
--FILE--
<?php

class Canary {
public function __construct(public mixed $value) {}
public function __destruct() {
printf("%s\n", __METHOD__);
}
}

function g() {
Fiber::suspend();
}

function f($canary) {
try {
var_dump(yield from g());
} finally {
print "Generator finally\n";
}
}

$canary = new Canary(null);
$iterable = f($canary);
$fiber = new Fiber(function () use ($iterable, $canary) {
try {
$iterable->next();
} finally {
print "Fiber finally\n";
}
});
$canary->value = $fiber;
$fiber->start();

// Reset roots
gc_collect_cycles();

// Add to roots, create garbage cycles
$fiber = $iterable = $canary = null;

print "Collect cycles\n";
gc_collect_cycles();

?>
==DONE==
--EXPECT--
Collect cycles
Canary::__destruct
Generator finally
Fiber finally
==DONE==
25 changes: 6 additions & 19 deletions Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,43 +218,30 @@ static zend_always_inline void clear_link_to_root(zend_generator *generator) {
}
}

/* In the context of zend_generator_dtor_storage during shutdown, check if
* the intermediate node 'generator' is running in a fiber */
/* Check if the node 'generator' is running in a fiber */
static inline bool check_node_running_in_fiber(zend_generator *generator) {
ZEND_ASSERT(EG(flags) & EG_FLAGS_IN_SHUTDOWN);
ZEND_ASSERT(generator->execute_data);

if (generator->flags & ZEND_GENERATOR_IN_FIBER) {
if (EXPECTED(generator->flags & ZEND_GENERATOR_IN_FIBER)) {
return true;
}

if (generator->node.children == 0) {
if (EXPECTED(generator->node.children == 0)) {
return false;
}

if (generator->flags & ZEND_GENERATOR_DTOR_VISITED) {
return false;
}
generator->flags |= ZEND_GENERATOR_DTOR_VISITED;

if (generator->node.children == 1) {
if (check_node_running_in_fiber(generator->node.child.single)) {
goto in_fiber;
}
return false;
return check_node_running_in_fiber(generator->node.child.single);
}

zend_generator *child;
ZEND_HASH_FOREACH_PTR(generator->node.child.ht, child) {
if (check_node_running_in_fiber(child)) {
goto in_fiber;
return true;
}
} ZEND_HASH_FOREACH_END();
return false;

in_fiber:
generator->flags |= ZEND_GENERATOR_IN_FIBER;
return true;
return false;
}

static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
Expand Down
1 change: 0 additions & 1 deletion Zend/zend_generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static const zend_uchar ZEND_GENERATOR_FORCED_CLOSE = 0x2;
static const zend_uchar ZEND_GENERATOR_AT_FIRST_YIELD = 0x4;
static const zend_uchar ZEND_GENERATOR_DO_INIT = 0x8;
static const zend_uchar ZEND_GENERATOR_IN_FIBER = 0x10;
static const zend_uchar ZEND_GENERATOR_DTOR_VISITED = 0x20;

void zend_register_generator_ce(void);
ZEND_API void zend_generator_close(zend_generator *generator, bool finished_execution);
Expand Down
Loading