From fd9340112dc57157bfa2c2983e5ec772788ddfd2 Mon Sep 17 00:00:00 2001 From: Dino Viehland Date: Fri, 4 Dec 2026 12:07:15 -0800 Subject: [PATCH] Only track on replace --- Objects/codeobject.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index aac511d146cf8f..73fcb981a23b15 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -754,8 +754,8 @@ _PyCode_New(struct _PyCodeConstructor *con) #ifdef Py_GIL_DISABLED co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co); -#endif _PyObject_GC_TRACK(co); +#endif Py_XDECREF(replacement_locations); return co; } @@ -2362,6 +2362,13 @@ code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount, linetable, exceptiontable ); +#ifndef Py_GIL_DISABLED + // not tracked by default w/ GIL, but replace can create cycles + if (co) { + _PyObject_GC_TRACK(co); + } +#endif + cleanup: Py_XDECREF(ournames); Py_XDECREF(ourvarnames); @@ -2827,6 +2834,13 @@ code_replace_impl(PyCodeObject *self, int co_argcount, co_qualname, co_firstlineno, co_linetable, co_exceptiontable); +#ifndef Py_GIL_DISABLED + // not tracked by default w/ GIL, but replace can create cycles + if (co) { + _PyObject_GC_TRACK(co); + } +#endif + error: Py_XDECREF(code); Py_XDECREF(varnames);