From 481b030a5f52e9943340a34f87495fddb775ca0f Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 14 Nov 2022 12:42:16 -0700 Subject: [PATCH] Silence any errors from the LibreOffice shutdown on macOS, usually due to users closing LibreOffice, fixes #1200 --- .../presentations/lib/libreofficeserver.py | 35 ++++++++++--------- .../presentations/lib/maclocontroller.py | 7 +++- .../presentations/test_maclocontroller.py | 17 +++++++++ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/openlp/plugins/presentations/lib/libreofficeserver.py b/openlp/plugins/presentations/lib/libreofficeserver.py index a105e0d33..f69881f08 100644 --- a/openlp/plugins/presentations/lib/libreofficeserver.py +++ b/openlp/plugins/presentations/lib/libreofficeserver.py @@ -180,23 +180,24 @@ class LibreOfficeServer(object): if hasattr(self, '_docs'): while self._docs: self._docs[0].close_presentation() - docs = self.desktop.getComponents() - count = 0 - if docs.hasElements(): - list_elements = docs.createEnumeration() - while list_elements.hasMoreElements(): - doc = list_elements.nextElement() - if doc.getImplementationName() != 'com.sun.star.comp.framework.BackingComp': - count += 1 - if count > 0: - log.debug('LibreOffice not terminated as docs are still open') - can_kill = False - else: - try: - self.desktop.terminate() - log.debug('LibreOffice killed') - except Exception: - log.exception('Failed to terminate LibreOffice') + if self.desktop: + docs = self.desktop.getComponents() + count = 0 + if docs.hasElements(): + list_elements = docs.createEnumeration() + while list_elements.hasMoreElements(): + doc = list_elements.nextElement() + if doc.getImplementationName() != 'com.sun.star.comp.framework.BackingComp': + count += 1 + if count > 0: + log.debug('LibreOffice not terminated as docs are still open') + can_kill = False + else: + try: + self.desktop.terminate() + log.debug('LibreOffice killed') + except Exception: + log.exception('Failed to terminate LibreOffice') if getattr(self, '_process') and can_kill: self._process.kill() diff --git a/openlp/plugins/presentations/lib/maclocontroller.py b/openlp/plugins/presentations/lib/maclocontroller.py index 526132292..f93fa1fc9 100644 --- a/openlp/plugins/presentations/lib/maclocontroller.py +++ b/openlp/plugins/presentations/lib/maclocontroller.py @@ -114,7 +114,12 @@ class MacLOController(PresentationController, LogMixin): Called at system exit to clean up any running presentations. """ log.debug('Kill LibreOffice') - self.client.shutdown() + try: + # Some people like to close LibreOffice themselves, let's just catch any errors so that OpenLP fails + # silently + self.client.shutdown() + except Exception: + pass self.server_process.kill() diff --git a/tests/openlp_plugins/presentations/test_maclocontroller.py b/tests/openlp_plugins/presentations/test_maclocontroller.py index d750c2633..e819b1e23 100644 --- a/tests/openlp_plugins/presentations/test_maclocontroller.py +++ b/tests/openlp_plugins/presentations/test_maclocontroller.py @@ -159,6 +159,23 @@ class TestMacLOController(TestCase, TestMixin): controller._client.shutdown.assert_called_once_with() controller.server_process.kill.assert_called_once_with() + @patch('openlp.plugins.presentations.lib.maclocontroller.MacLOController._start_server') + def test_kill_client_already_closed(self, mocked_start_server): + """ + Test the kill() method when the client is already closed + """ + # GIVEN: A controller and a client + controller = MacLOController(plugin=self.mock_plugin) + controller._client = MagicMock(**{'shutdown.side_effect': Exception}) + controller.server_process = MagicMock() + + # WHEN: start_process() is called + controller.kill() + + # THEN: The client's start_process() should have been called + controller._client.shutdown.assert_called_once_with() + controller.server_process.kill.assert_called_once_with() + class TestMacLODocument(TestCase): """