title: Fatal error during Py_Finalize in embedded Python application
body: <p>Thanks for your help with this -- variations on this question have been asked many times, but I haven't found a complete answer. I am adding embedded Python 3.4.2 to an existing simulator tool written in C++ using MS MFC classes. The application is multithreaded so that the user can execute Python scripts and interact with the simulator system.</p>
  
  <p>How do I exit successfully?
  Am I using the GIL and thread state commands in the proper order?
  Am I ending the Python interpreter thread prematurely and breaking the Python thread consolidation mechanism?</p>
  
  <p>My problem is that when I call Py_Finalize, it calls wait_for_thread_shutdown, then PyThreadState_Get, and hits a fatal error, "PyThreadState_Get: no current thread." Based on the point where the fatal error is detected, it seems to relate to the consolidation of threads at the end of a multithreaded embedded Python application.</p>
  
  <p>I've condensed my code to clarify it and to eliminate anything that doesn't seem relevant. My apologies if I went too far or not far enough. The main thread initializes and finalizes Python.</p>
  
  <pre><code>BOOL CSimApp::InitInstance()
  {
      ...
      // Initialize command table for appl. object and for documents
      int iReturn = PyImport_AppendInittab("sim", &amp;PyInit_SimApp);
      iReturn = PyImport_AppendInittab("sim_doc", &amp;PyInit_SimDoc);
  
      // Initialize Python and prepar to create threads
      _pHInstance = new CPyInstance();
      ...
  }
  
  int CSimApp::ExitInstance() 
  {
      ...
      if (_pHInstance) {
          delete _pHInstance;
          _pHInstance = NULL;
      }
      ...
  }
  </code></pre>
  
  <p>I'm using utility classes to create the Python instance (CPyInstance) and to manage the Python GIL (ACQUIRE_PY_GIL). When the application is initialized an instance of CPyInstance is also created. The class CPyInstance initializes and finalizes the Python thread management. Python Global lock management is accomplished with the ACQUIRE_PY_GIL and RELEASE_PY_GIL structures.</p>
  
  <pre><code>class CPyInstance
  {
  public:
      CPyInstance();
      ~CPyInstance();
      static PyThreadState * mainThreadState;
  };
  
  inline CPyInstance::CPyInstance()
  {
      mainThreadState = NULL;
      Py_Initialize();
      PyEval_InitThreads();
      mainThreadState = PyThreadState_Get();
      PyEval_ReleaseLock();
  }
  
  inline CPyInstance::~CPyInstance()
  {
      Py_Finalize();
  }
  
  static CPyInstance    *_pHInstance = NULL;
  
  int PyExit()
  {
      if (_pHInstance) {
          delete _pHInstance;
          _pHInstance = NULL;
      }
      return 0;
  }
  
  struct ACQUIRE_PY_GIL {
      PyGILState_STATE state;
      ACQUIRE_PY_GIL() { state = PyGILState_Ensure(); }
      ~ACQUIRE_PY_GIL() { PyGILState_Release(state); }
  };
  
  struct RELEASE_PY_GIL {
      PyThreadState *state;
      RELEASE_PY_GIL() {  state = PyEval_SaveThread(); }
      ~RELEASE_PY_GIL() { PyEval_RestoreThread(state); }
  };
  </code></pre>
  
  <p>The Python interpreter thread is created in response to a Windows message handled by the CMainFrame window. The Python thread and interpreter runs in response to a user command. When the user finishes with the interpreter (Control-Z), the interpreter exits, the thread clears and deletes the Python thread state, and then the thread terminates itself.</p>
  
  <pre><code>void CMainFrame::OnOpenPythonInterpreter()
  {
      // Create PyThread thread
      m_pPyThread = (CPyThread*)AfxBeginThread(RUNTIME_CLASS(CPyThread),
                      THREAD_PRIORITY_BELOW_NORMAL,0, CREATE_SUSPENDED);
      CMainFrame* mf = (CMainFrame*)theApp.m_pMainWnd;
      m_pPyThread-&gt;SetOwner(this,((CWnd*)mf)-&gt;GetSafeHwnd());
      m_pPyThread-&gt;CreateLocks(&amp;m_PyThreadEvent,&amp;m_PyThreadBusyMutex);
      m_pPyThread-&gt;ResumeThread();
  }
  </code></pre>
  
  <p>The CPyThread class actually calls the Python interpreter. When the interpreter returns the GIL is released and the Python thread state is cleared and deleted. The thread terminates in response to the PostQuitMessage.</p>
  
  <pre><code>int CPyThread::Run() 
  {
      PyEval_AcquireLock();
      PyInterpreterState * mainInterpreterState = CPyInstance::mainThreadState-&gt;interp;
      PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);
      PyEval_ReleaseLock();
  
      try {
          ACQUIRE_PY_GIL    lock;
          FILE* fp1 = stdin;
          char *filename = "Embedded";
          PyRun_InteractiveLoop(fp1, filename);
      } catch(const std::exception &amp;e) {
          safe_cout &lt;&lt; "Exception in PyRun_InteractiveLoop: " &lt;&lt; e.what() &lt;&lt; "\n";
      } catch(...) {
          std::cout &lt;&lt; "Exception in Python code: UNKNOWN\n";
      }
  
      PyThreadState_Clear(myThreadState);
      PyThreadState_Delete(myThreadState);
  
      ::PostQuitMessage(0);
      return 0;
  }
  
  int CPyThread::ExitInstance() 
  {
      return CWinThread::ExitInstance();
  }
  </code></pre>
  
  <p>At the suggestion of "user4815162342" I modified my ~CPyInstance() destructor to acquire the GIL before calling Py_Finalize(). Now my application appears to exit properly, thanks.</p>
  
  <pre><code>inline CPyInstance::~CPyInstance()
  {
      try {
          PyGILState_STATE state = PyGILState_Ensure();
          Py_Finalize();
      } catch(const std::exception &amp;e) {
          safe_cout &lt;&lt; "Exception in ~CPyInstance(): " &lt;&lt; e.what() &lt;&lt; "\n";
      } catch(...) {
          std::cout &lt;&lt; "Exception in Python code: UNKNOWN\n";
      }
  }
  </code></pre>