8000 gh-113750: Fix object resurrection in free-threaded builds (gh-113751) · python/cpython@d0f0308 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0f0308

Browse files
authored
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.
1 parent 3375dfe commit d0f0308

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Include/cpython/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
66
PyAPI_FUNC(void) _Py_NewReferenceNoTotal(PyObject *op);
7+
PyAPI_FUNC(void) _Py_ResurrectReference(PyObject *op);
78

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

Modules/_testcapi/gc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ slot_tp_del(PyObject *self)
126126
* never happened.
127127
*/
128128
{
129-
Py_ssize_t refcnt = Py_REFCNT(self);
130-
_Py_NewReferenceNoTotal(self);
131-
Py_SET_REFCNT(self, refcnt);
129+
_Py_ResurrectReference(self);
132130
}
133131
assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
134132
}

Objects/object.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,7 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
509509

510510
/* tp_finalize resurrected it! Make it look like the original Py_DECREF
511511
* never happened. */
512-
Py_ssize_t refcnt = Py_REFCNT(self);
513-
_Py_NewReferenceNoTotal(self);
514-
Py_SET_REFCNT(self, refcnt);
512+
_Py_ResurrectReference(self);
515513

516514
_PyObject_ASSERT(self,
517515
(!_PyType_IS_GC(Py_TYPE(self))
@@ -2389,6 +2387,17 @@ _Py_NewReferenceNoTotal(PyObject *op)
23892387
new_reference(op);
23902388
}
23912389

2390+
void
2391+
_Py_ResurrectReference(PyObject *op)
2392+
{
2393+
if (_PyRuntime.tracemalloc.config.tracing) {
2394+
_PyTraceMalloc_NewReference(op);
2395+
}
2396+
#ifdef Py_TRACE_REFS
2397+
_Py_AddToAllObjects(op);
2398+
#endif
2399+
}
2400+
23922401

23932402
#ifdef Py_TRACE_REFS
23942403
void

0 commit comments

Comments
 (0)
0