Skip to content

Fix destruction of suspended generators in suspended fibers during shutdown #15158

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 2 commits into from
Jul 30, 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
36 changes: 36 additions & 0 deletions Zend/tests/gh15108-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
GH-15108 001: Segfault with delegated generator in suspended fiber
--FILE--
<?php

class It implements \IteratorAggregate
{
public function getIterator(): \Generator
{
yield 'foo';
Fiber::suspend();
var_dump("not executed");
}
}

function f() {
yield from new It();
}

$iterable = f();

$fiber = new Fiber(function () use ($iterable) {
var_dump($iterable->current());
$iterable->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
==DONE==
40 changes: 40 additions & 0 deletions Zend/tests/gh15108-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-15108 002: Segfault with delegated generator in suspended fiber
--FILE--
<?php

class It implements \IteratorAggregate
{
public function getIterator(): \Generator
{
yield 'foo';
Fiber::suspend();
var_dump("not executed");
}
}

function g() {
yield from new It();
}

function f() {
yield from g();
}

$iterable = f();

$fiber = new Fiber(function () use ($iterable) {
var_dump($iterable->current());
$iterable->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
==DONE==
38 changes: 38 additions & 0 deletions Zend/tests/gh15108-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
GH-15108 003: Segfault with delegated generator in suspended fiber
--FILE--
<?php

class It implements \IteratorAggregate
{
public function getIterator(): \Generator
{
yield 'foo';
Fiber::suspend();
var_dump("not executed");
}
}

function f($gen) {
yield from $gen;
}

$a = new It();
$b = f($a);
$c = f($a);

$fiber = new Fiber(function () use ($a, $b, $c) {
var_dump($b->current());
$b->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
==DONE==
39 changes: 39 additions & 0 deletions Zend/tests/gh15108-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-15108 004: Segfault with delegated generator in suspended fiber
--FILE--
<?php

function gen1() {
yield 'foo';
Fiber::suspend();
var_dump("not executed");
};

function gen2($gen) {
yield from $gen;
var_dump("not executed");
}

$a = gen1();
/* Both $b and $c have a root marked with IN_FIBER, but only $b is actually
* running in a fiber (at shutdown) */
$b = gen2($a);
$c = gen2($a);

$fiber = new Fiber(function () use ($a, $b, $c) {
var_dump($b->current());
var_dump($c->current());
$b->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
string(3) "foo"
==DONE==
45 changes: 45 additions & 0 deletions Zend/tests/gh15108-005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
GH-15108 005: Segfault with delegated generator in suspended fiber
--FILE--
<?php

class It implements \IteratorAggregate
{
public function getIterator(): \Generator
{
yield 'foo';
Fiber::suspend();
var_dump("not executed");
}
}

function f() {
yield from new It();
}

function g() {
yield from f();
}

function h() {
/* g() is an intermediate node and will not be marked with IN_FIBER */
yield from g();
}

$iterable = h();
var_dump($iterable->current());

$fiber = new Fiber(function () use ($iterable) {
$iterable->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
==DONE==
49 changes: 49 additions & 0 deletions Zend/tests/gh15108-006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
GH-15108 006: Segfault with delegated generator in suspended fiber
--FILE--
<?php

class It implements \IteratorAggregate
{
public function getIterator(): \Generator
{
yield 'foo';
Fiber::suspend();
var_dump("not executed");
}
}

function f() {
yield from new It();
}

function g() {
yield from f();
}

function gen($gen) {
/* $gen is an intermediate node and will not be marked with IN_FIBER */
yield from $gen;
}

$g = g();
$a = gen($g);
$b = gen($g);
var_dump($a->current());
var_dump($b->current());

$fiber = new Fiber(function () use ($a, $b, $g) {
$a->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
string(3) "foo"
==DONE==
51 changes: 51 additions & 0 deletions Zend/tests/gh15108-007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
GH-15108 007: Segfault with delegated generator in suspended fiber
--FILE--
<?php

class It implements \IteratorAggregate
{
public function getIterator(): \Generator
{
yield 'foo';
Fiber::suspend();
var_dump("not executed");
}
}

function f() {
yield from new It();
}

function g() {
yield from f();
}

function gen($gen) {
/* $gen is an intermediate node and will not be marked with IN_FIBER */
yield from $gen;
}

$g = g();
$a = gen($g);
$b = gen($g);
$c = gen($g);
$d = gen($g);
var_dump($a->current());
var_dump($b->current());

$fiber = new Fiber(function () use ($a, $b, $c, $d, $g) {
$b->next();
var_dump("not executed");
});

$ref = $fiber;

$fiber->start();

?>
==DONE==
--EXPECT--
string(3) "foo"
string(3) "foo"
==DONE==
Loading
Loading