diff options
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h index 56bc55d..9d2cdca 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -88,12 +88,21 @@ public: StructuredPythonObject() : StructuredData::Generic() {} StructuredPythonObject(void *obj) : StructuredData::Generic(obj) { + assert(PyGILState_Check()); Py_XINCREF(GetValue()); } ~StructuredPythonObject() override { - if (Py_IsInitialized()) - Py_XDECREF(GetValue()); + if (Py_IsInitialized()) { + if (_Py_IsFinalizing()) { + // Leak GetValue() rather than crashing the process. + // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure + } else { + PyGILState_STATE state = PyGILState_Ensure(); + Py_XDECREF(GetValue()); + PyGILState_Release(state); + } + } SetValue(nullptr); } @@ -264,8 +273,16 @@ public: ~PythonObject() { Reset(); } void Reset() { - if (m_py_obj && Py_IsInitialized()) - Py_DECREF(m_py_obj); + if (m_py_obj && Py_IsInitialized()) { + if (_Py_IsFinalizing()) { + // Leak m_py_obj rather than crashing the process. + // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure + } else { + PyGILState_STATE state = PyGILState_Ensure(); + Py_DECREF(m_py_obj); + PyGILState_Release(state); + } + } m_py_obj = nullptr; } |