diff options
author | Ralf Grosse-Kunstleve <rwgk@google.com> | 2022-01-11 19:34:38 +0100 |
---|---|---|
committer | Pavel Labath <pavel@labath.sk> | 2022-01-17 10:32:19 +0100 |
commit | a6598575f4bc20f9a01c2bced2d0b1ff14d7576f (patch) | |
tree | afed531a0dc6c93f934d486253bc2ec8251281d3 /lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp | |
parent | af12a3f4a95889bd1c1af054eda4e1d270f16ad8 (diff) | |
download | llvm-a6598575f4bc20f9a01c2bced2d0b1ff14d7576f.zip llvm-a6598575f4bc20f9a01c2bced2d0b1ff14d7576f.tar.gz llvm-a6598575f4bc20f9a01c2bced2d0b1ff14d7576f.tar.bz2 |
[LLDB] Fix Python GIL-not-held issues
The GIL must be held when calling any Python C API functions. In multithreaded applications that use callbacks this requirement can easily be violated by accident. A general tool to ensure GIL health is not available, but patching Python Py_INCREF to add an assert provides a basic health check:
```
+int PyGILState_Check(void); /* Include/internal/pystate.h */
+
#define Py_INCREF(op) ( \
+ assert(PyGILState_Check()), \
_Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
((PyObject *)(op))->ob_refcnt++)
#define Py_DECREF(op) \
do { \
+ assert(PyGILState_Check()); \
PyObject *_py_decref_tmp = (PyObject *)(op); \
if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
--(_py_decref_tmp)->ob_refcnt != 0) \
```
Adding this assertion causes around 50 test failures in LLDB. Adjusting the scope of things guarded by `py_lock` fixes them.
More background: https://docs.python.org/3/glossary.html#term-global-interpreter-lock
Patch by Ralf Grosse-Kunstleve
Differential Revision: https://reviews.llvm.org/D114722
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp index 7c71c93..d68af67 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -257,6 +257,7 @@ PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const { } StructuredData::ObjectSP PythonObject::CreateStructuredObject() const { + assert(PyGILState_Check()); switch (GetObjectType()) { case PyObjectType::Dictionary: return PythonDictionary(PyRefType::Borrowed, m_py_obj) |