8000 [3.13] gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165… · python/cpython@91c4444 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91c4444

Browse files
miss-islingtonFidget-SpinnerpicnixzZheaoli
authored
[3.13] gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165) (GH-120403)
gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165) (cherry picked fr 8000 om commit e16aed6) Co-authored-by: Ken Jin <kenjin@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
1 parent 39825a7 commit 91c4444

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

Include/internal/pycore_interp.h

Lines changed: 4 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
331331

332332
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
333333
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
334+
#ifdef Py_GIL_DISABLED
335+
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
336+
#else
334337
return ob->ob_type;
338+
#endif
335339
}
336340
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
337341
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
@@ -421,7 +425,11 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
421425

422426

423427
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
428+
#ifdef Py_GIL_DISABLED
429+
_Py_atomic_store_ptr(&ob->ob_type, type);
430+
#else
424431
ob->ob_type = type;
432+
#endif
425433
}
426434
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
427435
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)

Lib/test/test_free_threading/test_type.py

Lines changed: 26 additions & 0 deletions
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

Copy file name to clipboard
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6474,9 +6474,15 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
64746474
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
64756475
Py_INCREF(newto);
64766476
}
6477+
Py_BEGIN_CRITICAL_SECTION(self);
6478+
// The real Py_TYPE(self) (`oldto`) may have changed from
6479+
// underneath us in another thread, so we re-fetch it here.
6480+
oldto = Py_TYPE(self);
64776481
Py_SET_TYPE(self, newto);
6478-
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
6482+
Py_END_CRITICAL_SECTION();
6483+
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
64796484
Py_DECREF(oldto);
6485+
}
64806486

64816487
RARE_EVENT_INC(set_class);
64826488
return 0;

Tools/tsan/suppressions_free_threading.txt

Lines changed: 0 additions & 2 deletions
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)
0