Skip to content

Commit 5bebd9a

Browse files
committed
gh-153852: Fix data race in hash cal for Decimal
1 parent 13e3f8a commit 5bebd9a

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

Modules/_decimal/_decimal.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <Python.h>
3333
#include "pycore_object.h" // _PyObject_VisitType()
34+
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_SSIZE_RELAXED() and FT_ATOMIC_STORE_SSIZE_RELAXED()
3435
#include "pycore_pystate.h" // _PyThreadState_GET()
3536
#include "pycore_tuple.h" // _PyTuple_FromPair
3637
#include "pycore_typeobject.h"
@@ -5921,11 +5922,13 @@ static Py_hash_t
59215922
dec_hash(PyObject *op)
59225923
{
59235924
PyDecObject *self = _PyDecObject_CAST(op);
5924-
if (self->hash == -1) {
5925-
self->hash = _dec_hash(self);
5925+
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->hash);
5926+
if (hash == -1) {
5927+
hash = _dec_hash(self);
5928+
FT_ATOMIC_STORE_SSIZE_RELAXED(self->hash, hash);
59265929
}
59275930

5928-
return self->hash;
5931+
return hash;
59295932
}
59305933

59315934
/*[clinic input]

0 commit comments

Comments
 (0)