]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-140431: Fix GC crash due to partially initialized coroutines (gh-140470)
authorSam Gross <colesbury@gmail.com>
Thu, 23 Oct 2025 14:18:13 +0000 (10:18 -0400)
committerGitHub <noreply@github.com>
Thu, 23 Oct 2025 14:18:13 +0000 (10:18 -0400)
The `make_gen()` function creates and tracks generator/coro objects, but
doesn't fully initialize all the fields. At a minimum, we need to
initialize all the fields that may be accessed by gen_traverse because
the call to `compute_cr_origin()` can trigger a GC.

Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-17-22-22.gh-issue-140431.m8D_A-.rst [new file with mode: 0644]
Objects/genobject.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-17-22-22.gh-issue-140431.m8D_A-.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-22-17-22-22.gh-issue-140431.m8D_A-.rst
new file mode 100644 (file)
index 0000000..3d62d21
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a crash in Python's :term:`garbage collector <garbage collection>` due to
+partially initialized :term:`coroutine` objects when coroutine origin tracking
+depth is enabled (:func:`sys.set_coroutine_origin_tracking_depth`).
index c9ca2f1de51ddc9b139f9ca7479445b3cd1099ba..2371ad16d5c1a666e2f4cc2fcdda7b6f3feefb8a 100644 (file)
@@ -932,6 +932,7 @@ make_gen(PyTypeObject *type, PyFunctionObject *func)
     gen->gi_weakreflist = NULL;
     gen->gi_exc_state.exc_value = NULL;
     gen->gi_exc_state.previous_item = NULL;
+    gen->gi_iframe.f_executable = PyStackRef_None;
     assert(func->func_name != NULL);
     gen->gi_name = Py_NewRef(func->func_name);
     assert(func->func_qualname != NULL);