File tree 1 file changed +5
-2
lines changed
1 file changed +5
-2
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,9 @@ reftotal_add(PyThreadState *tstate, Py_ssize_t n)
77
77
{
78
78
#ifdef Py_GIL_DISABLED
79
79
_PyThreadStateImpl * tstate_impl = (_PyThreadStateImpl * )tstate ;
80
- tstate_impl -> reftotal += n ;
80
+ // relaxed store to avoid data race with read in get_reftotal()
81
+ Py_ssize_t reftotal = tstate_impl -> reftotal + n ;
82
+ _Py_atomic_store_ssize_relaxed (& tstate_impl -> reftotal , reftotal );
81
83
#else
82
84
REFTOTAL (tstate -> interp ) += n ;
83
85
#endif
@@ -114,7 +116,8 @@ get_reftotal(PyInterpreterState *interp)
114
116
#ifdef Py_GIL_DISABLED
115
117
for (PyThreadState * p = interp -> threads .head ; p != NULL ; p = p -> next ) {
116
118
/* This may race with other threads modifications to their reftotal */
117
- total += ((_PyThreadStateImpl * )p )-> reftotal ;
119
+ _PyThreadStateImpl * tstate_impl = (_PyThreadStateImpl * )p ;
120
+ total += _Py_atomic_load_ssize_relaxed (& tstate_impl -> reftotal );
118
121
}
119
122
#endif
120
123
return total ;
You can’t perform that action at this time.
0 commit comments