Skip to content

[mypyc] Ensure a generator can't be entered while it's being executed - #21939

Merged
JukkaL merged 11 commits into
masterfrom
mypyc-async-2
Sep 4, 2026
Merged

[mypyc] Ensure a generator can't be entered while it's being executed#21939
JukkaL merged 11 commits into
masterfrom
mypyc-async-2

Conversation

@JukkaL

@JukkaL JukkaL commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

This matches Python semantics -- an exception is now raised if there is an attempt to enter generator/coroutine while it's running.

This also allows unsynchronized access of generator attributes in free-threaded builds, since there can't be concurrent accesses (in simple cases where we use a merged generator and environment). This has a big performance impact on free-threaded-builds. Some microbenchmarks were 1.5x+ faster with this optimization, as synchronized attribute access is quite inefficient, and it was being used for all registers in generators and async defs.

I used coding agent assist but reviewed changes manually.

This only applies to coroutines. This allows using unsynchronized
attribute access, which is a bug win on free-threaded builds. This
matches Python semantics.

We should perhaps do this in non-FT builds as well, but this is
left as a follow-up task, as there it won't directly help with
performance.
Comment thread mypyc/test-data/run-generators.test Outdated
Comment on lines +1019 to +1024
try:
next(g)
except ValueError as e:
assert str(e) == "generator already executing", str(e)
else:
assert False

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use assertRaises from testutil, here and in other tests.

@JukkaL
JukkaL merged commit 0ff707d into master Sep 4, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-async-2 branch September 4, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants