[3.13] gh-117657: Fix data races reported by TSAN in some set methods (GH-120914) (#121240)

Refactor the fast Unicode hash check into `_PyObject_HashFast` and use relaxed
atomic loads in the free-threaded build.

After this change, the TSAN doesn't report data races for this method.
(cherry picked from commit 294e724964)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-07-01 21:40:28 +02:00 committed by GitHub
parent fc0b1cbdfe
commit 06fd745dd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 81 additions and 121 deletions

View file

@ -365,13 +365,9 @@ set_discard_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
static int
set_add_key(PySetObject *so, PyObject *key)
{
Py_hash_t hash;
if (!PyUnicode_CheckExact(key) ||
(hash = _PyASCIIObject_CAST(key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
Py_hash_t hash = _PyObject_HashFast(key);
if (hash == -1) {
return -1;
}
return set_add_entry(so, key, hash);
}
@ -379,13 +375,9 @@ set_add_key(PySetObject *so, PyObject *key)
static int
set_contains_key(PySetObject *so, PyObject *key)
{
Py_hash_t hash;
if (!PyUnicode_CheckExact(key) ||
(hash = _PyASCIIObject_CAST(key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
Py_hash_t hash = _PyObject_HashFast(key);
if (hash == -1) {
return -1;
}
return set_contains_entry(so, key, hash);
}
@ -393,13 +385,9 @@ set_contains_key(PySetObject *so, PyObject *key)
static int
set_discard_key(PySetObject *so, PyObject *key)
{
Py_hash_t hash;
if (!PyUnicode_CheckExact(key) ||
(hash = _PyASCIIObject_CAST(key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
Py_hash_t hash = _PyObject_HashFast(key);
if (hash == -1) {
return -1;
}
return set_discard_entry(so, key, hash);
}