bpo-31095: Fix potential crash during GC (GH-3197)
(cherry picked from commit a6296d34a478b4f697ea9db798146195075d496c)
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);
}