Skip to content

Commit 398204d

Browse files
committed
object: fix reported TSAN races
1 parent a62d376 commit 398204d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Include/object.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ Py_DECREF(PyObject *op)
723723
_Py_DECREF_STAT_INC();
724724
// Non-limited C API and limited C API for Python 3.9 and older access
725725
// directly PyObject.ob_refcnt.
726-
uintptr_t tid = op->ob_tid;
726+
uintptr_t tid = _Py_atomic_load_uintptr_relaxed(&op->ob_tid);
727727
if (tid == _Py_STATIC_CAST(uintptr_t, Py_REF_IMMORTAL)) {
728728
return;
729729
}
@@ -736,8 +736,14 @@ Py_DECREF(PyObject *op)
736736
_Py_NegativeRefcount(filename, lineno, op);
737737
}
738738
#endif
739-
op->ob_ref_local -= (1 << _Py_REF_LOCAL_SHIFT);
740-
if (_PY_UNLIKELY(op->ob_ref_local == 0)) {
739+
uint32_t local = op->ob_ref_local;
740+
local -= (1 << _Py_REF_LOCAL_SHIFT);
741+
#ifdef _Py_THREAD_SANITIZER
742+
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local);
743+
#else
744+
op->ob_ref_local = local;
745+
#endif
746+
if (_PY_UNLIKELY(local == 0)) {
741747
_Py_MergeZeroRefcount(op);
742748
}
743749
}

0 commit comments

Comments
 (0)