summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorINADA Naoki <methane@users.noreply.github.com>2017-09-04 03:31:09 (GMT)
committerGitHub <noreply@github.com>2017-09-04 03:31:09 (GMT)
commit2eea952b1b9ebbc2d94fd3faca1536c6b4963725 (patch)
tree5262bce350c874f5397b8ad2ca2a6671cb4dd9d6 /Doc
parent7d8282d25d4900dd3367daf28bb393be7f276729 (diff)
downloadcpython-2eea952b1b9ebbc2d94fd3faca1536c6b4963725.zip
cpython-2eea952b1b9ebbc2d94fd3faca1536c6b4963725.tar.gz
cpython-2eea952b1b9ebbc2d94fd3faca1536c6b4963725.tar.bz2
bpo-31095: fix potential crash during GC (GH-3195)
(cherry picked from commit a6296d34a478b4f697ea9db798146195075d496c)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/extending/newtypes.rst29
-rw-r--r--Doc/includes/noddy4.c1
2 files changed, 21 insertions, 9 deletions
diff --git a/Doc/extending/newtypes.rst b/Doc/extending/newtypes.rst
index 003b4e5..abd5da9 100644
--- a/Doc/extending/newtypes.rst
+++ b/Doc/extending/newtypes.rst
@@ -728,8 +728,9 @@ functions. With :c:func:`Py_VISIT`, :c:func:`Noddy_traverse` can be simplified:
uniformity across these boring implementations.
We also need to provide a method for clearing any subobjects that can
-participate in cycles. We implement the method and reimplement the deallocator
-to use it::
+participate in cycles.
+
+::
static int
Noddy_clear(Noddy *self)
@@ -747,13 +748,6 @@ to use it::
return 0;
}
- static void
- Noddy_dealloc(Noddy* self)
- {
- Noddy_clear(self);
- Py_TYPE(self)->tp_free((PyObject*)self);
- }
-
Notice the use of a temporary variable in :c:func:`Noddy_clear`. We use the
temporary variable so that we can set each member to *NULL* before decrementing
its reference count. We do this because, as was discussed earlier, if the
@@ -776,6 +770,23 @@ be simplified::
return 0;
}
+Note that :c:func:`Noddy_dealloc` may call arbitrary functions through
+``__del__`` method or weakref callback. It means circular GC can be
+triggered inside the function. Since GC assumes reference count is not zero,
+we need to untrack the object from GC by calling :c:func:`PyObject_GC_UnTrack`
+before clearing members. Here is reimplemented deallocator which uses
+:c:func:`PyObject_GC_UnTrack` and :c:func:`Noddy_clear`.
+
+::
+
+ static void
+ Noddy_dealloc(Noddy* self)
+ {
+ PyObject_GC_UnTrack(self);
+ Noddy_clear(self);
+ Py_TYPE(self)->tp_free((PyObject*)self);
+ }
+
Finally, we add the :const:`Py_TPFLAGS_HAVE_GC` flag to the class flags::
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
diff --git a/Doc/includes/noddy4.c b/Doc/includes/noddy4.c
index eb9622a..08ba4c3 100644
--- a/Doc/includes/noddy4.c
+++ b/Doc/includes/noddy4.c
@@ -46,6 +46,7 @@ Noddy_clear(Noddy *self)
static void
Noddy_dealloc(Noddy* self)
{
+ PyObject_GC_UnTrack(self);
Noddy_clear(self);
Py_TYPE(self)->tp_free((PyObject*)self);
}