f66d577
From 1638b4f78af70b7b97d0a081ed51390dd87bf1f9 Mon Sep 17 00:00:00 2001
f66d577
From: Stephan Bergmann <sbergman@redhat.com>
f66d577
Date: Thu, 16 Jun 2022 18:58:18 +0200
f66d577
Subject: [PATCH] rhbz#2097411 Avoid obsolete PyThreadState_Delete crashing
f66d577
 Python 3.11
f66d577
f66d577
1fb53a637597f76bea86514b62ddfad34f60c889 "pyuno_loader::CreateInstance: delete
f66d577
the initial PyThreadState" had added the PyThreadState_Delete for claimed
f66d577
benefits but whose details appear lost to history (cf. the comment thread
f66d577
starting at
f66d577
<https://gerrit.libreoffice.org/c/core/+/3452/2#message-602ff52abdd1c95fd5c13cfe405b5fadd0048c5f>
f66d577
"pyuno_loader::CreateInstance: delete the initial PyThreadState").  And at least
f66d577
a recent master Linux --enable-python=fully-internal build with the bundled
f66d577
Python 3.8.12 appears to succeed `make check` just fine with the
f66d577
PyThreadState_Delete temporarily removed.
f66d577
f66d577
But on the other hand, building against upcoming Python 3.11 now started to make
f66d577
CppunitTest_services fail with
f66d577
f66d577
> Fatal Python error: init_threadstate: thread state already initialized
f66d577
> Python runtime state: initialized
f66d577
> Thread 0x0000ffff81c8b020 (most recent call first):
f66d577
>   <no Python frame>
f66d577
> Fatal exception: Signal 6
f66d577
> Stack:
f66d577
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libuno_sal.so.3(+0x37c28)[0xffff81be7c28]
f66d577
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libuno_sal.so.3(+0x37e40)[0xffff81be7e40]
f66d577
> linux-vdso.so.1(__kernel_rt_sigreturn+0x0)[0xffff81ccb7ec]
f66d577
> /lib64/libc.so.6(+0x82878)[0xffff81742878]
f66d577
> /lib64/libc.so.6(raise+0x20)[0xffff816fae00]
f66d577
> /lib64/libc.so.6(abort+0xe8)[0xffff816e72b8]
f66d577
> /lib64/libpython3.11.so.1.0(+0x104e28)[0xfffee4de4e28]
f66d577
> /lib64/libpython3.11.so.1.0(+0x105200)[0xfffee4de5200]
f66d577
> /lib64/libpython3.11.so.1.0(PyThread_get_thread_native_id+0x0)[0xfffee4ed6764]
f66d577
> /lib64/libpython3.11.so.1.0(PyThreadState_New+0x14)[0xfffee4ed6628]
f66d577
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libpyuno.so(_ZN5pyuno14PyThreadAttachC2EP3_is+0x78)[0xfffee4c8c52c]
f66d577
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libpythonloaderlo.so(pyuno_Loader_get_implementation+0x5c)[0xfffee5243060]
f66d577
> /builddir/build/BUILD/libreoffice-7.3.4.2/instdir/program/libuno_cppuhelpergcc3.so.3(+0x544b4)[0xffff815544b4]
f66d577
f66d577
because of the PyThreadState_Delete.  (The deleted PyThreadState, while not
f66d577
reused again directly, is still recorded in the state obtained from
f66d577
PyInterpreterState_Head() later.)
f66d577
f66d577
So conservatively keep the PyThreadState_Delete of unclear benefit for older
f66d577
Python versions and only drop it for 3.11 where it is known to have negative
f66d577
effects now.
f66d577
f66d577
Change-Id: I9b99f1e947f0b165ddc95c2bfbd764858dda39db
f66d577
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136006
f66d577
Tested-by: Jenkins
f66d577
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
f66d577
---
f66d577
 pyuno/source/loader/pyuno_loader.cxx | 2 ++
f66d577
 1 file changed, 2 insertions(+)
f66d577
f66d577
diff --git a/pyuno/source/loader/pyuno_loader.cxx b/pyuno/source/loader/pyuno_loader.cxx
f66d577
index c9b8248c1b25..da0467f450c6 100644
f66d577
--- a/pyuno/source/loader/pyuno_loader.cxx
f66d577
+++ b/pyuno/source/loader/pyuno_loader.cxx
f66d577
@@ -229,10 +229,12 @@ void pythonInit() {
f66d577
 
f66d577
     PyThreadState *tstate = PyThreadState_Get();
f66d577
     PyEval_ReleaseThread( tstate );
f66d577
+#if PY_VERSION_HEX < 0x030B0000
f66d577
     // This tstate is never used again, so delete it here.
f66d577
     // This prevents an assertion in PyThreadState_Swap on the
f66d577
     // PyThreadAttach below.
f66d577
     PyThreadState_Delete(tstate);
f66d577
+#endif
f66d577
 }
f66d577
 
f66d577
 }
f66d577
-- 
f66d577
2.36.1
f66d577