Skip to content

Prevent dtor of generator in suspended fiber #10462

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 3 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions Zend/tests/gh9916-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Bug GH-9916 001 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
try {
print "Before suspend\n";
Fiber::suspend();
print "Not executed";
yield;
} finally {
print "Finally\n";
}
print "Not executed";
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed";
});
$fiber->start();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
Finally
21 changes: 21 additions & 0 deletions Zend/tests/gh9916-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Bug GH-9916 002 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
print "Before suspend\n";
Fiber::suspend();
print "Not executed\n";
yield;
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed";
});
$fiber->start();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
36 changes: 36 additions & 0 deletions Zend/tests/gh9916-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Bug GH-9916 003 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
try {
yield from (function () {
$x = new stdClass;
try {
print "Before suspend\n";
Fiber::suspend();
print "Not executed\n";
yield;
} finally {
print "Finally (inner)\n";
}
})();
print "Not executed\n";
yield;
} finally {
print "Finally\n";
}
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed";
});
$fiber->start();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
Finally (inner)
Finally
26 changes: 26 additions & 0 deletions Zend/tests/gh9916-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Bug GH-9916 004 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
yield from (function () {
$x = new stdClass;
print "Before suspend\n";
Fiber::suspend();
print "Not executed\n";
yield;
})();
print "Not executed\n";
yield;
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed";
});
$fiber->start();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
25 changes: 25 additions & 0 deletions Zend/tests/gh9916-005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Bug GH-9916 005 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
$fiber = yield;
print "Before suspend\n";
Fiber::suspend();
yield;
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->send($fiber);
$gen->current();
});
$fiber->start();

$gen = null;
$fiber = null;
gc_collect_cycles();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
37 changes: 37 additions & 0 deletions Zend/tests/gh9916-006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Bug GH-9916 006 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
yield from (function () {
$x = new stdClass;
print "Before suspend\n";
Fiber::suspend();
print "After suspend\n";
yield;
})();
yield from (function () {
$x = new stdClass;
print "Before exit\n";
exit;
print "Not executed\n";
yield;
})();
yield;
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Fiber return\n";
});
$fiber->start();
$fiber->resume();
$gen->next();
$gen->current();
?>
==DONE==
--EXPECT--
Before suspend
After suspend
Fiber return
Before exit
57 changes: 57 additions & 0 deletions Zend/tests/gh9916-007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
Bug GH-9916 007 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$it = new class implements Iterator
{
public function current(): mixed
{
return null;
}

public function key(): mixed
{
return 0;
}

public function next(): void
{
}

public function rewind(): void
{
try {
print "Before suspend\n";
Fiber::suspend();
print "Not executed\n";
} finally {
print "Finally (iterator)\n";
}
}

public function valid(): bool
{
return true;
}
};

$gen = (function() use ($it) {
try {
yield from $it;
print "Not executed\n";
} finally {
print "Finally\n";
}
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed";
});
$fiber->start();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
Finally (iterator)
Finally
47 changes: 47 additions & 0 deletions Zend/tests/gh9916-008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Bug GH-9916 008 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$it = new class implements Iterator
{
public function current(): mixed
{
return null;
}

public function key(): mixed
{
return 0;
}

public function next(): void
{
}

public function rewind(): void
{
$x = new stdClass;
print "Before suspend\n";
Fiber::suspend();
}

public function valid(): bool
{
return true;
}
};

$gen = (function() use ($it) {
$x = new stdClass;
yield from $it;
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed";
});
$fiber->start();
?>
==DONE==
--EXPECT--
Before suspend
==DONE==
35 changes: 35 additions & 0 deletions Zend/tests/gh9916-009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Bug GH-9916 009 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
try {
print "Before suspend\n";
Fiber::suspend();
print "Not executed\n";
} finally {
print "Finally\n";
yield from ['foo' => new stdClass];
print "Not executed\n";
}
})();
$fiber = new Fiber(function() use ($gen, &$fiber) {
$gen->current();
print "Not executed\n";
});
$fiber->start();
?>
==DONE==
--EXPECTF--
Before suspend
==DONE==
Finally

Fatal error: Uncaught Error: Cannot use "yield from" in a force-closed generator in %s:%d
Stack trace:
#0 [internal function]: {closure}()
#1 %s(%d): Generator->current()
#2 [internal function]: {closure}()
#3 {main}
thrown in %s on line %d
35 changes: 35 additions & 0 deletions Zend/tests/gh9916-010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Bug GH-9916 010 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
print "Before yield\n";
yield from (function () {
$x = new stdClass;
print "Before yield 2\n";
yield;
print "Before suspend\n";
Fiber::suspend();
})();
})();

$fiber = new Fiber(function () use ($gen, &$fiber) {
print "Before current\n";
$gen->current();
print "Before next\n";
$gen->next();
print "Not executed\n";
});

$fiber->start();
?>
==DONE==
--EXPECT--
Before current
Before yield
Before yield 2
Before next
Before suspend
==DONE==
.
41 changes: 41 additions & 0 deletions Zend/tests/gh9916-011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Bug GH-9916 011 (Entering shutdown sequence with a fiber suspended in a Generator emits an unavoidable fatal error or crashes)
--FILE--
<?php
$gen = (function() {
$x = new stdClass;
print "Before yield\n";
$from = (function () {
$x = new stdClass;
print "Before yield 2\n";
yield;
print "Before suspend\n";
Fiber::suspend();
print "Not executed\n";
yield;
})();
try {
yield from $from;
} finally {
$from->next();
}
})();

$fiber = new Fiber(function () use ($gen, &$fiber) {
print "Before current\n";
$gen->current();
print "Before next\n";
$gen->next();
print "Not executed\n";
});

$fiber->start();
?>
==DONE==
--EXPECT--
Before current
Before yield
Before yield 2
Before next
Before suspend
==DONE==
Loading