]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45813: Make sure that frame->generator is NULLed when generator is deallocated...
authorMark Shannon <mark@hotpy.org>
Mon, 22 Nov 2021 14:01:23 +0000 (14:01 +0000)
committerGitHub <noreply@github.com>
Mon, 22 Nov 2021 14:01:23 +0000 (14:01 +0000)
Lib/test/test_coroutines.py
Misc/NEWS.d/next/Core and Builtins/2021-11-22-11-28-13.bpo-45813.ZMaWE2.rst [new file with mode: 0644]
Objects/genobject.c
Python/frame.c

index 4350e185a247f1286d8c9a49058969a6be31bfe4..fc8b8bc9541eba145377f7963f8d39e6d5f48055 100644 (file)
@@ -2191,6 +2191,13 @@ class CoroutineTest(unittest.TestCase):
             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):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-11-22-11-28-13.bpo-45813.ZMaWE2.rst b/Misc/NEWS.d/next/Core and Builtins/2021-11-22-11-28-13.bpo-45813.ZMaWE2.rst
new file mode 100644 (file)
index 0000000..65f64b1
--- /dev/null
@@ -0,0 +1 @@
+Fix crash when calling coro.cr_frame.clear() after coroutine has been freed.
index efd255d33f9be86f66498f6d26f0335d9037da9f..c899ed6a82e30b5db5f1039cbf3cfc7f57ee2b18 100644 (file)
@@ -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);
     }
index 3d2415fee70978ae8bd842b824a2b505c2478caf..a5c93eaaa5f37a513fcf14134ade6288e500464f 100644 (file)
@@ -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;