Skip to content

GH-133261: Make sure that the GC doesn't untrack objects in trashcan #133431

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

Merged
merged 5 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug where the cycle GC could untrack objects in the trashcan because
they looked like they were immortal. When objects are added to the trashcan,
we take care to ensure they keep a mortal reference count.
44 changes: 38 additions & 6 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2930,6 +2930,39 @@

/* Trashcan support. */

/* We need to store a pointer in the refcount field of
* an object. It is important that we never store 0 (NULL).
* It is also important to not make the object appear immortal,
* or it might be untracked by the cycle GC. */
static uintptr_t
pointer_to_safe_refcount(void *ptr)

Check warning on line 2938 in Objects/object.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

‘pointer_to_safe_refcount’ defined but not used [-Wunused-function]

Check warning on line 2938 in Objects/object.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

‘pointer_to_safe_refcount’ defined but not used [-Wunused-function]
{
uintptr_t full = (uintptr_t)ptr;
assert((full & 3) == 0);
#if defined(Py_GIL_DISABLED)
return full + 1;
#else
uint32_t refcnt = (uint32_t)full;
if (refcnt >= (uint32_t)_Py_IMMORTAL_MINIMUM_REFCNT) {
full = full - ((uintptr_t)_Py_IMMORTAL_MINIMUM_REFCNT) + 1;
}
return full + 2;
#endif
}

static void *
safe_refcount_to_pointer(uintptr_t refcnt)

Check warning on line 2954 in Objects/object.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

‘safe_refcount_to_pointer’ defined but not used [-Wunused-function]

Check warning on line 2954 in Objects/object.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

‘safe_refcount_to_pointer’ defined but not used [-Wunused-function]
{
#if defined(Py_GIL_DISABLED)
return (void *)(refcnt - 1);
#else
if (refcnt & 1) {
refcnt += _Py_IMMORTAL_MINIMUM_REFCNT - 1;
}
return (void *)(refcnt - 2);
#endif
}

/* Add op to the gcstate->trash_delete_later list. Called when the current
* call-stack depth gets large. op must be a currently untracked gc'ed
* object, with refcount 0. Py_DECREF must already have been called on it.
Expand All @@ -2941,11 +2974,10 @@
#ifdef Py_GIL_DISABLED
op->ob_tid = (uintptr_t)tstate->delete_later;
#else
/* Store the delete_later pointer in the refcnt field.
* As this object may still be tracked by the GC,
* it is important that we never store 0 (NULL). */
uintptr_t refcnt = (uintptr_t)tstate->delete_later;
*((uintptr_t*)op) = refcnt+1;
/* Store the delete_later pointer in the refcnt field. */
uintptr_t refcnt = pointer_to_safe_refcount(tstate->delete_later);
*((uintptr_t*)op) = refcnt;
assert(!_Py_IsImmortal(op));
#endif
tstate->delete_later = op;
}
Expand All @@ -2967,7 +2999,7 @@
/* Get the delete_later pointer from the refcnt field.
* See _PyTrash_thread_deposit_object(). */
uintptr_t refcnt = *((uintptr_t*)op);
tstate->delete_later = (PyObject *)(refcnt - 1);
tstate->delete_later = safe_refcount_to_pointer(refcnt);
op->ob_refcnt = 0;
#endif

Expand Down
1 change: 1 addition & 0 deletions Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ update_refs(PyGC_Head *containers)
next = GC_NEXT(gc);
PyObject *op = FROM_GC(gc);
if (_Py_IsImmortal(op)) {
assert(!_Py_IsStaticImmortal(op));
_PyObject_GC_UNTRACK(op);
gc = next;
continue;
Expand Down
Loading