Repository navigation
[mypyc] Fix crashes from unchecked throw() arguments, bare raise and unbound async calls - #22417
Merged
JukkaL merged 3 commits intoNov 10, 2026
Merged
Conversation
The generated throw() method passed its arguments to the generator body without validation, so throwing a non-exception (such as gen.throw(5)) segfaulted, and other invalid argument combinations raised a TypeError inside the generator, where an except clause could catch it. Check the arguments like CPython's _gen_throw does before resuming the generator: the first argument must be an exception class or instance, an exception instance may not have a separate value, and the traceback must be a traceback object or None.
CPy_Reraise passed the handled exception to PyErr_Restore even when there was none. This segfaulted on Python 3.10, left the error state corrupted on 3.11 and raised "TypeError: 'NoneType' object is not callable" on 3.12+. Raise "RuntimeError: No active exception to reraise" instead, like CPython.
Async methods and classmethods of native classes are wrapped in a CPyFunction object whose vectorcall handler takes self from the first positional argument without checking that there is one. Calling the function with no arguments, such as C.m() or C.__dict__['m'](), read past the end of the argument array and usually segfaulted. Raise a TypeError instead. tp_call also goes through the vectorcall handler, so this covers both paths.
JukkaL
approved these changes
Nov 10, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
Thanks, these fixes are good!
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 fixes three crashes in mypyc's runtime, each caused by an argument or piece of interpreter state that wasn't validated.
1.
generator.throw()with invalid argumentsThe generated
thrownow callsCPyGen_CheckThrowArgsbefore resuming the generator. It makes the same checks, with the same messages, as CPython's_gen_throw. The first argument must be an exception class, or an instance with no separate value. The traceback must be a traceback object or None.2. Bare
raisewith no active exceptionThis segfaulted on 3.10, corrupted interpreter state on 3.11 and raised
TypeError: 'NoneType' object is not callableon 3.12+.CPy_Reraisenow raisesRuntimeError: No active exception to reraise, as CPython does.3. Calling an async method or classmethod without
selfCPyFunction_Vectorcallreadargs[0]without checkingnargs. It now raisesTypeError: unbound method C.m() needs an argument.tp_callgoes through the same handler, so it is covered too.Regression tests are added to
run-generators.test,run-exceptions.testandrun-async.test.