Skip to content

[generator] Close all open generators in compile_subgraph #156880

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

Closed

Conversation

guilhermeleobas
Copy link
Collaborator

@guilhermeleobas guilhermeleobas commented Jun 25, 2025

Stack from ghstack (oldest at bottom):

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

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-poisoned]
Copy link

pytorch-bot bot commented Jun 25, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/156880

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 10 New Failures, 4 Cancelled Jobs, 1 Unrelated Failure

As of commit 15314d9 with merge base c82a174 (image):

NEW FAILURES - The following jobs have failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Jun 26, 2025
guilhermeleobas added a commit that referenced this pull request Jun 26, 2025
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: d3d7e36
Pull-Request: #156880
@guilhermeleobas guilhermeleobas changed the title [generator] Raise StopIteration with value from return stmt [generator] Close all open generators in compile_subgraph Jun 26, 2025
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Jun 26, 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
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
@github-actions github-actions bot deleted the gh/guilhermeleobas/187/head branch July 28, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants