Skip to content

Commit

Permalink
pythongh-117657: Enable test_opcache under TSAN
Browse files Browse the repository at this point in the history
Fix a few thread-safety bugs to enable test_opcache when run with TSAN:

 * Use relaxed atomics when clearing `ht->_spec_cache.getitem`
   (pythongh-115999)
 * Add temporary suppression for type slot modifications (pythongh-127266)
 * Use atomic load when reading `*dictptr`
  • Loading branch information
colesbury committed Feb 7, 2025
1 parent 7b2e01b commit a6c8937
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions Lib/test/libregrtest/tsan.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'test_importlib',
'test_io',
'test_logging',
'test_opcache',
'test_queue',
'test_signal',
'test_socket',
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
else {
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
if (dictptr != NULL) {
dict = *dictptr;
dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr);
}
else {
dict = NULL;
Expand Down
6 changes: 4 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,8 @@ type_modified_unlocked(PyTypeObject *type)
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
// This field *must* be invalidated if the type is modified (see the
// comment on struct _specialization_cache):
((PyHeapTypeObject *)type)->_spec_cache.getitem = NULL;
FT_ATOMIC_STORE_PTR_RELAXED(
((PyHeapTypeObject *)type)->_spec_cache.getitem, NULL);
}
}

Expand Down Expand Up @@ -1166,7 +1167,8 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
// This field *must* be invalidated if the type is modified (see the
// comment on struct _specialization_cache):
((PyHeapTypeObject *)type)->_spec_cache.getitem = NULL;
FT_ATOMIC_STORE_PTR_RELAXED(
((PyHeapTypeObject *)type)->_spec_cache.getitem, NULL);
}
}

Expand Down
3 changes: 3 additions & 0 deletions Tools/tsan/suppressions_free_threading.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ race_top:rangeiter_next
# gh-129748: test.test_free_threading.test_slots.TestSlots.test_object
race_top:mi_block_set_nextx

# gh-127266: type slot updates are not thread-safe (test_opcache.test_load_attr_method_lazy_dict)
race_top:update_one_slot

# https://gist.github.com/mpage/6962e8870606cfc960e159b407a0cb40
thread:pthread_create

0 comments on commit a6c8937

Please sign in to comment.