Skip to content
Snippets Groups Projects
Unverified Commit 1a4d1c1c authored by Victor Stinner's avatar Victor Stinner Committed by GitHub
Browse files

bpo-46070: _PyGC_Fini() untracks objects (GH-30577)

Py_EndInterpreter() now explicitly untracks all objects currently
tracked by the GC. Previously, if an object was used later by another
interpreter, calling PyObject_GC_UnTrack() on the object crashed if
the previous or the next object of the PyGC_Head structure became a
dangling pointer.
parent 6be84892
No related branches found
No related tags found
No related merge requests found
:c:func:`Py_EndInterpreter` now explicitly untracks all objects currently
tracked by the GC. Previously, if an object was used later by another
interpreter, calling :c:func:`PyObject_GC_UnTrack` on the object crashed if the
previous or the next object of the :c:type:`PyGC_Head` structure became a
dangling pointer. Patch by Victor Stinner.
...@@ -2161,12 +2161,36 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp) ...@@ -2161,12 +2161,36 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp)
} }
} }
static void
gc_fini_untrack(PyGC_Head *list)
{
PyGC_Head *gc;
for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(list)) {
PyObject *op = FROM_GC(gc);
_PyObject_GC_UNTRACK(op);
}
}
void void
_PyGC_Fini(PyInterpreterState *interp) _PyGC_Fini(PyInterpreterState *interp)
{ {
GCState *gcstate = &interp->gc; GCState *gcstate = &interp->gc;
Py_CLEAR(gcstate->garbage); Py_CLEAR(gcstate->garbage);
Py_CLEAR(gcstate->callbacks); Py_CLEAR(gcstate->callbacks);
if (!_Py_IsMainInterpreter(interp)) {
// bpo-46070: Explicitly untrack all objects currently tracked by the
// GC. Otherwise, if an object is used later by another interpreter,
// calling PyObject_GC_UnTrack() on the object crashs if the previous
// or the next object of the PyGC_Head structure became a dangling
// pointer.
for (int i = 0; i < NUM_GENERATIONS; i++) {
PyGC_Head *gen = GEN_HEAD(gcstate, i);
gc_fini_untrack(gen);
}
}
} }
/* for debugging */ /* for debugging */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment