GeezerVison is showing github.com — bpo-44881: Integrate GC untrack into trashcan begin. by nascheme · Pull Request #27718 · python/cpython
bpo-44881: Integrate GC untrack into trashcan begin.
#27718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.c
(12)
.h
(1)
.rst
(1)
All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -471,7 +471,6 @@ mytype_dealloc(mytype *p)
{
... declarations go here ...
PyObject_GC_UnTrack(p); // must untrack first
Py_TRASHCAN_BEGIN(p, mytype_dealloc)
... The body of the deallocator goes here, including all calls ...
... to Py_DECREF on contained objects. ...
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,9 @@
Change `Py_TRASHCAN_BEGIN()` to automatically call `PyObject_GC_UnTrack()`.
Previously, the untrack function must be explicitly called before
`Py_TRASHCAN_BEGIN()`. There have been a number of bugs over the years
related to not doing this particular dance just right. This change makes it
harder to do things incorrectly. That avoids some hard to find bugs (e.g.
only triggered when object nesting gets deep enough). Extensions that still
call `PyObject_GC_UnTrack()` explictly will still work correctly but the
call is redundant after this change. It would still be needed for the
extension to work correctly with both older and newer versions of Python.
Original file line number
Diff line number
Diff line change
Expand Up
@@ -664,8 +664,6 @@ element_gc_clear(ElementObject *self)
static void
element_dealloc(ElementObject* self)
{
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, element_dealloc)
if (self->weakreflist != NULL)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1265,7 +1265,6 @@ typedef struct {
static void
wrapper_dealloc(wrapperobject *wp)
{
PyObject_GC_UnTrack(wp);
Py_TRASHCAN_BEGIN(wp, wrapper_dealloc)
Py_XDECREF(wp->descr);
Py_XDECREF(wp->self);
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1928,8 +1928,6 @@ dict_dealloc(PyDictObject *mp)
PyDictKeysObject *keys = mp->ma_keys;
Py_ssize_t i, n;
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(mp);
Py_TRASHCAN_BEGIN(mp, dict_dealloc)
if (values != NULL) {
if (values != empty_values) {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -615,10 +615,6 @@ static PyGetSetDef frame_getsetlist[] = {
static void _Py_HOT_FUNCTION
frame_dealloc(PyFrameObject *f)
{
if (_PyObject_GC_IS_TRACKED(f)) {
_PyObject_GC_UNTRACK(f);
}
Py_TRASHCAN_BEGIN(f, frame_dealloc);
PyCodeObject *co = NULL;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -331,7 +331,6 @@ static void
list_dealloc(PyListObject *op)
{
Py_ssize_t i;
PyObject_GC_UnTrack(op);
Py_TRASHCAN_BEGIN(op, list_dealloc)
if (op->ob_item != NULL) {
/* Do it backwards, for Christian Tismer.
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2101,6 +2101,11 @@ _PyTrash_thread_deposit_object(PyObject *op)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyObject_ASSERT(op, _PyObject_IS_GC(op));
/* untrack is done by _PyTrash_begin() so the following assert must be
* true. Previously _PyTrash_begin() did not untrack itself and it was the
* responsibility of the users of the trashcan mechanism to ensure that
* untrack was called first.
*/
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
_PyObject_ASSERT(op, Py_REFCNT(op) == 0);
_PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Expand Down
Expand Up
@@ -2150,6 +2155,9 @@ _PyTrash_thread_destroy_chain(void)
int
_PyTrash_begin(PyThreadState *tstate, PyObject *op)
{
Comment thread
ambv marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
if (_PyObject_GC_IS_TRACKED(op)) {
_PyObject_GC_UNTRACK(op);
}
if (tstate->trash_delete_nesting >= _PyTrash_UNWIND_LEVEL) {
/* Store the object (to be deallocated later) and jump past
* Py_TRASHCAN_END, skipping the body of the deallocator */
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1365,7 +1365,6 @@ static PyGetSetDef odict_getset[] = {
static void
odict_dealloc(PyODictObject *self)
{
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, odict_dealloc)
Py_XDECREF(self->od_inst_dict);
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -485,8 +485,6 @@ set_dealloc(PySetObject *so)
setentry *entry;
Py_ssize_t used = so->used;
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(so);
Py_TRASHCAN_BEGIN(so, set_dealloc)
if (so->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) so);
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -263,7 +263,6 @@ static void
tupledealloc(PyTupleObject *op)
{
Py_ssize_t len = Py_SIZE(op);
PyObject_GC_UnTrack(op);
Py_TRASHCAN_BEGIN(op, tupledealloc)
if (len > 0) {
Py_ssize_t i = len;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1381,9 +1381,6 @@ subtype_dealloc(PyObject *self)
/* We get here only if the type has GC */
/* UnTrack and re-Track around the trashcan macro, alas */
/* See explanation at end of function for full disclosure */
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, subtype_dealloc);
/* Find the nearest base with a different tp_dealloc */
Expand Down
Expand Up
@@ -1487,43 +1484,6 @@ subtype_dealloc(PyObject *self)
endlabel:
Py_TRASHCAN_END
/* Explanation of the weirdness around the trashcan macros:
Q. What do the trashcan macros do?
A. Read the comment titled "Trashcan mechanism" in object.h.
For one, this explains why there must be a call to GC-untrack
before the trashcan begin macro. Without understanding the
trashcan code, the answers to the following questions don't make
sense.
Q. Why do we GC-untrack before the trashcan and then immediately
GC-track again afterward?
A. In the case that the base class is GC-aware, the base class
probably GC-untracks the object. If it does that using the
UNTRACK macro, this will crash when the object is already
untracked. Because we don't know what the base class does, the
only safe thing is to make sure the object is tracked when we
call the base class dealloc. But... The trashcan begin macro
requires that the object is *untracked* before it is called. So
the dance becomes:
GC untrack
trashcan begin
GC track
Q. Why did the last question say "immediately GC-track again"?
It's nowhere near immediately.
A. Because the code *used* to re-track immediately. Bad Idea.
self has a refcount of 0, and if gc ever gets its hands on it
(which can happen if any weakref callback gets invoked), it
looks like trash to gc too, and gc also tries to delete self
then. But we're already deleting self. Double deallocation is
a subtle disaster.
*/
}
static PyTypeObject *solid_base(PyTypeObject *type);
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1155,7 +1155,6 @@ hamt_node_bitmap_dealloc(PyHamtNode_Bitmap *self)
Py_ssize_t len = Py_SIZE(self);
Py_ssize_t i;
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, hamt_node_bitmap_dealloc)
if (len > 0) {
Expand Down
Expand Up
@@ -1563,7 +1562,6 @@ hamt_node_collision_dealloc(PyHamtNode_Collision *self)
Py_ssize_t len = Py_SIZE(self);
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, hamt_node_collision_dealloc)
if (len > 0) {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -168,7 +168,6 @@ static PyGetSetDef tb_getsetters[] = {
static void
tb_dealloc(PyTracebackObject *tb)
{
PyObject_GC_UnTrack(tb);
Py_TRASHCAN_BEGIN(tb, tb_dealloc)
Py_XDECREF(tb->tb_next);
Py_XDECREF(tb->tb_frame);
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.