Skip to content

Commit f963239

Browse files
authored
gh-130202: Fix bug in _PyObject_ResurrectEnd in free threaded build (gh-130281)
This fixes a fairly subtle bug involving finalizers and resurrection in debug free threaded builds: if `_PyObject_ResurrectEnd` returns `1` (i.e., the object was resurrected by a finalizer), it's not safe to access the object because it might still be deallocated. For example: * The finalizer may have exposed the object to another thread. That thread may hold the last reference and concurrently deallocate it any time after `_PyObject_ResurrectEnd()` returns `1`. * `_PyObject_ResurrectEnd()` may call `_Py_brc_queue_object()`, which may internally deallocate the object immediately if the owning thread is dead. Therefore, it's important not to access the object after it's resurrected. We only violate this in two cases, and only in debug builds: * We assert that the object is tracked appropriately. This is now moved up betewen the finalizer and the `_PyObject_ResurrectEnd()` call. * The `--with-trace-refs` builds may need to remember the object if it's resurrected. This is now handled by `_PyObject_ResurrectStart()` and `_PyObject_ResurrectEnd()`. Note that `--with-trace-refs` is currently disabled in `--disable-gil` builds because the refchain hash table isn't thread-safe, but this refactoring avoids an additional thread-safety issue.
1 parent c5f925c commit f963239

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

Include/cpython/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
66
PyAPI_FUNC(void) _Py_NewReferenceNoTotal(PyObject *op);
77
PyAPI_FUNC(void) _Py_ResurrectReference(PyObject *op);
8+
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *op);
89

910
#ifdef Py_REF_DEBUG
1011
/* These are useful as debugging aids when chasing down refleaks. */

Include/internal/pycore_object.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ _PyObject_ResurrectStart(PyObject *op)
730730
#else
731731
Py_SET_REFCNT(op, 1);
732732
#endif
733+
#ifdef Py_TRACE_REFS
734+
_Py_ResurrectReference(op);
735+
#endif
733736
}
734737

735738
// Undoes an object resurrection by decrementing the refcount without calling
@@ -743,13 +746,22 @@ _PyObject_ResurrectEnd(PyObject *op)
743746
#endif
744747
#ifndef Py_GIL_DISABLED
745748
Py_SET_REFCNT(op, Py_REFCNT(op) - 1);
746-
return Py_REFCNT(op) != 0;
749+
if (Py_REFCNT(op) == 0) {
750+
# ifdef Py_TRACE_REFS
751+
_Py_ForgetReference(op);
752+
# endif
753+
return 0;
754+
}
755+
return 1;
747756
#else
748757
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local);
749758
Py_ssize_t shared = _Py_atomic_load_ssize_acquire(&op->ob_ref_shared);
750759
if (_Py_IsOwnedByCurrentThread(op) && local == 1 && shared == 0) {
751760
// Fast-path: object has a single refcount and is owned by this thread
752761
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0);
762+
# ifdef Py_TRACE_REFS
763+
_Py_ForgetReference(op);
764+
# endif
753765
return 0;
754766
}
755767
// Slow-path: object has a shared refcount or is not owned by this thread

Objects/object.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,22 @@ _PyObject_ResurrectEndSlow(PyObject *op)
496496
// merge the refcount. This isn't necessary in all cases, but it
497497
// simplifies the implementation.
498498
Py_ssize_t refcount = _Py_ExplicitMergeRefcount(op, -1);
499-
return refcount != 0;
499+
if (refcount == 0) {
500+
#ifdef Py_TRACE_REFS
501+
_Py_ForgetReference(op);
502+
#endif
503+
return 0;
504+
}
505+
return 1;
500506
}
501507
int is_dead = _Py_DecRefSharedIsDead(op, NULL, 0);
502-
return !is_dead;
508+
if (is_dead) {
509+
#ifdef Py_TRACE_REFS
510+
_Py_ForgetReference(op);
511+
#endif
512+
return 0;
513+
}
514+
return 1;
503515
}
504516

505517

@@ -589,20 +601,24 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
589601
Py_REFCNT(self) > 0,
590602
"refcount is too small");
591603

604+
_PyObject_ASSERT(self,
605+
(!_PyType_IS_GC(Py_TYPE(self))
606+
|| _PyObject_GC_IS_TRACKED(self)));
607+
592608
/* Undo the temporary resurrection; can't use DECREF here, it would
593609
* cause a recursive call. */
594-
if (!_PyObject_ResurrectEnd(self)) {
595-
return 0; /* this is the normal path out */
610+
if (_PyObject_ResurrectEnd(self)) {
611+
/* tp_finalize resurrected it!
612+
gh-130202: Note that the object may still be dead in the free
613+
threaded build in some circumstances, so it's not safe to access
614+
`self` after this point. For example, the last reference to the
615+
resurrected `self` may be held by another thread, which can
616+
concurrently deallocate it. */
617+
return -1;
596618
}
597619

598-
/* tp_finalize resurrected it! Make it look like the original Py_DECREF
599-
* never happened. */
600-
_Py_ResurrectReference(self);
601-
602-
_PyObject_ASSERT(self,
603-
(!_PyType_IS_GC(Py_TYPE(self))
604-
|| _PyObject_GC_IS_TRACKED(self)));
605-
return -1;
620+
/* this is the normal path out, the caller continues with deallocation. */
621+
return 0;
606622
}
607623

608624
int
@@ -2601,11 +2617,10 @@ _Py_ResurrectReference(PyObject *op)
26012617
#endif
26022618
}
26032619

2604-
2605-
#ifdef Py_TRACE_REFS
26062620
void
26072621
_Py_ForgetReference(PyObject *op)
26082622
{
2623+
#ifdef Py_TRACE_REFS
26092624
if (Py_REFCNT(op) < 0) {
26102625
_PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
26112626
}
@@ -2621,8 +2636,11 @@ _Py_ForgetReference(PyObject *op)
26212636
#endif
26222637

26232638
_PyRefchain_Remove(interp, op);
2639+
#endif
26242640
}
26252641

2642+
2643+
#ifdef Py_TRACE_REFS
26262644
static int
26272645
_Py_PrintReference(_Py_hashtable_t *ht,
26282646
const void *key, const void *value,

0 commit comments

Comments
 (0)