Skip to content

Commit 1424336

Browse files
authored
gh-92036: Fix gc_fini_untrack() (#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.
1 parent d20bb33 commit 1424336

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a crash in subinterpreters related to the garbage collector. When a
2+
subinterpreter is deleted, untrack all objects tracked by its GC. To prevent a
3+
crash in deallocator functions expecting objects to be tracked by the GC, leak
4+
a strong reference to these objects on purpose, so they are never deleted and
5+
their deallocator functions are not called. Patch by Victor Stinner.

Modules/gcmodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,12 @@ gc_fini_untrack(PyGC_Head *list)
21652165
for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(list)) {
21662166
PyObject *op = FROM_GC(gc);
21672167
_PyObject_GC_UNTRACK(op);
2168+
// gh-92036: If a deallocator function expect the object to be tracked
2169+
// by the GC (ex: func_dealloc()), it can crash if called on an object
2170+
// which is no longer tracked by the GC. Leak one strong reference on
2171+
// purpose so the object is never deleted and its deallocator is not
2172+
// called.
2173+
Py_INCREF(op);
21682174
}
21692175
}
21702176

0 commit comments

Comments
 (0)