Skip to content

Commit

Permalink
pythongh-92036: Fix gc_fini_untrack() (pythonGH-92037)
Browse files Browse the repository at this point in the history
Fix a crash in subinterpreters related to the garbage collector. When
a subinterpreter is deleted, untrack all objects tracked by its GC.
To prevent a crash in deallocator functions expecting objects to be
tracked by the GC, leak a strong reference to these objects on
purpose, so they are never deleted and their deallocator functions
are not called.
(cherry picked from commit 1424336)

Co-authored-by: Victor Stinner <[email protected]>
  • Loading branch information
2 people authored and hello-adam committed Jun 2, 2022
1 parent 882a3c2 commit ede522e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a crash in subinterpreters related to the garbage collector. When a
subinterpreter is deleted, untrack all objects tracked by its GC. To prevent a
crash in deallocator functions expecting objects to be tracked by the GC, leak
a strong reference to these objects on purpose, so they are never deleted and
their deallocator functions are not called. Patch by Victor Stinner.
6 changes: 6 additions & 0 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,12 @@ gc_fini_untrack(PyGC_Head *list)
for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(list)) {
PyObject *op = FROM_GC(gc);
_PyObject_GC_UNTRACK(op);
// gh-92036: If a deallocator function expect the object to be tracked
// by the GC (ex: func_dealloc()), it can crash if called on an object
// which is no longer tracked by the GC. Leak one strong reference on
// purpose so the object is never deleted and its deallocator is not
// called.
Py_INCREF(op);
}
}

Expand Down

0 comments on commit ede522e

Please sign in to comment.