]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107630: Initialize Each Interpreter's refchain Properly (gh-107733)
authorEric Snow <ericsnowcurrently@gmail.com>
Mon, 7 Aug 2023 19:14:56 +0000 (13:14 -0600)
committerGitHub <noreply@github.com>
Mon, 7 Aug 2023 19:14:56 +0000 (13:14 -0600)
This finishes fixing the crashes in Py_TRACE_REFS builds.  We missed this part in gh-107567.

Include/internal/pycore_object.h
Objects/object.c
Python/pylifecycle.c
Python/pystate.c

index 76b3cd69cbf5a74411a5ba4aafc4623ffc36e44a..7cdf64bcdacb9f893115102a549d3bf6c84bcc77 100644 (file)
@@ -173,6 +173,7 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
 
 extern void _PyType_InitCache(PyInterpreterState *interp);
 
+extern void _PyObject_InitState(PyInterpreterState *interp);
 
 /* Inline functions trading binary compatibility for speed:
    _PyObject_Init() is the fast version of PyObject_Init(), and
index c4413a00ede80ceb8b5bf1c255799fd1e1f068ee..d1154eb344fab1ab73410c6dab71009553cc0814 100644 (file)
@@ -162,6 +162,14 @@ _PyDebug_PrintTotalRefs(void) {
 
 #define REFCHAIN(interp) &interp->object_state.refchain
 
+static inline void
+init_refchain(PyInterpreterState *interp)
+{
+    PyObject *refchain = REFCHAIN(interp);
+    refchain->_ob_prev = refchain;
+    refchain->_ob_next = refchain;
+}
+
 /* Insert op at the front of the list of all objects.  If force is true,
  * op is added even if _ob_prev and _ob_next are non-NULL already.  If
  * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
@@ -2019,6 +2027,18 @@ PyObject _Py_NotImplementedStruct = {
     &_PyNotImplemented_Type
 };
 
+
+void
+_PyObject_InitState(PyInterpreterState *interp)
+{
+#ifdef Py_TRACE_REFS
+    if (!_Py_IsMainInterpreter(interp)) {
+        init_refchain(interp);
+    }
+#endif
+}
+
+
 extern PyTypeObject _Py_GenericAliasIterType;
 extern PyTypeObject _PyMemoryIter_Type;
 extern PyTypeObject _PyLineIterator;
@@ -2326,7 +2346,7 @@ _Py_GetObjects(PyObject *self, PyObject *args)
 
 #undef REFCHAIN
 
-#endif
+#endif  /* Py_TRACE_REFS */
 
 
 /* Hack to force loading of abstract.o */
index 7a17f92b550c0542940c45699b6b2811344332e0..0de3abf9407899f5f7b93351036248b5bd4041d1 100644 (file)
@@ -2075,6 +2075,8 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
     }
     has_gil = 1;
 
+    /* No objects have been created yet. */
+
     status = pycore_interp_init(tstate);
     if (_PyStatus_EXCEPTION(status)) {
         goto error;
index a1864cc3249e2f78d28087eae439aef4758b2120..3a05cb0fa7988d9097cbe14e453efdbc9e433591 100644 (file)
@@ -674,6 +674,7 @@ init_interpreter(PyInterpreterState *interp,
                 _obmalloc_pools_INIT(interp->obmalloc.pools);
         memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp));
     }
+    _PyObject_InitState(interp);
 
     _PyEval_InitState(interp, pending_lock);
     _PyGC_InitState(&interp->gc);