[php-src] PR #15158: Fix destruction of suspended generators in suspended fibers during shutdown
| From: | arnaud-lb | Date: | Mon, 29 Jul 2024 16:44:11 +0000 |
| Subject: | [php-src] PR #15158: Fix destruction of suspended generators in suspended fibers during shutdown | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-27563@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/15158
Author: arnaud-lb
Fixes #15108.
In https://github.com/php/php-src/pull/10462
we ensured that generators running in a fiber are not destroyed before the fiber:
> The shutdown sequence calls the destructor of any live generator or fiber (or any object).
>
> When a fiber is suspended in a generator, the fiber destructor will resume the execution the
> generator, which may be already be destroyed. This leads to the issues described in # 9916.
>
> Because of this, we must not attempt to destroy a generator that was executing in a suspended
> fiber.
>
> The fiber destructor conveniently throws a zend_ce_graceful_exit in the fiber context, which
> will also properly terminate the generator (and execute its finally blocks). I believe that this has
> the same semantics as the generator destructor.
This was extended to support
yield from in 00be6e1aed5e55c04f716a7364ff55e698e154a5 and
b9bca2dadb0d8d554615f944c3fbff6e0750d72d (I can not find the related PR anymore).
Here I fix two additional cases not handled by https://github.com/php/php-src/pull/10462:
- The ZEND_GENERATOR_IN_FIBER flag was not added when resuming a yield from
$nonGenerator. I fix that by adding the flag earlier.
- When a generator that is running in a fiber has multiple children (aka multiple generators
yielding from it), all of them could be considered to also run in a fiber (only one actually is),
and could leak if not destroyed before shutdown.
Unfortunately the second one requires traversing the child tree (at most once) during dtor if the
root is marked ZEND_GENERATOR_IN_FIBER.