https://github.com/python/cpython/commit/e5dfcea3e32f02cc740d7cb96f36f8327f3650bd
commit: e5dfcea3e32f02cc740d7cb96f36f8327f3650bd
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: corona10 <donghee.n...@gmail.com>
date: 2024-06-03T03:45:44Z
summary:

[3.13] gh-117657: Fix data races report by TSAN unicode-hash (gh-119907) 
(gh-119963)

gh-117657: Fix data races report by TSAN unicode-hash (gh-119907)
(cherry picked from commit 0594a27e5f1d87d59fa8a761dd8ca9df4e42816d)

Co-authored-by: Donghee Na <donghee...@python.org>

files:
M Objects/unicodeobject.c
M Tools/tsan/suppressions_free_threading.txt

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index c8a07288d397d2..240f4e76e2a0e1 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1625,7 +1625,7 @@ unicode_modifiable(PyObject *unicode)
     assert(_PyUnicode_CHECK(unicode));
     if (Py_REFCNT(unicode) != 1)
         return 0;
-    if (_PyUnicode_HASH(unicode) != -1)
+    if (FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(unicode)) != -1)
         return 0;
     if (PyUnicode_CHECK_INTERNED(unicode))
         return 0;
@@ -10819,9 +10819,10 @@ _PyUnicode_EqualToASCIIId(PyObject *left, 
_Py_Identifier *right)
     if (PyUnicode_CHECK_INTERNED(left))
         return 0;
 
-    assert(_PyUnicode_HASH(right_uni) != -1);
-    Py_hash_t hash = _PyUnicode_HASH(left);
-    if (hash != -1 && hash != _PyUnicode_HASH(right_uni)) {
+    Py_hash_t right_hash = 
FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(right_uni));
+    assert(right_hash != -1);
+    Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(left));
+    if (hash != -1 && hash != right_hash) {
         return 0;
     }
 
@@ -11306,12 +11307,14 @@ unicode_hash(PyObject *self)
 #ifdef Py_DEBUG
     assert(_Py_HashSecret_Initialized);
 #endif
-    if (_PyUnicode_HASH(self) != -1)
-        return _PyUnicode_HASH(self);
-
+    Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(self));
+    if (hash != -1) {
+        return hash;
+    }
     x = _Py_HashBytes(PyUnicode_DATA(self),
                       PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self));
-    _PyUnicode_HASH(self) = x;
+
+    FT_ATOMIC_STORE_SSIZE_RELAXED(_PyUnicode_HASH(self), x);
     return x;
 }
 
diff --git a/Tools/tsan/suppressions_free_threading.txt 
b/Tools/tsan/suppressions_free_threading.txt
index a451c9dd6bed31..60839e636a654c 100644
--- a/Tools/tsan/suppressions_free_threading.txt
+++ b/Tools/tsan/suppressions_free_threading.txt
@@ -49,7 +49,6 @@ race_top:set_discard_entry
 race_top:set_inheritable
 race_top:start_the_world
 race_top:tstate_set_detached
-race_top:unicode_hash
 race_top:Py_SET_TYPE
 race_top:_PyDict_CheckConsistency
 race_top:_PyImport_AcquireLock

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to