Skip to content

Commit b3f0c15

Browse files
authored
gh-116915: Make _thread._ThreadHandle support GC (#116934)
Even though it has no internal references to Python objects it still has a reference to its type by virtue of being a heap type. We need to provide a traverse function that visits the type, but we do not need to provide a clear function.
1 parent 2982bdb commit b3f0c15

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Modules/_threadmodule.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,21 @@ PyThreadHandleObject_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
589589
return (PyObject *)PyThreadHandleObject_new(type);
590590
}
591591

592+
static int
593+
PyThreadHandleObject_traverse(PyThreadHandleObject *self, visitproc visit,
594+
void *arg)
595+
{
596+
Py_VISIT(Py_TYPE(self));
597+
return 0;
598+
}
599+
592600
static void
593601
PyThreadHandleObject_dealloc(PyThreadHandleObject *self)
594602
{
595-
PyObject *tp = (PyObject *) Py_TYPE(self);
603+
PyObject_GC_UnTrack(self);
604+
PyTypeObject *tp = Py_TYPE(self);
596605
ThreadHandle_decref(self->handle);
597-
PyObject_Free(self);
606+
tp->tp_free(self);
598607
Py_DECREF(tp);
599608
}
600609

@@ -673,6 +682,7 @@ static PyType_Slot ThreadHandle_Type_slots[] = {
673682
{Py_tp_dealloc, (destructor)PyThreadHandleObject_dealloc},
674683
{Py_tp_repr, (reprfunc)PyThreadHandleObject_repr},
675684
{Py_tp_getset, ThreadHandle_getsetlist},
685+
{Py_tp_traverse, PyThreadHandleObject_traverse},
676686
{Py_tp_methods, ThreadHandle_methods},
677687
{Py_tp_new, PyThreadHandleObject_tp_new},
678688
{0, 0}
@@ -682,7 +692,7 @@ static PyType_Spec ThreadHandle_Type_spec = {
682692
"_thread._ThreadHandle",
683693
sizeof(PyThreadHandleObject),
684694
0,
685-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
695+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC,
686696
ThreadHandle_Type_slots,
687697
};
688698

0 commit comments

Comments
 (0)