[Lldb-commits] [lldb] r154027 - /lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp

Enrico Granata egranata at apple.com
Wed Apr 4 10:31:29 PDT 2012


Author: enrico
Date: Wed Apr  4 12:31:29 2012
New Revision: 154027

URL: http://llvm.org/viewvc/llvm-project?rev=154027&view=rev
Log:
Fixing a potential crasher where Python would assume we have no thread state while clearing out an SBDebugger which was acquiring input from the interactive interpreter

Modified:
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=154027&r1=154026&r2=154027&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Wed Apr  4 12:31:29 2012
@@ -381,15 +381,22 @@
 void
 ScriptInterpreterPython::LeaveSession ()
 {
-    PyObject *sysmod = PyImport_AddModule ("sys");
-    PyObject *sysdict = PyModule_GetDict (sysmod);
-
-    if (m_new_sysout && sysmod && sysdict)
-    {
-        if (m_old_sysout)
-            PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_old_sysout);
-        if (m_old_syserr)
-            PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_old_syserr);
+    // checking that we have a valid thread state - since we use our own threading and locking
+    // in some (rare) cases during cleanup Python may end up believing we have no thread state
+    // and PyImport_AddModule will crash if that is the case - since that seems to only happen
+    // when destroying the SBDebugger, we can make do without clearing up stdout and stderr
+    if (PyThreadState_Get())
+    {
+        PyObject *sysmod = PyImport_AddModule ("sys");
+        PyObject *sysdict = PyModule_GetDict (sysmod);
+
+        if (m_new_sysout && sysmod && sysdict)
+        {
+            if (m_old_sysout)
+                PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_old_sysout);
+            if (m_old_syserr)
+                PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_old_syserr);
+        }
     }
 
     m_session_is_active = false;





More information about the lldb-commits mailing list