Blame SOURCES/0005-Resolves-rhbz-144437-make-gnome-documents-not-crash-.patch

69cc7b
From 226b7436988f7a0e4c7d06353c630f10047ac7f6 Mon Sep 17 00:00:00 2001
69cc7b
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
69cc7b
Date: Mon, 8 May 2017 15:26:22 +0100
69cc7b
Subject: [PATCH] Resolves: rhbz#144437 make gnome-documents not crash the
69cc7b
 whole time
69cc7b
69cc7b
accept that once initted that LibreOffice cannot be deinitted and reinited
69cc7b
(without lots of work), but allow the main loop to quit and restart so LOKs
69cc7b
thread can run and exit successfully, new LOK connections will restart the main
69cc7b
loop.
69cc7b
69cc7b
The buckets of global state continues to be valid the whole time this way
69cc7b
69cc7b
Change-Id: Ide54c0df2ce4065f7c192ae8c2cedfaaa2b58d72
69cc7b
---
69cc7b
 desktop/source/app/app.cxx               |  2 +-
69cc7b
 desktop/source/app/officeipcthread.cxx   |  9 +++++++--
69cc7b
 desktop/source/app/officeipcthread.hxx   |  2 +-
69cc7b
 desktop/source/lib/init.cxx              |  5 ++++-
69cc7b
 framework/source/services/desktop.cxx    |  8 +++++---
69cc7b
 libreofficekit/source/gtk/lokdocview.cxx | 27 ++++++++++++++++++---------
69cc7b
 vcl/source/app/svmain.cxx                |  8 ++++++++
69cc7b
 7 files changed, 44 insertions(+), 17 deletions(-)
69cc7b
69cc7b
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
69cc7b
index df79f41..dc7ff1e 100644
69cc7b
--- a/desktop/source/app/app.cxx
69cc7b
+++ b/desktop/source/app/app.cxx
69cc7b
@@ -1998,7 +1998,7 @@ IMPL_LINK_NOARG(Desktop, OpenClients_Impl, void*, void)
69cc7b
         // When this server closes down it attempts to recreate the pipe (in RequestHandler::Disable()).
69cc7b
         // It's possible that the client has a pending connection request.
69cc7b
         // When the IPC thread is not running, this connection locks (because maPipe.accept()) is never called
69cc7b
-        RequestHandler::SetReady();
69cc7b
+        RequestHandler::SetReady(true);
69cc7b
         OpenClients();
69cc7b
 
69cc7b
         CloseSplashScreen();
69cc7b
diff --git a/desktop/source/app/officeipcthread.cxx b/desktop/source/app/officeipcthread.cxx
69cc7b
index 7a2fd46..5ea5f41 100644
69cc7b
--- a/desktop/source/app/officeipcthread.cxx
69cc7b
+++ b/desktop/source/app/officeipcthread.cxx
69cc7b
@@ -939,6 +939,8 @@ void RequestHandler::Disable()
69cc7b
             handler->mIpcThread->join();
69cc7b
             handler->mIpcThread.clear();
69cc7b
         }
69cc7b
+
69cc7b
+        handler->cReady.reset();
69cc7b
     }
69cc7b
 }
69cc7b
 
69cc7b
@@ -953,12 +955,15 @@ RequestHandler::~RequestHandler()
69cc7b
     assert(!mIpcThread.is());
69cc7b
 }
69cc7b
 
69cc7b
-void RequestHandler::SetReady()
69cc7b
+void RequestHandler::SetReady(bool bIsReady)
69cc7b
 {
69cc7b
     osl::MutexGuard g(GetMutex());
69cc7b
     if (pGlobal.is())
69cc7b
     {
69cc7b
-        pGlobal->cReady.set();
69cc7b
+        if (bIsReady)
69cc7b
+            pGlobal->cReady.set();
69cc7b
+        else
69cc7b
+            pGlobal->cReady.reset();
69cc7b
     }
69cc7b
 }
69cc7b
 
69cc7b
diff --git a/desktop/source/app/officeipcthread.hxx b/desktop/source/app/officeipcthread.hxx
69cc7b
index 2826aba..2b58ce4 100644
69cc7b
--- a/desktop/source/app/officeipcthread.hxx
69cc7b
+++ b/desktop/source/app/officeipcthread.hxx
69cc7b
@@ -122,7 +122,7 @@ class RequestHandler: public salhelper::SimpleReferenceObject
69cc7b
     static Status               Enable(bool ipc);
69cc7b
     static void                 Disable();
69cc7b
     // start dispatching events...
69cc7b
-    static void                 SetReady();
69cc7b
+    static void                 SetReady(bool bIsReady);
69cc7b
     static void                 WaitForReady();
69cc7b
 
69cc7b
     bool                        AreRequestsEnabled() const { return mState == State::RequestsEnabled; }
69cc7b
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
69cc7b
index 9df6471..14c7bfd 100644
69cc7b
--- a/desktop/source/lib/init.cxx
69cc7b
+++ b/desktop/source/lib/init.cxx
69cc7b
@@ -2742,10 +2742,12 @@ static void lo_startmain(void*)
69cc7b
 {
69cc7b
     osl_setThreadName("lo_startmain");
69cc7b
 
69cc7b
-    if (GetpApp())
69cc7b
+    if (comphelper::SolarMutex::get())
69cc7b
         Application::GetSolarMutex().tryToAcquire();
69cc7b
 
69cc7b
     soffice_main();
69cc7b
+
69cc7b
+    Application::ReleaseSolarMutex();
69cc7b
 }
69cc7b
 
69cc7b
 static bool bInitialized = false;
69cc7b
@@ -2912,6 +2914,7 @@ static int lo_initialize(LibreOfficeKit* pThis, const char* pAppPath, const char
69cc7b
             SAL_INFO("lok", "Enabling RequestHandler");
69cc7b
             RequestHandler::Enable(false);
69cc7b
             SAL_INFO("lok", "Starting soffice_main");
69cc7b
+            RequestHandler::SetReady(false);
69cc7b
             pLib->maThread = osl_createThread(lo_startmain, nullptr);
69cc7b
             SAL_INFO("lok", "Waiting for RequestHandler");
69cc7b
             RequestHandler::WaitForReady();
69cc7b
diff --git a/framework/source/services/desktop.cxx b/framework/source/services/desktop.cxx
69cc7b
index 20afab1..6f6412a 100644
69cc7b
--- a/framework/source/services/desktop.cxx
69cc7b
+++ b/framework/source/services/desktop.cxx
69cc7b
@@ -59,6 +59,7 @@
69cc7b
 #include <com/sun/star/frame/XTerminateListener2.hpp>
69cc7b
 
69cc7b
 #include <comphelper/sequence.hxx>
69cc7b
+#include <comphelper/lok.hxx>
69cc7b
 #include <cppuhelper/supportsservice.hxx>
69cc7b
 #include <rtl/instance.hxx>
69cc7b
 #include <vcl/svapp.hxx>
69cc7b
@@ -228,8 +229,9 @@ sal_Bool SAL_CALL Desktop::terminate()
69cc7b
 
69cc7b
     // try to close all open frames.
69cc7b
     // Allow using of any UI ... because Desktop.terminate() was designed as UI functionality in the past.
69cc7b
-    bool bIsEventTestingMode = Application::IsEventTestingModeEnabled();
69cc7b
-    bool bFramesClosed = impl_closeFrames(!bIsEventTestingMode);
69cc7b
+    bool bRestartableMainLoop = Application::IsEventTestingModeEnabled() ||
69cc7b
+                                comphelper::LibreOfficeKit::isActive();
69cc7b
+    bool bFramesClosed = impl_closeFrames(!bRestartableMainLoop);
69cc7b
 
69cc7b
     // Ask normal terminate listener. They could stop terminating the process.
69cc7b
     Desktop::TTerminateListenerList lCalledTerminationListener;
69cc7b
@@ -241,7 +243,7 @@ sal_Bool SAL_CALL Desktop::terminate()
69cc7b
         return false;
69cc7b
     }
69cc7b
 
69cc7b
-    if (bIsEventTestingMode)
69cc7b
+    if (bRestartableMainLoop)
69cc7b
     {
69cc7b
         Application::Quit();
69cc7b
         return true;
69cc7b
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
69cc7b
index 6d05b7a..5d64d7b 100644
69cc7b
--- a/libreofficekit/source/gtk/lokdocview.cxx
69cc7b
+++ b/libreofficekit/source/gtk/lokdocview.cxx
69cc7b
@@ -2610,16 +2610,25 @@ static void lok_doc_view_destroy (GtkWidget* widget)
69cc7b
 
69cc7b
     aGuard.unlock();
69cc7b
 
69cc7b
-    if (priv->m_pDocument && priv->m_pDocument->pClass->getViewsCount(priv->m_pDocument) > 1)
69cc7b
-    {
69cc7b
-        priv->m_pDocument->pClass->destroyView(priv->m_pDocument, priv->m_nViewId);
69cc7b
-    }
69cc7b
-    else
69cc7b
+    if (priv->m_pDocument)
69cc7b
     {
69cc7b
-        if (priv->m_pDocument)
69cc7b
-            priv->m_pDocument->pClass->destroy (priv->m_pDocument);
69cc7b
-        if (priv->m_pOffice)
69cc7b
-            priv->m_pOffice->pClass->destroy (priv->m_pOffice);
69cc7b
+        if (priv->m_pDocument->pClass->getViewsCount(priv->m_pDocument) > 1)
69cc7b
+        {
69cc7b
+            priv->m_pDocument->pClass->destroyView(priv->m_pDocument, priv->m_nViewId);
69cc7b
+        }
69cc7b
+        else
69cc7b
+        {
69cc7b
+            if (priv->m_pDocument)
69cc7b
+            {
69cc7b
+                priv->m_pDocument->pClass->destroy (priv->m_pDocument);
69cc7b
+                priv->m_pDocument = nullptr;
69cc7b
+            }
69cc7b
+            if (priv->m_pOffice)
69cc7b
+            {
69cc7b
+                priv->m_pOffice->pClass->destroy (priv->m_pOffice);
69cc7b
+                priv->m_pOffice = nullptr;
69cc7b
+            }
69cc7b
+        }
69cc7b
     }
69cc7b
 
69cc7b
     GTK_WIDGET_CLASS (lok_doc_view_parent_class)->destroy (widget);
69cc7b
diff --git a/vcl/source/app/svmain.cxx b/vcl/source/app/svmain.cxx
69cc7b
index 2cd6137..a5f7807 100644
69cc7b
--- a/vcl/source/app/svmain.cxx
69cc7b
+++ b/vcl/source/app/svmain.cxx
69cc7b
@@ -84,6 +84,7 @@
69cc7b
 #include <com/sun/star/lang/XComponent.hpp>
69cc7b
 #include <com/sun/star/frame/Desktop.hpp>
69cc7b
 
69cc7b
+#include <comphelper/lok.hxx>
69cc7b
 #include <cppuhelper/implbase.hxx>
69cc7b
 #include <uno/current_context.hxx>
69cc7b
 
69cc7b
@@ -361,6 +362,13 @@ VCLUnoWrapperDeleter::disposing(lang::EventObject const& /* rSource */)
69cc7b
 
69cc7b
 void DeInitVCL()
69cc7b
 {
69cc7b
+    //rhbz#1444437, when using LibreOffice like a library you can't realistically
69cc7b
+    //tear everything down and recreate them on the next call, there's too many
69cc7b
+    //(c++) singletons that point to stuff that gets deleted during shutdown
69cc7b
+    //which won't be recreated on restart.
69cc7b
+    if (comphelper::LibreOfficeKit::isActive())
69cc7b
+        return;
69cc7b
+
69cc7b
     {
69cc7b
         SolarMutexReleaser r; // unblock threads blocked on that so we can join
69cc7b
         ::comphelper::JoinAsyncEventNotifiers();
69cc7b
-- 
69cc7b
2.13.0
69cc7b