Skip to content

Commit 235490e

Browse files
committed
Make sure that the GC never sees objects with refcount of zero
1 parent 6ec46fa commit 235490e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Objects/object.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,7 +2912,11 @@ _PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op)
29122912
#ifdef Py_GIL_DISABLED
29132913
op->ob_tid = (uintptr_t)tstate->delete_later;
29142914
#else
2915-
*((PyObject**)op) = tstate->delete_later;
2915+
/* Store the pointer in the refcnt field.
2916+
* As this object may still be tracked by the GC,
2917+
* it is important that we never store 0 (NULL). */
2918+
uintptr_t refcnt = (uintptr_t)tstate->delete_later;
2919+
*((uintptr_t*)op) = refcnt+1;
29162920
#endif
29172921
tstate->delete_later = op;
29182922
}
@@ -2931,7 +2935,8 @@ _PyTrash_thread_destroy_chain(PyThreadState *tstate)
29312935
op->ob_tid = 0;
29322936
_Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, _Py_REF_MERGED);
29332937
#else
2934-
tstate->delete_later = *((PyObject**)op);
2938+
uintptr_t refcnt = *((uintptr_t*)op);
2939+
tstate->delete_later = (PyObject *)(refcnt - 1);
29352940
op->ob_refcnt = 0;
29362941
#endif
29372942

0 commit comments

Comments
 (0)