gh-113750: Fix object resurrection in free-threaded builds (gh-113751)

gh-113750: Fix object resurrection on free-threaded builds

This avoids the undesired re-initializing of fields like `ob_gc_bits`,
`ob_mutex`, and `ob_tid` when an object is resurrected due to its
finalizer being called.

This change has no effect on the default (with GIL) build.
This commit is contained in:
Sam Gross 2024-01-05 22:12:26 -05:00 committed by GitHub
parent 3375dfed40
commit d0f0308a37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -4,6 +4,7 @@
PyAPI_FUNC(void) _Py_NewReference(PyObject *op); PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
PyAPI_FUNC(void) _Py_NewReferenceNoTotal(PyObject *op); PyAPI_FUNC(void) _Py_NewReferenceNoTotal(PyObject *op);
PyAPI_FUNC(void) _Py_ResurrectReference(PyObject *op);
#ifdef Py_REF_DEBUG #ifdef Py_REF_DEBUG
/* These are useful as debugging aids when chasing down refleaks. */ /* These are useful as debugging aids when chasing down refleaks. */

View File

@ -126,9 +126,7 @@ slot_tp_del(PyObject *self)
* never happened. * never happened.
*/ */
{ {
Py_ssize_t refcnt = Py_REFCNT(self); _Py_ResurrectReference(self);
_Py_NewReferenceNoTotal(self);
Py_SET_REFCNT(self, refcnt);
} }
assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self)); assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
} }

View File

@ -509,9 +509,7 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
/* tp_finalize resurrected it! Make it look like the original Py_DECREF /* tp_finalize resurrected it! Make it look like the original Py_DECREF
* never happened. */ * never happened. */
Py_ssize_t refcnt = Py_REFCNT(self); _Py_ResurrectReference(self);
_Py_NewReferenceNoTotal(self);
Py_SET_REFCNT(self, refcnt);
_PyObject_ASSERT(self, _PyObject_ASSERT(self,
(!_PyType_IS_GC(Py_TYPE(self)) (!_PyType_IS_GC(Py_TYPE(self))
@ -2389,6 +2387,17 @@ _Py_NewReferenceNoTotal(PyObject *op)
new_reference(op); new_reference(op);
} }
void
_Py_ResurrectReference(PyObject *op)
{
if (_PyRuntime.tracemalloc.config.tracing) {
_PyTraceMalloc_NewReference(op);
}
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op);
#endif
}
#ifdef Py_TRACE_REFS #ifdef Py_TRACE_REFS
void void