8000 add refcnt check in `FT_CLEAR_WEAKREFS` · python/cpython@3a91299 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a91299

Browse files
committed
add refcnt check in FT_CLEAR_WEAKREFS
1 parent 42a0743 commit 3a91299

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

Include/internal/pycore_weakref.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ extern "C" {
2929
PyMutex_LockFlags(wr->weakrefs_lock, _Py_LOCK_DONT_DETACH)
3030
#define UNLOCK_WEAKREFS_FOR_WR(wr) PyMutex_Unlock(wr->weakrefs_lock)
3131

32-
#define FT_CLEAR_WEAKREFS(obj, weakref_list) \
33-
PyObject_ClearWeakRefs(obj)
32+
#define FT_CLEAR_WEAKREFS(obj, weakref_list) \
33+
do { \
34+
assert(Py_REFCNT(obj) == 0); \
35+
PyObject_ClearWeakRefs(obj); \
36+
} while (0)
3437

3538
#else
3639

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Fix potential :mod:`weakref` races on objects dealloc on the :term:`free threaded <free
1+
Fix potential :mod:`weakref` races in an object's destructor on the :term:`free threaded <free
22
threading>` build.

Modules/_sre/sre.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,7 @@ pattern_dealloc(PyObject *self)
737737
{
738738
PyTypeObject *tp = Py_TYPE(self);
739739
PyObject_GC_UnTrack(self);
740-
PatternObject *obj = _PatternObject_CAST(self);
741-
FT_CLEAR_WEAKREFS(self, obj->weakreflist);
740+
FT_CLEAR_WEAKREFS(self, _PatternObject_CAST(self)->weakreflist);
742741
(void)pattern_clear(self);
743742
tp->tp_free(self);
744743
Py_DECREF(tp);

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ static void
13651365
localdummy_dealloc(PyObject *op)
13661366
{
13671367
localdummyobject *self = localdummyobject_CAST(op);
1368-
PyObject_ClearWeakRefs(op);
1368+
FT_CLEAR_WEAKREFS(op, self->weakreflist);
13691369
PyTypeObject *tp = Py_TYPE(self);
13701370
tp->tp_free(self);
13711371
Py_DECREF(tp);

0 commit comments

Comments
 (0)
0