Skip to content

Commit

Permalink
Fixed bug #75396
Browse files Browse the repository at this point in the history
Do not run finally blocks in generators on unclean shutdown (e.g.
caused by exit). This is consistent with how finally blocks outside
of generators behave.
  • Loading branch information
nikic committed Jan 12, 2018
1 parent 9e98e99 commit 420d11e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ PHP NEWS
. Fixed bug #75786 (segfault when using spread operator on generator passed
by reference). (Nikita)
. Fixed bug #75799 (arg of get_defined_functions is optional). (carusogabriel)
. Fixed bug #75396 (Exit inside generator finally results in fatal error).
(Nikita)

- Opcache:
. Fixed bug #75720 (File cache not populated after SHM runs full). (Dmitry)
Expand Down
22 changes: 22 additions & 0 deletions Zend/tests/generators/bug75396.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Bug #75396: Exit inside generator finally results in fatal error
--FILE--
<?php

$gen = (function () {
yield 42;

try {
echo "Try\n";
exit("Exit\n");
} finally {
echo "Finally\n";
}
})();

$gen->send("x");

?>
--EXPECT--
Try
Exit
3 changes: 2 additions & 1 deletion Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
generator->node.parent = NULL;
}

if (EXPECTED(!ex) || EXPECTED(!(ex->func->op_array.fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK))) {
if (EXPECTED(!ex) || EXPECTED(!(ex->func->op_array.fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK))
|| CG(unclean_shutdown)) {
return;
}

Expand Down

0 comments on commit 420d11e

Please sign in to comment.