gh-92036: Fix gc_fini_untrack() (GH-92037)

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 14243369b5)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2022-05-04 03:25:33 -07:00 committed by GitHub
parent 524d2750e3
commit f84c51eb7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -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.

View File

@ -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);
}
}