Skip to content

Commit e16aed6

Browse files
Fidget-SpinnerpicnixzZheaoli
authored
gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
1 parent ce3879b commit e16aed6

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

Include/internal/pycore_interp.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,10 @@ PyAPI_FUNC(PyStatus) _PyInterpreterState_New(
401401
#define RARE_EVENT_INTERP_INC(interp, name) \
402402
do { \
403403
/* saturating add */ \
404-
if (interp->rare_events.name < UINT8_MAX) interp->rare_events.name++; \
404+
int val = FT_ATOMIC_LOAD_UINT8_RELAXED(interp->rare_events.name); \
405+
if (val < UINT8_MAX) { \
406+
FT_ATOMIC_STORE_UINT8(interp->rare_events.name, val + 1); \
407+
} \
405408
RARE_EVENT_STAT_INC(name); \
406409
} while (0); \
407410

Include/object.h

+8
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ _Py_IsOwnedByCurrentThread(PyObject *ob)
246246

247247
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
248248
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
249+
#ifdef Py_GIL_DISABLED
250+
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
251+
#else
249252
return ob->ob_type;
253+
#endif
250254
}
251255
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
252256
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
@@ -274,7 +278,11 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
274278

275279

276280
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
281+
#ifdef Py_GIL_DISABLED
282+
_Py_atomic_store_ptr(&ob->ob_type, type);
283+
#else
277284
ob->ob_type = type;
285+
#endif
278286
}
279287
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
280288
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)

Lib/test/test_free_threading/test_type.py

+26
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,32 @@ def reader_func():
9696

9797
self.run_one(writer_func, reader_func)
9898

99+
def test___class___modification(self):
100+
class Foo:
101+
pass
102+
103+
class Bar:
104+
pass
105+
106+
thing = Foo()
107+
def work():
108+
foo = thing
109+
for _ in range(10000):
110+
foo.__class__ = Bar
111+
type(foo)
112+
foo.__class__ = Foo
113+
type(foo)
114+
115+
116+
threads = []
117+
for i in range(NTHREADS):
118+
thread = threading.Thread(target=work)
119+
thread.start()
120+
threads.append(thread)
121+
122+
for thread in threads:
123+
thread.join()
124+
99125
def run_one(self, writer_func, reader_func):
100126
writer = Thread(target=writer_func)
101127
readers = []

Objects/typeobject.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -6633,9 +6633,15 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
66336633
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
66346634
Py_INCREF(newto);
66356635
}
6636+
Py_BEGIN_CRITICAL_SECTION(self);
6637+
// The real Py_TYPE(self) (`oldto`) may have changed from
6638+
// underneath us in another thread, so we re-fetch it here.
6639+
oldto = Py_TYPE(self);
66366640
Py_SET_TYPE(self, newto);
6637-
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
6641+
Py_END_CRITICAL_SECTION();
6642+
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
66386643
Py_DECREF(oldto);
6644+
}
66396645

66406646
RARE_EVENT_INC(set_class);
66416647
return 0;

Tools/tsan/suppressions_free_threading.txt

-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ race_top:set_contains_key
3737
# https://gist.github.com/colesbury/d13d033f413b4ad07929d044bed86c35
3838
race_top:set_discard_entry
3939
race_top:set_inheritable
40-
race_top:Py_SET_TYPE
4140
race_top:_PyDict_CheckConsistency
4241
race_top:_Py_dict_lookup_threadsafe
4342
race_top:_multiprocessing_SemLock_acquire_impl
@@ -58,7 +57,6 @@ race_top:_PyFrame_Initialize
5857
race_top:PyInterpreterState_ThreadHead
5958
race_top:_PyObject_TryGetInstanceAttribute
6059
race_top:PyThreadState_Next
61-
race_top:Py_TYPE
6260
race_top:PyUnstable_InterpreterFrame_GetLine
6361
race_top:sock_close
6462
race_top:tstate_delete_common

0 commit comments

Comments
 (0)