Skip to content

Commit

Permalink
object: fix reported TSAN races
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent a62d376 commit 398204d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ Py_DECREF(PyObject *op)
_Py_DECREF_STAT_INC();
// Non-limited C API and limited C API for Python 3.9 and older access
// directly PyObject.ob_refcnt.
uintptr_t tid = op->ob_tid;
uintptr_t tid = _Py_atomic_load_uintptr_relaxed(&op->ob_tid);
if (tid == _Py_STATIC_CAST(uintptr_t, Py_REF_IMMORTAL)) {
return;
}
Expand All @@ -736,8 +736,14 @@ Py_DECREF(PyObject *op)
_Py_NegativeRefcount(filename, lineno, op);
}
#endif
op->ob_ref_local -= (1 << _Py_REF_LOCAL_SHIFT);
if (_PY_UNLIKELY(op->ob_ref_local == 0)) {
uint32_t local = op->ob_ref_local;
local -= (1 << _Py_REF_LOCAL_SHIFT);
#ifdef _Py_THREAD_SANITIZER
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local);
#else
op->ob_ref_local = local;
#endif
if (_PY_UNLIKELY(local == 0)) {
_Py_MergeZeroRefcount(op);
}
}
Expand Down

0 comments on commit 398204d

Please sign in to comment.