bpo-31095: Fix potential crash during GC (GH-3197)
(cherry picked from commit a6296d34a478b4f697ea9db798146195075d496c)
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 1dd4b99..2e4da4c 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1271,6 +1271,8 @@
static void
dequeiter_dealloc(dequeiterobject *dio)
{
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
+ PyObject_GC_UnTrack(dio);
Py_XDECREF(dio->deque);
PyObject_GC_Del(dio);
}
@@ -1556,6 +1558,8 @@
static void
defdict_dealloc(defdictobject *dd)
{
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
+ PyObject_GC_UnTrack(dd);
Py_CLEAR(dd->default_factory);
PyDict_Type.tp_dealloc((PyObject *)dd);
}
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index e0781a9..e9e8933 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -143,6 +143,7 @@
static void
partial_dealloc(partialobject *pto)
{
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(pto);
if (pto->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) pto);
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 1335ae8..0ee4b80 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -745,6 +745,7 @@
static void
bytesio_dealloc(bytesio *self)
{
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
_PyObject_GC_UNTRACK(self);
if (self->buf != NULL) {
PyMem_Free(self->buf);
diff --git a/Modules/_json.c b/Modules/_json.c
index 42c93ab..be1e079 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -840,7 +840,8 @@
static void
scanner_dealloc(PyObject *self)
{
- /* Deallocate scanner object */
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
+ PyObject_GC_UnTrack(self);
scanner_clear(self);
Py_TYPE(self)->tp_free(self);
}
@@ -2298,7 +2299,8 @@
static void
encoder_dealloc(PyObject *self)
{
- /* Deallocate Encoder */
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
+ PyObject_GC_UnTrack(self);
encoder_clear(self);
Py_TYPE(self)->tp_free(self);
}
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 45a1d01..213c7d2 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -2214,6 +2214,8 @@
static void
context_dealloc(PySSLContext *self)
{
+ /* bpo-31095: UnTrack is needed before calling any callbacks */
+ PyObject_GC_UnTrack(self);
context_clear(self);
SSL_CTX_free(self->ctx);
#ifdef OPENSSL_NPN_NEGOTIATED