Skip to content

Commit 3d2b6f1

Browse files
ericsnowcurrentlypull[bot]
authored andcommitted
gh-107630: Initialize Each Interpreter's refchain Properly (gh-107733)
This finishes fixing the crashes in Py_TRACE_REFS builds. We missed this part in gh-107567.
1 parent 9cf57de commit 3d2b6f1

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Include/internal/pycore_object.h

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
173173

174174
extern void _PyType_InitCache(PyInterpreterState *interp);
175175

176+
extern void _PyObject_InitState(PyInterpreterState *interp);
176177

177178
/* Inline functions trading binary compatibility for speed:
178179
_PyObject_Init() is the fast version of PyObject_Init(), and

Objects/object.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ _PyDebug_PrintTotalRefs(void) {
162162

163163
#define REFCHAIN(interp) &interp->object_state.refchain
164164

165+
static inline void
166+
init_refchain(PyInterpreterState *interp)
167+
{
168+
PyObject *refchain = REFCHAIN(interp);
169+
refchain->_ob_prev = refchain;
170+
refchain->_ob_next = refchain;
171+
}
172+
165173
/* Insert op at the front of the list of all objects. If force is true,
166174
* op is added even if _ob_prev and _ob_next are non-NULL already. If
167175
* force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
@@ -2019,6 +2027,18 @@ PyObject _Py_NotImplementedStruct = {
20192027
&_PyNotImplemented_Type
20202028
};
20212029

2030+
2031+
void
2032+
_PyObject_InitState(PyInterpreterState *interp)
2033+
{
2034+
#ifdef Py_TRACE_REFS
2035+
if (!_Py_IsMainInterpreter(interp)) {
2036+
init_refchain(interp);
2037+
}
2038+
#endif
2039+
}
2040+
2041+
20222042
extern PyTypeObject _Py_GenericAliasIterType;
20232043
extern PyTypeObject _PyMemoryIter_Type;
20242044
extern PyTypeObject _PyLineIterator;
@@ -2326,7 +2346,7 @@ _Py_GetObjects(PyObject *self, PyObject *args)
23262346

23272347
#undef REFCHAIN
23282348

2329-
#endif
2349+
#endif /* Py_TRACE_REFS */
23302350

23312351

23322352
/* Hack to force loading of abstract.o */

Python/pylifecycle.c

+2
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,8 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
20752075
}
20762076
has_gil = 1;
20772077

2078+
/* No objects have been created yet. */
2079+
20782080
status = pycore_interp_init(tstate);
20792081
if (_PyStatus_EXCEPTION(status)) {
20802082
goto error;

Python/pystate.c

+1
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ init_interpreter(PyInterpreterState *interp,
674674
_obmalloc_pools_INIT(interp->obmalloc.pools);
675675
memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp));
676676
}
677+
_PyObject_InitState(interp);
677678

678679
_PyEval_InitState(interp, pending_lock);
679680
_PyGC_InitState(&interp->gc);

0 commit comments

Comments
 (0)