-
Notifications
You must be signed in to change notification settings - Fork 24.9k
[generator] Close all open generators in compile_subgraph #157149
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
Open
guilhermeleobas
wants to merge
11
commits into
gh/guilhermeleobas/193/base
Choose a base branch
from
gh/guilhermeleobas/193/head
base: gh/guilhermeleobas/193/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[generator] Close all open generators in compile_subgraph #157149
guilhermeleobas
wants to merge
11
commits into
gh/guilhermeleobas/193/base
from
gh/guilhermeleobas/193/head
Conversation
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
This was referenced Jun 27, 2025
guilhermeleobas
added a commit
that referenced
this pull request
Jun 27, 2025
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: e1abab6 Pull-Request: #156880 ghstack-source-id: 45df03f Pull Request resolved: #157149
guilhermeleobas
added a commit
that referenced
this pull request
Jun 27, 2025
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
guilhermeleobas
added a commit
that referenced
this pull request
Jun 27, 2025
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
guilhermeleobas
added a commit
that referenced
this pull request
Jun 27, 2025
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
guilhermeleobas
added a commit
that referenced
this pull request
Jun 27, 2025
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
zou3519
reviewed
Jul 1, 2025
zou3519
reviewed
Jul 1, 2025
@@ -1397,6 +1397,7 @@ def compile_subgraph( | |||
overridden_sources=overridden_sources, | |||
) | |||
self.codegen_suffix(tx, stack_values_flat, pass1) | |||
self.side_effects.close_local_generators() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment about why this is here.
also TODO(rzou): is this actually the right place?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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.
Stack from ghstack (oldest at bottom):
StopIteration(value)
with value from the return stmt #157152contextlib.suppress
#147990Motivation and Example
Explictly close all open generators in compile_subgraph to ensure that
all remaining finally blocks are executed. In CPython this is done by
invoking the
tp_finalize
function of the generator object, which triggersgenclose
:https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames