Fix destruction of suspended generators in suspended fibers during shutdown - #15158
Merged
Conversation
arnaud-lb
force-pushed
the
gh15108
branch
2 times, most recently
from
July 29, 2024 17:04
2119053 to
c4fd31a
Compare
arnaud-lb
marked this pull request as ready for review
July 30, 2024 10:40
bwoebi
reviewed
Jul 30, 2024
Member
There was a problem hiding this comment.
Redundant condition (a few lines deeper as well).
…utdown The generator destructor is a no-op when the generator is running in a fiber, because unwinding the fiber will resume the generator. Normally the destructor is never called in this case, but this can happen during shutdown. We detect that a generator is running in a fiber with the ZEND_GENERATOR_IN_FIBER flag. This change fixes two cases in which this mechanism was broken: - The flag was not added when resuming a 'yield from $nonGenerator', as this is handled in a separate code path - 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.
bwoebi
approved these changes
Jul 30, 2024
bwoebi
left a comment
Member
There was a problem hiding this comment.
It looks right to me, visiting all generators only once sounds good to me.
Maybe call ZEND_GENERATOR_VISITED ZEND_GENERATOR_DTOR_VISITED instead.
arnaud-lb
added a commit
that referenced
this pull request
Jul 30, 2024
* PHP-8.2: [ci skip] NEWS Fix destruction of generator running in fibers during shutdown (#15158)
arnaud-lb
added a commit
that referenced
this pull request
Jul 30, 2024
* PHP-8.3: [ci skip] NEWS [ci skip] NEWS Fix destruction of generator running in fibers during shutdown (#15158)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15108.
In #10462 we ensured that generators running in a fiber are not destroyed before the fiber:
This was extended to support
yield fromin 00be6e1 and b9bca2d (I can not find the related PR anymore).Here I fix two additional cases not handled by #10462:
The
ZEND_GENERATOR_IN_FIBERflag was not added when resuming ayield 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.