[Lldb-commits] [PATCH] D114722: [LLDB] Fix Python GIL-not-held issues

Jordan Rupprecht via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Nov 29 09:58:18 PST 2021


rupprecht created this revision.
rupprecht added a reviewer: labath.
rupprecht requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114722

Files:
  lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114722.390397.patch
Type: text/x-patch
Size: 5619 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20211129/49f7ef34/attachment.bin>


More information about the lldb-commits mailing list