[Libreoffice-commits] core.git: pyuno/source

Michael Stahl mstahl at redhat.com
Thu Feb 1 19:13:51 UTC 2018


 pyuno/source/loader/pyuno_loader.cxx |   20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

New commits:
commit 5357ca82846ea7147ad61e9340f25647a5934eb0
Author: Michael Stahl <mstahl at redhat.com>
Date:   Thu Feb 1 13:52:00 2018 +0100

    tdf#114815 pyuno: avoid 2 threads initing python in parallel
    
    According to the crash reports, it's possible for the grammar checking
    thread to call GetGrammarChecker, instantiating lightproof, at the same
    time as the main thread instantiates LngSvcMgr, which also instantiates
    (some?) (all?) grammar checkers.
    
    Ensure that pyuno_loader::CreateInstance() initialises Python only
    once with a C++11 thread safe static.
    
    Change-Id: I5b1faba9107355c508831a078366e4a29fdbfadf

diff --git a/pyuno/source/loader/pyuno_loader.cxx b/pyuno/source/loader/pyuno_loader.cxx
index 559dc64d1d99..31b4f8f494f9 100644
--- a/pyuno/source/loader/pyuno_loader.cxx
+++ b/pyuno/source/loader/pyuno_loader.cxx
@@ -176,11 +176,10 @@ static void prependPythonPath( const OUString & pythonPathBootstrap )
     osl_setEnvironment(envVar.pData, envValue.pData);
 }
 
-Reference< XInterface > CreateInstance( const Reference< XComponentContext > & ctx )
+struct PythonInit
 {
-    Reference< XInterface > ret;
-
-    if( ! Py_IsInitialized() )
+PythonInit() {
+    if (! Py_IsInitialized()) // may be inited by getComponentContext() already
     {
         OUString pythonPath;
         OUString pythonHome;
@@ -236,9 +235,22 @@ Reference< XInterface > CreateInstance( const Reference< XComponentContext > & c
         // PyThreadAttach below.
         PyThreadState_Delete(tstate);
     }
+}
+};
+
+Reference<XInterface> CreateInstance(const Reference<XComponentContext> & ctx)
+{
+    // tdf#114815 thread-safe static to init python only once
+    static PythonInit s_Init;
+
+    Reference< XInterface > ret;
 
     PyThreadAttach attach( PyInterpreterState_Head() );
     {
+        // note: this can't race against getComponentContext() because
+        // either (in soffice.bin) CreateInstance() must be called before
+        // getComponentContext() can be called, or (in python.bin) the other
+        // way around
         if( ! Runtime::isInitialized() )
         {
             Runtime::initialize( ctx );


More information about the Libreoffice-commits mailing list