[Lldb-commits] [lldb] r162147 - in /lldb/trunk: include/lldb/Interpreter/ScriptInterpreterPython.h source/Interpreter/ScriptInterpreterPython.cpp

Johnny Chen johnny.chen at apple.com
Fri Aug 17 16:44:36 PDT 2012


Author: johnny
Date: Fri Aug 17 18:44:35 2012
New Revision: 162147

URL: http://llvm.org/viewvc/llvm-project?rev=162147&view=rev
Log:
Fix a race condition where multiple PythonInputReaderManager instances could, during destruction,
tread on the m_embedded_thread_input_reader_sp singleton maintained by the script interpreter.
Furthermore, use two additional slots under the script interpreter to store the PseudoTerminal and
the InputReaderSP pertaining to the embedded python interpreter -- resulted from the
ScriptInterpreterPython::ExecuteInterpreterLoop() call -- to facilitate separation from what is being
used by the PythonInputReaderManager instances.

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

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h?rev=162147&r1=162146&r2=162147&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h Fri Aug 17 18:44:35 2012
@@ -321,8 +321,10 @@
                          size_t bytes_len);
 
 
+    lldb_utility::PseudoTerminal m_embedded_thread_pty;
     lldb_utility::PseudoTerminal m_embedded_python_pty;
     lldb::InputReaderSP m_embedded_thread_input_reader_sp;
+    lldb::InputReaderSP m_embedded_python_input_reader_sp;
     FILE *m_dbg_stdout;
     PyObject *m_new_sysout;
     PyObject *m_old_sysout;

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=162147&r1=162146&r2=162147&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Fri Aug 17 18:44:35 2012
@@ -308,23 +308,20 @@
 
 ScriptInterpreterPython::PythonInputReaderManager::~PythonInputReaderManager()
 {
-    if (m_interpreter)
-    {
-        if (m_interpreter->m_embedded_thread_input_reader_sp)
-            m_interpreter->m_embedded_thread_input_reader_sp->SetIsDone (true);
-        m_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor();
-    }
-    
-    
-    if (m_reader_sp)
+    // Nothing to do if either m_interpreter or m_reader_sp is invalid.
+    if (!m_interpreter || !m_reader_sp)
+        return;
+
+    m_reader_sp->SetIsDone (true);
+    if (m_debugger_sp)
+        m_debugger_sp->PopInputReader(m_reader_sp);
+
+    // Only mess with m_interpreter's counterpart if, indeed, they are the same object.
+    if (m_reader_sp.get() == m_interpreter->m_embedded_thread_input_reader_sp.get())
     {
-        m_reader_sp->SetIsDone (true);
-        if (m_debugger_sp)
-            m_debugger_sp->PopInputReader(m_reader_sp);
-    }
-    
-    if (m_interpreter)
+        m_interpreter->m_embedded_thread_pty.CloseSlaveFileDescriptor();
         m_interpreter->m_embedded_thread_input_reader_sp.reset();
+    }
 }
 
 size_t
@@ -368,16 +365,16 @@
             }
             
             char error_str[1024];
-            if (script_interpreter->m_embedded_python_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, 
+            if (script_interpreter->m_embedded_thread_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, 
                                                                                     sizeof(error_str)))
             {
                 if (log)
                     log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in opening master pty (fd = %d).",
-                                 script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor());
+                                 script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor());
                 {
                     StreamString run_string;
                     char error_str[1024];
-                    const char *pty_slave_name = script_interpreter->m_embedded_python_pty.GetSlaveName (error_str, sizeof (error_str));
+                    const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str));
                     if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL)
                     {
                         run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str());
@@ -460,14 +457,14 @@
             break;
             
         case eInputReaderGotToken:
-            if (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor() != -1)
+            if (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor() != -1)
             {
                 if (log)
                     log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes,
                                  bytes_len);
                 if (bytes && bytes_len)
-                    ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), bytes, bytes_len);
-                ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "\n", 1);
+                    ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), bytes, bytes_len);
+                ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), "\n", 1);
             }
             else
             {
@@ -484,7 +481,7 @@
         {
             StreamString run_string;
             char error_str[1024];
-            const char *pty_slave_name = script_interpreter->m_embedded_python_pty.GetSlaveName (error_str, sizeof (error_str));
+            const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str));
             if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL)
             {
                 run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str());
@@ -503,7 +500,7 @@
             
             script_interpreter->RestoreTerminalState ();
             
-            script_interpreter->m_embedded_python_pty.CloseMasterFileDescriptor();
+            script_interpreter->m_embedded_thread_pty.CloseMasterFileDescriptor();
             break;
     }
     
@@ -512,8 +509,10 @@
 
 ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) :
     ScriptInterpreter (interpreter, eScriptLanguagePython),
+    m_embedded_thread_pty (),
     m_embedded_python_pty (),
     m_embedded_thread_input_reader_sp (),
+    m_embedded_python_input_reader_sp (),
     m_dbg_stdout (interpreter.GetDebugger().GetOutputFile().GetStream()),
     m_new_sysout (NULL),
     m_old_sysout (NULL),
@@ -587,10 +586,19 @@
     if (m_embedded_thread_input_reader_sp.get() != NULL)
     {
         m_embedded_thread_input_reader_sp->SetIsDone (true);
-        m_embedded_python_pty.CloseSlaveFileDescriptor();
+        m_embedded_thread_pty.CloseSlaveFileDescriptor();
         const InputReaderSP reader_sp = m_embedded_thread_input_reader_sp;
+        debugger.PopInputReader (reader_sp);
         m_embedded_thread_input_reader_sp.reset();
+    }
+    
+    if (m_embedded_python_input_reader_sp.get() != NULL)
+    {
+        m_embedded_python_input_reader_sp->SetIsDone (true);
+        m_embedded_python_pty.CloseSlaveFileDescriptor();
+        const InputReaderSP reader_sp = m_embedded_python_input_reader_sp;
         debugger.PopInputReader (reader_sp);
+        m_embedded_python_input_reader_sp.reset();
     }
     
     if (m_new_sysout)
@@ -1048,7 +1056,7 @@
         if (error.Success())
         {
             debugger.PushInputReader (reader_sp);
-            m_embedded_thread_input_reader_sp = reader_sp;
+            m_embedded_python_input_reader_sp = reader_sp;
         }
     }
 }
@@ -2084,8 +2092,8 @@
         
     }
     
-    if (script_interpreter->m_embedded_thread_input_reader_sp)
-        script_interpreter->m_embedded_thread_input_reader_sp->SetIsDone (true);
+    if (script_interpreter->m_embedded_python_input_reader_sp)
+        script_interpreter->m_embedded_python_input_reader_sp->SetIsDone (true);
     
     script_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor();
 
@@ -2096,9 +2104,12 @@
 
     // Clean up the input reader and make the debugger pop it off the stack.
     Debugger &debugger = script_interpreter->GetCommandInterpreter().GetDebugger();
-    const InputReaderSP reader_sp = script_interpreter->m_embedded_thread_input_reader_sp;
-    script_interpreter->m_embedded_thread_input_reader_sp.reset();
-    debugger.PopInputReader (reader_sp);
+    const InputReaderSP reader_sp = script_interpreter->m_embedded_python_input_reader_sp;
+    if (reader_sp)
+    {
+        debugger.PopInputReader (reader_sp);
+        script_interpreter->m_embedded_python_input_reader_sp.reset();
+    }
     
     return NULL;
 }





More information about the lldb-commits mailing list