You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Interpreter crashes if creating a generator for a function whose __code__ was re-assigned #163129
When the interpreter goes to create a generator in RETURN_GENERATOR, it fetches the frame's function and then that function's code object, which it then uses to size the generator. However, the function's code object might have changed in-between the current frame being pushed and the RETURN_GENERATOR opcode being executed, such as during argument binding. This might lead to the interpreter allocating a generator that is too small to accommodate the frame that it's about to copy into it, result in a heap overflow, and possibly a crash.
deftest_code_reassigned_before_generator_created(self):
# A generator must be created from the code object of the frame being# run, even if __code__ was reassigned after that frame was pushed.# Here a keyword default lookup runs __eq__ during argument binding,# and swaps in a code object with a much smaller frame.defsmall():
yield'small'src= ('def big(*, x):\n'+''.join(f' v{i} = {i}\n'foriinrange(1000))
+' yield x\n')
ns= {}
exec(src, ns)
big=ns['big']
big_code=big.__code__classKey(str):
__hash__=str.__hash__def__eq__(self, other):
big.__code__=small.__code__returnstr.__eq__(self, other)
gens= []
foriinrange(10):
big.__code__=big_codebig.__kwdefaults__= {Key('x'): i}
gens.append(big())
self.assertIs(big.__code__, small.__code__)
# Run a GC pass to touch memory and increase the chance of a crash if# the generators trampled over memory when they copied their frames.support.gc_collect()
fori, geninenumerate(gens):
self.assertIs(gen.gi_code, big_code)
self.assertEqual(list(gen), [i])
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.16.0a2+dev (heads/main:6ed9dc2ead6, Dec 4 2026, 00:39:14) [GCC 16.2.1 20261029 (Red Hat 16.2.1-3)]
I feel that this crash is similar to messing up with code.__replace__. When is it possible for such even to happen for regular code and not just code that replaces __code__ attributes without taking care of that? is it something we should support?
@picnixz It's definitely not something most users would encounter. I would say that it's important to address the potential crash, because the interpreter shouldn't crash from Python code like this, but it doesn't really matter how the code behaves / what it resolves to.
, but it doesn't really matter how the code behaves / what it resolves to.
No it matters. We don't consider these as real issues if the reproducer is messing with interpreter's state in such a way that could affect performance for instance.
For instance, any crashes obtained by incorrectly using code.__replace__ is not considered a bug. Potential crashes that require contrived examples like that are therefore rejected. But if you can get that crash without such convoluted example, we usually accept the report.
That's why I want to know if there are legitimate use cases of changing __code__ entirely after creating a generator.
@picnixz Asking some basic questions, sorry, new here, thanks for bearing with me :)
We don't consider these as real issues if the reproducer is messing with interpreter's state in such a way that could affect performance for instance.
"Messing with the interpreter's state" to me sounds like a user calling through the C API though, rather than through Python code. I would assume that Python code should never crash the interpreter, is that not the case?
For instance, any crashes obtained by incorrectly using code.replace is not considered a bug.
That's why I want to know if there are legitimate use cases of changing code entirely after creating a generator.
It's not really a legitimate situation on the GIL build, it's more of a problem for the free-threaded build. Any thread that's re-assigning a function's __code__ (hot reloading?) can collide with another thread that's creating a generator for the same function. I wanted to submit a reproducer that affected both GIL and FT so it could be tested across both builds, hence the odd case.
"Messing with the interpreter's state" to me sounds like a user calling through the C API though, rather than through Python code. I would assume that Python code should never crash the interpreter, is that not the case?
No it's not the case. For instance, messing with ctypes, with concurrent mutations that are weird or with code.__replace__ are usually not legitimate changes.
It's not documented because it's something a "if you mess with something incorrectly that is deeply related to internals, you're on your own", and we don't want to document any changes that should not be done because it can crash something (it's part of what people would consider "common sense"). It's an oral... knowledge (like anything in computer science). I've written something in the devguide but I haven't merged that yet.
. Any thread that's re-assigning a function's code (hot reloading?) can collide with another thread that's creating a generator for the same function.
Is this something that happens frequently? I would assume that it's a case where you're responsible for the change.
That being said, the change you're suggesting looks small enough to be accepted but I still think it's a pathological case.
Is this something that happens frequently? I would assume that it's a case where you're responsible for the change.
Probably not frequently, but without this fix it would be troublesome to handle it from user code. To me at least, it would be a surprise that I'd need guard all uses (reads, calls, .etc) of func with a lock to be safe to assign to func.__code__ but not for func.__name__, for instance. At least in the context of not crashing, vs not racing.
Activity
picnixz commentedon Dec 4, 2026
I feel that this crash is similar to messing up with
code.__replace__. When is it possible for such even to happen for regular code and not just code that replaces__code__attributes without taking care of that? is it something we should support?alexmalyshev commentedon Dec 4, 2026
@picnixz It's definitely not something most users would encounter. I would say that it's important to address the potential crash, because the interpreter shouldn't crash from Python code like this, but it doesn't really matter how the code behaves / what it resolves to.
picnixz commentedon Dec 4, 2026
No it matters. We don't consider these as real issues if the reproducer is messing with interpreter's state in such a way that could affect performance for instance.
For instance, any crashes obtained by incorrectly using
code.__replace__is not considered a bug. Potential crashes that require contrived examples like that are therefore rejected. But if you can get that crash without such convoluted example, we usually accept the report.That's why I want to know if there are legitimate use cases of changing
__code__entirely after creating a generator.alexmalyshev commentedon Dec 4, 2026
@picnixz Asking some basic questions, sorry, new here, thanks for bearing with me :)
"Messing with the interpreter's state" to me sounds like a user calling through the C API though, rather than through Python code. I would assume that Python code should never crash the interpreter, is that not the case?
Is this documented anywhere? And what does "incorrect" usage here mean? I only found https://docs.python.org/3/reference/datamodel.html#codeobject.replace which doesn't include any warnings / suggestions on using this correctly.
It's not really a legitimate situation on the GIL build, it's more of a problem for the free-threaded build. Any thread that's re-assigning a function's
__code__(hot reloading?) can collide with another thread that's creating a generator for the same function. I wanted to submit a reproducer that affected both GIL and FT so it could be tested across both builds, hence the odd case.picnixz commentedon Dec 4, 2026
No it's not the case. For instance, messing with
ctypes, with concurrent mutations that are weird or withcode.__replace__are usually not legitimate changes.It's not documented because it's something a "if you mess with something incorrectly that is deeply related to internals, you're on your own", and we don't want to document any changes that should not be done because it can crash something (it's part of what people would consider "common sense"). It's an oral... knowledge (like anything in computer science). I've written something in the devguide but I haven't merged that yet.
Is this something that happens frequently? I would assume that it's a case where you're responsible for the change.
That being said, the change you're suggesting looks small enough to be accepted but I still think it's a pathological case.
alexmalyshev commentedon Dec 4, 2026
Probably not frequently, but without this fix it would be troublesome to handle it from user code. To me at least, it would be a surprise that I'd need guard all uses (reads, calls, .etc) of
funcwith a lock to be safe to assign tofunc.__code__but not forfunc.__name__, for instance. At least in the context of not crashing, vs not racing.