https://github.com/python/cpython/commit/a8ec511900d0d84cffbb4ee6419c9a790d131129
commit: a8ec511900d0d84cffbb4ee6419c9a790d131129
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-06-20T22:43:23+05:30
summary:
gh-135380: enhance critical section held assertions (#135381)
files:
M Include/internal/pycore_critical_section.h
diff --git a/Include/internal/pycore_critical_section.h
b/Include/internal/pycore_critical_section.h
index 42f06b935bd0a0..62460c5f8fad30 100644
--- a/Include/internal/pycore_critical_section.h
+++ b/Include/internal/pycore_critical_section.h
@@ -64,7 +64,7 @@ extern "C" {
# define _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op)
\
if (Py_REFCNT(op) != 1) {
\
-
_Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&_PyObject_CAST(op)->ob_mutex); \
+ _PyCriticalSection_AssertHeldObj(_PyObject_CAST(op)); \
}
#else /* Py_DEBUG */
@@ -239,6 +239,28 @@ _PyCriticalSection_AssertHeld(PyMutex *mutex)
#endif
}
+static inline void
+_PyCriticalSection_AssertHeldObj(PyObject *op)
+{
+#ifdef Py_DEBUG
+ PyMutex *mutex = &_PyObject_CAST(op)->ob_mutex;
+ PyThreadState *tstate = _PyThreadState_GET();
+ uintptr_t prev = tstate->critical_section;
+ if (prev & _Py_CRITICAL_SECTION_TWO_MUTEXES) {
+ PyCriticalSection2 *cs = (PyCriticalSection2 *)(prev &
~_Py_CRITICAL_SECTION_MASK);
+ _PyObject_ASSERT_WITH_MSG(op,
+ (cs != NULL && (cs->_cs_base._cs_mutex == mutex || cs->_cs_mutex2
== mutex)),
+ "Critical section of object is not held");
+ }
+ else {
+ PyCriticalSection *cs = (PyCriticalSection *)(prev &
~_Py_CRITICAL_SECTION_MASK);
+ _PyObject_ASSERT_WITH_MSG(op,
+ (cs != NULL && cs->_cs_mutex == mutex),
+ "Critical section of object is not held");
+ }
+
+#endif
+}
#endif /* Py_GIL_DISABLED */
#ifdef __cplusplus
_______________________________________________
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]