mirror of https://github.com/python/cpython
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>
This commit is contained in:
parent
ce3879bd45
commit
e16aed63f6
|
@ -401,7 +401,10 @@ PyAPI_FUNC(PyStatus) _PyInterpreterState_New(
|
||||||
#define RARE_EVENT_INTERP_INC(interp, name) \
|
#define RARE_EVENT_INTERP_INC(interp, name) \
|
||||||
do { \
|
do { \
|
||||||
/* saturating add */ \
|
/* saturating add */ \
|
||||||
if (interp->rare_events.name < UINT8_MAX) interp->rare_events.name++; \
|
int val = FT_ATOMIC_LOAD_UINT8_RELAXED(interp->rare_events.name); \
|
||||||
|
if (val < UINT8_MAX) { \
|
||||||
|
FT_ATOMIC_STORE_UINT8(interp->rare_events.name, val + 1); \
|
||||||
|
} \
|
||||||
RARE_EVENT_STAT_INC(name); \
|
RARE_EVENT_STAT_INC(name); \
|
||||||
} while (0); \
|
} while (0); \
|
||||||
|
|
||||||
|
|
|
@ -246,7 +246,11 @@ _Py_IsOwnedByCurrentThread(PyObject *ob)
|
||||||
|
|
||||||
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
|
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
|
||||||
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
|
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
|
||||||
|
#ifdef Py_GIL_DISABLED
|
||||||
|
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
|
||||||
|
#else
|
||||||
return ob->ob_type;
|
return ob->ob_type;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||||
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
|
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
|
||||||
|
@ -274,7 +278,11 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
|
||||||
|
|
||||||
|
|
||||||
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
|
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
|
||||||
|
#ifdef Py_GIL_DISABLED
|
||||||
|
_Py_atomic_store_ptr(&ob->ob_type, type);
|
||||||
|
#else
|
||||||
ob->ob_type = type;
|
ob->ob_type = type;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
|
||||||
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
|
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
|
||||||
|
|
|
@ -96,6 +96,32 @@ class TestType(TestCase):
|
||||||
|
|
||||||
self.run_one(writer_func, reader_func)
|
self.run_one(writer_func, reader_func)
|
||||||
|
|
||||||
|
def test___class___modification(self):
|
||||||
|
class Foo:
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Bar:
|
||||||
|
pass
|
||||||
|
|
||||||
|
thing = Foo()
|
||||||
|
def work():
|
||||||
|
foo = thing
|
||||||
|
for _ in range(10000):
|
||||||
|
foo.__class__ = Bar
|
||||||
|
type(foo)
|
||||||
|
foo.__class__ = Foo
|
||||||
|
type(foo)
|
||||||
|
|
||||||
|
|
||||||
|
threads = []
|
||||||
|
for i in range(NTHREADS):
|
||||||
|
thread = threading.Thread(target=work)
|
||||||
|
thread.start()
|
||||||
|
threads.append(thread)
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
def run_one(self, writer_func, reader_func):
|
def run_one(self, writer_func, reader_func):
|
||||||
writer = Thread(target=writer_func)
|
writer = Thread(target=writer_func)
|
||||||
readers = []
|
readers = []
|
||||||
|
|
|
@ -6633,9 +6633,15 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
|
||||||
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
||||||
Py_INCREF(newto);
|
Py_INCREF(newto);
|
||||||
}
|
}
|
||||||
|
Py_BEGIN_CRITICAL_SECTION(self);
|
||||||
|
// The real Py_TYPE(self) (`oldto`) may have changed from
|
||||||
|
// underneath us in another thread, so we re-fetch it here.
|
||||||
|
oldto = Py_TYPE(self);
|
||||||
Py_SET_TYPE(self, newto);
|
Py_SET_TYPE(self, newto);
|
||||||
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
|
Py_END_CRITICAL_SECTION();
|
||||||
|
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
||||||
Py_DECREF(oldto);
|
Py_DECREF(oldto);
|
||||||
|
}
|
||||||
|
|
||||||
RARE_EVENT_INC(set_class);
|
RARE_EVENT_INC(set_class);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -37,7 +37,6 @@ race_top:set_contains_key
|
||||||
# https://gist.github.com/colesbury/d13d033f413b4ad07929d044bed86c35
|
# https://gist.github.com/colesbury/d13d033f413b4ad07929d044bed86c35
|
||||||
race_top:set_discard_entry
|
race_top:set_discard_entry
|
||||||
race_top:set_inheritable
|
race_top:set_inheritable
|
||||||
race_top:Py_SET_TYPE
|
|
||||||
race_top:_PyDict_CheckConsistency
|
race_top:_PyDict_CheckConsistency
|
||||||
race_top:_Py_dict_lookup_threadsafe
|
race_top:_Py_dict_lookup_threadsafe
|
||||||
race_top:_multiprocessing_SemLock_acquire_impl
|
race_top:_multiprocessing_SemLock_acquire_impl
|
||||||
|
@ -58,7 +57,6 @@ race_top:_PyFrame_Initialize
|
||||||
race_top:PyInterpreterState_ThreadHead
|
race_top:PyInterpreterState_ThreadHead
|
||||||
race_top:_PyObject_TryGetInstanceAttribute
|
race_top:_PyObject_TryGetInstanceAttribute
|
||||||
race_top:PyThreadState_Next
|
race_top:PyThreadState_Next
|
||||||
race_top:Py_TYPE
|
|
||||||
race_top:PyUnstable_InterpreterFrame_GetLine
|
race_top:PyUnstable_InterpreterFrame_GetLine
|
||||||
race_top:sock_close
|
race_top:sock_close
|
||||||
race_top:tstate_delete_common
|
race_top:tstate_delete_common
|
||||||
|
|
Loading…
Reference in New Issue