From d1a4b3e08973bf2b66f82ba128e2860d6240cbab Mon Sep 17 00:00:00 2001
From: "SND\\ussrhero_cp"
 <SND\ussrhero_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Tue, 27 Dec 2016 23:27:05 +0000
Subject: [PATCH] [pykd_ext_2.0] fixed : issue # 14024( eventHandler callbacks
 crash windbg)

git-svn-id: https://pykd.svn.codeplex.com/svn@91086 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd_ext/pyinterpret.cpp | 42 ++++++++++++++++++++++++++--------------
 pykd_ext/version.h       |  2 +-
 2 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/pykd_ext/pyinterpret.cpp b/pykd_ext/pyinterpret.cpp
index 8be1c66..ae80557 100644
--- a/pykd_ext/pyinterpret.cpp
+++ b/pykd_ext/pyinterpret.cpp
@@ -62,6 +62,8 @@ public:
     PyObject* Py_None;
     PyObject* PyExc_SystemExit;
 
+    PyThreadState*  PyThreadState_Current;
+
     void( *Py_Initialize)();
     void( *Py_Finalize)();
 
@@ -182,7 +184,18 @@ public:
 
     PythonInterpreter* currentInterpreter()
     {
-        return m_currentInterpter;
+        if ( m_currentInterpreter )
+            return m_currentInterpreter;
+
+        for ( auto mod : m_modules)
+        {
+            if ( mod.second->PyThreadState_Current != 0 )
+            {
+                return mod.second->m_globalInterpreter;
+            }
+        }
+        
+        return NULL;
     }
 
     PythonInterpreter* getInterpreter(int majorVersion, int minorVersion, bool global)
@@ -207,37 +220,37 @@ public:
                 module->m_globalInterpreter = new PythonInterpreter(module);
             }   
 
-            m_currentInterpter = module->m_globalInterpreter;
+            m_currentInterpreter = module->m_globalInterpreter;
             m_currentIsGlobal = true;
         }
         else
         {
             module->PyEval_RestoreThread(module->m_globalState);
-            m_currentInterpter = new PythonInterpreter(module);
+            m_currentInterpreter = new PythonInterpreter(module);
             m_currentIsGlobal = false;
         }
 
-        m_currentInterpter->m_module->PyEval_RestoreThread(m_currentInterpter->m_state);
-        m_currentInterpter->m_module->checkPykd();
+        m_currentInterpreter->m_module->PyEval_RestoreThread(m_currentInterpreter->m_state);
+        m_currentInterpreter->m_module->checkPykd();
         
-        return m_currentInterpter;
+        return m_currentInterpreter;
     }
 
     void releaseInterpretor(PythonInterpreter* interpret)
     {
-        PyModule*  module = m_currentInterpter->m_module;
+        PyModule*  module = m_currentInterpreter->m_module;
 
-        m_currentInterpter->m_state = module->PyEval_SaveThread();
+        m_currentInterpreter->m_state = module->PyEval_SaveThread();
 
         if (!m_currentIsGlobal)
         {
-            delete m_currentInterpter;
+            delete m_currentInterpreter;
 
             module->PyThreadState_Swap(module->m_globalState);
             module->m_globalState = module->PyEval_SaveThread();
         }
 
-        m_currentInterpter = 0;
+        m_currentInterpreter = 0;
     }
 
     bool isInterpreterLoaded(int majorVersion, int minorVersion)
@@ -247,17 +260,17 @@ public:
 
     void checkPykd()
     {
-        m_currentInterpter->m_module->checkPykd();
+        m_currentInterpreter->m_module->checkPykd();
     }
 
     void stopAllInterpreter()
     {
         for (auto m : m_modules)
         {
-            m_currentInterpter = m.second->m_globalInterpreter;
+            m_currentInterpreter = m.second->m_globalInterpreter;
             m.second->deactivate();
         }
-        m_currentInterpter = 0;
+        m_currentInterpreter = 0;
     }
 
 private:
@@ -266,7 +279,7 @@ private:
 
     std::map<std::pair<int,int>, PyModule*>  m_modules;
    
-    PythonInterpreter*  m_currentInterpter;
+    PythonInterpreter*  m_currentInterpreter;
     bool  m_currentIsGlobal;
 };
 
@@ -407,6 +420,7 @@ PyModule::PyModule(int majorVesion, int minorVersion)
     *reinterpret_cast<FARPROC*>(&PyProperty_Type) = GetProcAddress(m_handlePython, "PyProperty_Type");
     *reinterpret_cast<FARPROC*>(&Py_None) = GetProcAddress(m_handlePython, "_Py_NoneStruct");
     PyExc_SystemExit = *reinterpret_cast<PyObject**>(GetProcAddress(m_handlePython, "PyExc_SystemExit"));
+    PyThreadState_Current = reinterpret_cast<PyThreadState*>(GetProcAddress(m_handlePython, "_PyThreadState_Current"));
 
     *reinterpret_cast<FARPROC*>(&Py_Initialize) = GetProcAddress(m_handlePython, "Py_Initialize");
     *reinterpret_cast<FARPROC*>(&Py_Finalize) = GetProcAddress(m_handlePython, "Py_Finalize");
diff --git a/pykd_ext/version.h b/pykd_ext/version.h
index a1805fa..ddc7f4e 100644
--- a/pykd_ext/version.h
+++ b/pykd_ext/version.h
@@ -3,7 +3,7 @@
 #define PYKDEXT_VERSION_MAJOR      2
 #define PYKDEXT_VERSION_MINOR      0
 #define PYKDEXT_VERSION_SUBVERSION 0
-#define PYKDEXT_VERSION_BUILDNO    5
+#define PYKDEXT_VERSION_BUILDNO    6
 
 #define __VER_STR2__(x) #x
 #define __VER_STR1__(x) __VER_STR2__(x)