gh-154037: Fix race in the hash(Decimal) (#154045)

kumaraditya303 <[email protected]> Thu, 30 Jul 2026 11:03:15 -0400 (EDT)
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/2f381f5a90474d9dad12e1e4946f348dd4b513bc
commit: 2f381f5a90474d9dad12e1e4946f348dd4b513bc
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-07-30T15:02:59Z
summary:

gh-154037: Fix race in the hash(Decimal) (#154045)

files:
M Modules/_decimal/_decimal.c

diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index 6fbce0ed85e695d..ac3ffb23248f342 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -5935,11 +5935,13 @@ static Py_hash_t
 dec_hash(PyObject *op)
 {
     PyDecObject *self = _PyDecObject_CAST(op);
-    if (self->hash == -1) {
-        self->hash = _dec_hash(self);
-    }
+    Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->hash);
 
-    return self->hash;
+    if (hash == -1) {
+        hash = _dec_hash(self);
+        FT_ATOMIC_STORE_SSIZE_RELAXED(self->hash, hash);
+    }
+    return hash;
 }
 
 /*[clinic input]

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]