Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[mypyc] Free generator after await encounters StopIteration
Previously the awaited generator could stay alive until the generator
that performed the await was freed, delaying object reclamation. The
refcount analysis doesn't understand registers spilled to the
environment, so we need to manually clear the value.

Consider code like this:
```
async def foo() -> None:
    await bar()
    await zar()
```
Previously, the `bar()` generator was only freed at end of `foo()`. Now
we release it before `await zar()`, as expected.
  • Loading branch information
JukkaL committed Jun 4, 2025
commit 4ab3f0f736df4bda33464b883e36b577fe186021
4 changes: 4 additions & 0 deletions mypyc/irbuild/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ def emit_yield_from_or_await(
# If it wasn't, this reraises the exception.
builder.activate_block(stop_block)
builder.assign(result, builder.call_c(check_stop_op, [], line), line)
# Clear the spilled iterator/coroutine so that it will be freed.
# Otherwise, the freeing of the spilled register would likely be delayed.
err = builder.add(LoadErrorValue(object_rprimitive))
builder.assign(iter_reg, err, line)
builder.goto(done_block)

builder.activate_block(main_block)
Expand Down