GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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.

Sign up for GitHub

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

Closed
nascheme wants to merge 3 commits into python:main from nascheme:trashcan_begin_gc_untrack

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-44881: Integrate GC untrack into trashcan begin. #27718

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
d045bb6
Integrate GC untrack into trashcan begin.
nascheme Aug 10, 2021
42a10b4
small cleanup, move declarations after
nascheme Aug 11, 2021
7539849
Add check for GC protocol per @vstinner request
ambv Aug 16, 2021
File filter
Filter by extension

Clear filters
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
Diff view

There are no files selected for viewing

1 change: 0 additions & 1 deletion 1 Include/cpython/object.h
Show comments
View file
Open in desktop
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
9 changes: 9 additions & 0 deletions 9 Misc/NEWS.d/next/Core and Builtins/2021-08-10-14-46-10.bpo-44881.M1bcMW.rst
Show comments
View file
Open in desktop
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.
2 changes: 0 additions & 2 deletions 2 Modules/_elementtree.c
Show comments
View file
Open in desktop
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
1 change: 0 additions & 1 deletion 1 Objects/descrobject.c
Show comments
View file
Open in desktop
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
2 changes: 0 additions & 2 deletions 2 Objects/dictobject.c
Show comments
View file
Open in desktop
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
4 changes: 0 additions & 4 deletions 4 Objects/frameobject.c
Show comments
View file
Open in desktop
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
1 change: 0 additions & 1 deletion 1 Objects/listobject.c
Show comments
View file
Open in desktop
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
8 changes: 8 additions & 0 deletions 8 Objects/object.c
Show comments
View file
Open in desktop
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
1 change: 0 additions & 1 deletion 1 Objects/odictobject.c
Show comments
View file
Open in desktop
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
2 changes: 0 additions & 2 deletions 2 Objects/setobject.c
Show comments
View file
Open in desktop
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
1 change: 0 additions & 1 deletion 1 Objects/tupleobject.c
Show comments
View file
Open in desktop
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
40 changes: 0 additions & 40 deletions 40 Objects/typeobject.c
Show comments
View file
Open in desktop
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
2 changes: 0 additions & 2 deletions 2 Python/hamt.c
Show comments
View file
Open in desktop
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
1 change: 0 additions & 1 deletion 1 Python/traceback.c
Show comments
View file
Open in desktop
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.