Skip to content

Commit

Permalink
bpo-46070: _PyGC_Fini() untracks objects (pythonGH-30577) (pythonGH-3…
Browse files Browse the repository at this point in the history
…0580)

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.

(cherry picked from commit 1a4d1c1)
  • Loading branch information
vstinner authored Jan 13, 2022
1 parent 4ddd5da commit 52937c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
: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.
24 changes: 24 additions & 0 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2149,12 +2149,36 @@ _PyGC_DumpShutdownStats(PyThreadState *tstate)
}
}


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
_PyGC_Fini(PyThreadState *tstate)
{
GCState *gcstate = &tstate->interp->gc;
Py_CLEAR(gcstate->garbage);
Py_CLEAR(gcstate->callbacks);

if (!_Py_IsMainInterpreter(tstate)) {
// 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 */
Expand Down

0 comments on commit 52937c2

Please sign in to comment.