[mypyc] Ensure a generator can't be entered while it's being executed - #21939
Merged
Conversation
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.
# Conflicts: # mypyc/ir/class_ir.py
p-sawicki
approved these changes
Sep 4, 2026
Comment on lines
+1019
to
+1024
| try: | ||
| next(g) | ||
| except ValueError as e: | ||
| assert str(e) == "generator already executing", str(e) | ||
| else: | ||
| assert False |
Collaborator
There was a problem hiding this comment.
could use assertRaises from testutil, here and in other tests.
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.
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.