Skip to content

Commit ad9cff9

Browse files
corona10barneygale
authored andcommitted
pythongh-117657: Fix data races report by TSAN unicode-hash (pythongh-119907)
1 parent e9cef42 commit ad9cff9

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

Objects/unicodeobject.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ unicode_modifiable(PyObject *unicode)
16331633
assert(_PyUnicode_CHECK(unicode));
16341634
if (Py_REFCNT(unicode) != 1)
16351635
return 0;
1636-
if (_PyUnicode_HASH(unicode) != -1)
1636+
if (FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(unicode)) != -1)
16371637
return 0;
16381638
if (PyUnicode_CHECK_INTERNED(unicode))
16391639
return 0;
@@ -10901,9 +10901,10 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
1090110901
if (PyUnicode_CHECK_INTERNED(left))
1090210902
return 0;
1090310903

10904-
assert(_PyUnicode_HASH(right_uni) != -1);
10905-
Py_hash_t hash = _PyUnicode_HASH(left);
10906-
if (hash != -1 && hash != _PyUnicode_HASH(right_uni)) {
10904+
Py_hash_t right_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(right_uni));
10905+
assert(right_hash != -1);
10906+
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(left));
10907+
if (hash != -1 && hash != right_hash) {
1090710908
return 0;
1090810909
}
1090910910

@@ -11388,12 +11389,14 @@ unicode_hash(PyObject *self)
1138811389
#ifdef Py_DEBUG
1138911390
assert(_Py_HashSecret_Initialized);
1139011391
#endif
11391-
if (_PyUnicode_HASH(self) != -1)
11392-
return _PyUnicode_HASH(self);
11393-
11392+
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(self));
11393+
if (hash != -1) {
11394+
return hash;
11395+
}
1139411396
x = _Py_HashBytes(PyUnicode_DATA(self),
1139511397
PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self));
11396-
_PyUnicode_HASH(self) = x;
11398+
11399+
FT_ATOMIC_STORE_SSIZE_RELAXED(_PyUnicode_HASH(self), x);
1139711400
return x;
1139811401
}
1139911402

Tools/tsan/suppressions_free_threading.txt

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ race_top:set_discard_entry
4949
race_top:set_inheritable
5050
race_top:start_the_world
5151
race_top:tstate_set_detached
52-
race_top:unicode_hash
5352
race_top:Py_SET_TYPE
5453
race_top:_PyDict_CheckConsistency
5554
race_top:_PyImport_AcquireLock

0 commit comments

Comments
 (0)