From 5afae4c1a485bca5de6618d5f7f2ba1059d1e607 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 Jun 2020 15:02:11 +0530 Subject: [PATCH] macOS: When using the single instance option of the viewer, crashes should not require a restart of the computer to use the viewer again. See #1881375 (Viewer crash on macOS when advancing to next chapter) --- src/calibre/gui2/viewer/main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/viewer/main.py b/src/calibre/gui2/viewer/main.py index 164571998a..d4bf1020d4 100644 --- a/src/calibre/gui2/viewer/main.py +++ b/src/calibre/gui2/viewer/main.py @@ -12,13 +12,13 @@ from PyQt5.Qt import QIcon, QObject, Qt, QTimer, pyqtSignal from PyQt5.QtWebEngineCore import QWebEngineUrlScheme from calibre import as_unicode, prints -from calibre.constants import FAKE_PROTOCOL, VIEWER_APP_UID, islinux +from calibre.constants import FAKE_PROTOCOL, VIEWER_APP_UID, islinux, iswindows from calibre.gui2 import Application, error_dialog, setup_gui_option_parser from calibre.gui2.viewer.ui import EbookViewer, is_float +from calibre.gui2.viewer.web_view import get_session_pref, vprefs from calibre.ptempfile import reset_base_dir from calibre.utils.config import JSONConfig from calibre.utils.ipc import RC, viewer_socket_address -from calibre.gui2.viewer.web_view import vprefs, get_session_pref singleinstance_name = 'calibre_viewer' @@ -111,10 +111,20 @@ def listen(listener, msg_from_anotherinstance): def create_listener(): + addr = viewer_socket_address() if islinux: from calibre.utils.ipc.server import LinuxListener as Listener else: from multiprocessing.connection import Listener + if not iswindows: + # On macOS (and BSDs, I am guessing), following a crash, the + # listener socket file sticks around and needs to be explicitly + # removed. It is safe to do this since we are already guaranteed to + # be the owner of the socket by singleinstance() + try: + os.remove(addr) + except Exception: + pass return Listener(address=viewer_socket_address())