Kaydet (Commit) ed8a8c9a authored tarafından Michael Stahl's avatar Michael Stahl Kaydeden (comit) Stephan Bergmann

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
(cherry picked from commit 5357ca82)
Reviewed-on: https://gerrit.libreoffice.org/49115Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 0ca6361e
...@@ -176,11 +176,10 @@ static void prependPythonPath( const OUString & pythonPathBootstrap ) ...@@ -176,11 +176,10 @@ static void prependPythonPath( const OUString & pythonPathBootstrap )
osl_setEnvironment(envVar.pData, envValue.pData); osl_setEnvironment(envVar.pData, envValue.pData);
} }
Reference< XInterface > CreateInstance( const Reference< XComponentContext > & ctx ) struct PythonInit
{ {
Reference< XInterface > ret; PythonInit() {
if (! Py_IsInitialized()) // may be inited by getComponentContext() already
if( ! Py_IsInitialized() )
{ {
OUString pythonPath; OUString pythonPath;
OUString pythonHome; OUString pythonHome;
...@@ -236,9 +235,22 @@ Reference< XInterface > CreateInstance( const Reference< XComponentContext > & c ...@@ -236,9 +235,22 @@ Reference< XInterface > CreateInstance( const Reference< XComponentContext > & c
// PyThreadAttach below. // PyThreadAttach below.
PyThreadState_Delete(tstate); 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() ); 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() ) if( ! Runtime::isInitialized() )
{ {
Runtime::initialize( ctx ); Runtime::initialize( ctx );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment