Skip to content

Commit f151d27

Browse files
authored
gh-117657: Enable test_opcache under TSAN (GH-129831)
Fix a few thread-safety bugs to enable test_opcache when run with TSAN: * Use relaxed atomics when clearing `ht->_spec_cache.getitem` (gh-115999) * Add temporary suppression for type slot modifications (gh-127266) * Use atomic load when reading `*dictptr`
1 parent ed816f1 commit f151d27

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

Lib/test/libregrtest/tsan.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'test_importlib',
1414
'test_io',
1515
'test_logging',
16+
'test_opcache',
1617
'test_queue',
1718
'test_signal',
1819
'test_socket',

Objects/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
16121612
else {
16131613
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
16141614
if (dictptr != NULL) {
1615-
dict = *dictptr;
1615+
dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr);
16161616
}
16171617
else {
16181618
dict = NULL;

Objects/typeobject.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,8 @@ type_modified_unlocked(PyTypeObject *type)
10851085
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
10861086
// This field *must* be invalidated if the type is modified (see the
10871087
// comment on struct _specialization_cache):
1088-
((PyHeapTypeObject *)type)->_spec_cache.getitem = NULL;
1088+
FT_ATOMIC_STORE_PTR_RELAXED(
1089+
((PyHeapTypeObject *)type)->_spec_cache.getitem, NULL);
10891090
}
10901091
}
10911092

@@ -1166,7 +1167,8 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
11661167
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
11671168
// This field *must* be invalidated if the type is modified (see the
11681169
// comment on struct _specialization_cache):
1169-
((PyHeapTypeObject *)type)->_spec_cache.getitem = NULL;
1170+
FT_ATOMIC_STORE_PTR_RELAXED(
1171+
((PyHeapTypeObject *)type)->_spec_cache.getitem, NULL);
11701172
}
11711173
}
11721174

Tools/tsan/suppressions_free_threading.txt

+3
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ race_top:rangeiter_next
4949
# gh-129748: test.test_free_threading.test_slots.TestSlots.test_object
5050
race_top:mi_block_set_nextx
5151

52+
# gh-127266: type slot updates are not thread-safe (test_opcache.test_load_attr_method_lazy_dict)
53+
race_top:update_one_slot
54+
5255
# https://gist.github.com/mpage/6962e8870606cfc960e159b407a0cb40
5356
thread:pthread_create

0 commit comments

Comments
 (0)