Skip to content

bpo-45813: Make sure that frame->generator is NULLed when generator is deallocated. #29700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,13 @@ async def run_gen():
return 'end'
self.assertEqual(run_async(run_gen()), ([], 'end'))

def test_bpo_45813(self):
'This would crash the interpreter in 3.11a2'
async def f():
pass
frame = f().cr_frame
frame.clear()


class CoroAsyncIOCompatTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash when calling coro.cr_frame.clear() after coroutine has been freed.
1 change: 1 addition & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ gen_dealloc(PyGenObject *gen)
InterpreterFrame *frame = gen->gi_xframe;
if (frame != NULL) {
gen->gi_xframe = NULL;
frame->generator = NULL;
frame->previous = NULL;
_PyFrame_Clear(frame, 1);
}
Expand Down
3 changes: 3 additions & 0 deletions Python/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ take_ownership(PyFrameObject *f, InterpreterFrame *frame)
int
_PyFrame_Clear(InterpreterFrame * frame, int take)
{
/* It is the responsibility of the owning generator/coroutine
* to have cleared the generator pointer */
assert(frame->generator == NULL);
if (frame->frame_obj) {
PyFrameObject *f = frame->frame_obj;
frame->frame_obj = NULL;
Expand Down