Skip to content

[3.13] gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614) #136126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Include/internal/pycore_weakref.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ extern "C" {
PyMutex_LockFlags(wr->weakrefs_lock, _Py_LOCK_DONT_DETACH)
#define UNLOCK_WEAKREFS_FOR_WR(wr) PyMutex_Unlock(wr->weakrefs_lock)

#define FT_CLEAR_WEAKREFS(obj, weakref_list) \
do { \
assert(Py_REFCNT(obj) == 0); \
PyObject_ClearWeakRefs(obj); \
} while (0)

#else

#define LOCK_WEAKREFS(obj)
Expand All @@ -37,6 +43,14 @@ extern "C" {
#define LOCK_WEAKREFS_FOR_WR(wr)
#define UNLOCK_WEAKREFS_FOR_WR(wr)

#define FT_CLEAR_WEAKREFS(obj, weakref_list) \
do { \
assert(Py_REFCNT(obj) == 0); \
if (weakref_list != NULL) { \
PyObject_ClearWeakRefs(obj); \
} \
} while (0)

#endif

static inline int _is_dead(PyObject *obj)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix potential :mod:`weakref` races in an object's destructor on the :term:`free threaded <free
threading>` build.
4 changes: 2 additions & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pyatomic_ft_wrappers.h"
#include "pycore_typeobject.h" // _PyType_GetModuleState()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h>

Expand Down Expand Up @@ -1521,8 +1522,7 @@ deque_dealloc(dequeobject *deque)
Py_ssize_t i;

PyObject_GC_UnTrack(deque);
if (deque->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) deque);
FT_CLEAR_WEAKREFS((PyObject*)deque, deque->weakreflist);
if (deque->leftblock != NULL) {
deque_clear(deque);
assert(deque->leftblock != NULL);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Python.h"
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
#include "pycore_pyhash.h" // _Py_HashSecret
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
#include "expat.h"
Expand Down Expand Up @@ -688,8 +689,7 @@ element_dealloc(ElementObject* self)
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, element_dealloc)

if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject*)self, self->weakreflist);

/* element_gc_clear clears all references and deallocates extra
*/
Expand Down
9 changes: 3 additions & 6 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pycore_object.h" // _PyObject_GC_TRACK
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()


#include "clinic/_functoolsmodule.c.h"
Expand Down Expand Up @@ -189,9 +190,7 @@ partial_dealloc(partialobject *pto)
PyTypeObject *tp = Py_TYPE(pto);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(pto);
if (pto->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) pto);
}
FT_CLEAR_WEAKREFS((PyObject*)pto, pto->weakreflist);
(void)partial_clear(pto);
tp->tp_free(pto);
Py_DECREF(tp);
Expand Down Expand Up @@ -1317,9 +1316,7 @@ lru_cache_dealloc(lru_cache_object *obj)
PyTypeObject *tp = Py_TYPE(obj);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(obj);
if (obj->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)obj);
}
FT_CLEAR_WEAKREFS((PyObject*)obj, obj->weakreflist);

(void)lru_cache_tp_clear(obj);
tp->tp_free(obj);
Expand Down
7 changes: 3 additions & 4 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include "_iomodule.h"

Expand Down Expand Up @@ -416,8 +417,7 @@ buffered_dealloc(buffered *self)
return;
_PyObject_GC_UNTRACK(self);
self->ok = 0;
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
FT_CLEAR_WEAKREFS((PyObject*)self, self->weakreflist);
if (self->buffer) {
PyMem_Free(self->buffer);
self->buffer = NULL;
Expand Down Expand Up @@ -2299,8 +2299,7 @@ bufferedrwpair_dealloc(rwpair *self)
{
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
FT_CLEAR_WEAKREFS((PyObject *)self, self->weakreflist);
(void)bufferedrwpair_clear(self);
tp->tp_free((PyObject *) self);
Py_DECREF(tp);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Python.h"
#include "pycore_object.h"
#include "pycore_sysmodule.h" // _PySys_GetSizeOf()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
#include "_iomodule.h"
Expand Down Expand Up @@ -894,8 +895,7 @@ bytesio_dealloc(bytesio *self)
}
Py_CLEAR(self->buf);
Py_CLEAR(self->dict);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stdbool.h> // bool
#ifdef HAVE_UNISTD_H
Expand Down Expand Up @@ -545,8 +546,7 @@ fileio_dealloc(fileio *self)
if (_PyIOBase_finalize((PyObject *) self) < 0)
return;
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
(void)fileio_clear(self);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_object.h" // _PyType_HasFeature()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
#include "_iomodule.h"
Expand Down Expand Up @@ -376,8 +377,7 @@ iobase_dealloc(iobase *self)
}
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
Py_CLEAR(self->dict);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
Expand Down
5 changes: 2 additions & 3 deletions Modules/_io/stringio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Python.h"
#include <stddef.h> // offsetof()
#include "pycore_object.h"
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
#include "_iomodule.h"

/* Implementation note: the buffer is always at least one character longer
Expand Down Expand Up @@ -617,9 +618,7 @@ stringio_dealloc(stringio *self)
}
_PyUnicodeWriter_Dealloc(&self->writer);
(void)stringio_clear(self);
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) self);
}
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include "_iomodule.h"

Expand Down Expand Up @@ -1460,8 +1461,7 @@ textiowrapper_dealloc(textio *self)
return;
self->ok = 0;
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
FT_CLEAR_WEAKREFS((PyObject *)self, self->weakreflist);
(void)textiowrapper_clear(self);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#ifdef HAVE_WINDOWS_CONSOLE_IO

Expand Down Expand Up @@ -514,8 +515,7 @@ winconsoleio_dealloc(winconsoleio *self)
if (_PyIOBase_finalize((PyObject *) self) < 0)
return;
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
Py_CLEAR(self->dict);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_parking_lot.h"
#include "pycore_time.h" // _PyTime_FromSecondsObject()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stdbool.h>
#include <stddef.h> // offsetof()
Expand Down Expand Up @@ -217,8 +218,7 @@ simplequeue_dealloc(simplequeueobject *self)

PyObject_GC_UnTrack(self);
(void)simplequeue_clear(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
}
Expand Down
5 changes: 2 additions & 3 deletions Modules/_sqlite/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "blob.h"
#include "util.h"
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
#include "clinic/blob.c.h"
Expand Down Expand Up @@ -51,9 +52,7 @@ blob_dealloc(pysqlite_Blob *self)

close_blob(self);

if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
}
FT_CLEAR_WEAKREFS((PyObject*)self, self->in_weakreflist);
tp->tp_clear((PyObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
Expand Down
5 changes: 2 additions & 3 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "util.h"

#include "pycore_pyerrors.h" // _PyErr_FormatFromCause()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

typedef enum {
TYPE_LONG,
Expand Down Expand Up @@ -180,9 +181,7 @@ cursor_dealloc(pysqlite_Cursor *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
}
FT_CLEAR_WEAKREFS((PyObject*)self, self->in_weakreflist);
tp->tp_clear((PyObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
Expand Down
5 changes: 2 additions & 3 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const char copyright[] =
#include "pycore_dict.h" // _PyDict_Next()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include "sre.h" // SRE_CODE

Expand Down Expand Up @@ -729,9 +730,7 @@ pattern_dealloc(PatternObject* self)
PyTypeObject *tp = Py_TYPE(self);

PyObject_GC_UnTrack(self);
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) self);
}
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
(void)pattern_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "pycore_bytesobject.h" // _PyBytesWriter
#include "pycore_long.h" // _PyLong_AsByteArray()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()

Expand Down Expand Up @@ -1639,8 +1640,7 @@ s_dealloc(PyStructObject *s)
{
PyTypeObject *tp = Py_TYPE(s);
PyObject_GC_UnTrack(s);
if (s->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)s);
FT_CLEAR_WEAKREFS((PyObject *)s, s->weakreflist);
if (s->s_codes != NULL) {
PyMem_Free(s->s_codes);
}
Expand Down
3 changes: 1 addition & 2 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,7 @@ typedef struct {
static void
localdummy_dealloc(localdummyobject *self)
{
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
FT_CLEAR_WEAKREFS((PyObject *) self, self->weakreflist);
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject*)self);
Py_DECREF(tp);
Expand Down
5 changes: 2 additions & 3 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED()
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include "datetime.h" // PyDateTime_TZInfo

Expand Down Expand Up @@ -368,9 +369,7 @@ zoneinfo_dealloc(PyObject *obj_self)
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);

if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs(obj_self);
}
FT_CLEAR_WEAKREFS(obj_self, self->weakreflist);

if (self->trans_list_utc != NULL) {
PyMem_Free(self->trans_list_utc);
Expand Down
4 changes: 2 additions & 2 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
#include <stdbool.h>
Expand Down Expand Up @@ -708,8 +709,7 @@ array_dealloc(arrayobject *op)
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);

if (op->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) op);
FT_CLEAR_WEAKREFS((PyObject *) op, op->weakreflist);
if (op->ob_item != NULL)
PyMem_Free(op->ob_item);
tp->tp_free(op);
Expand Down
4 changes: 2 additions & 2 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t()
#include "pycore_bytesobject.h" // _PyBytes_Find()
#include "pycore_fileutils.h" // _Py_stat_struct
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
#ifndef MS_WINDOWS
Expand Down Expand Up @@ -161,8 +162,7 @@ mmap_object_dealloc(mmap_object *m_obj)
Py_END_ALLOW_THREADS
#endif /* UNIX */

if (m_obj->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) m_obj);
FT_CLEAR_WEAKREFS((PyObject *) m_obj, m_obj->weakreflist);

tp->tp_free(m_obj);
Py_DECREF(tp);
Expand Down
Loading
Loading