From 44c74dca9a6a27232ae7ee617001465bd3272c05 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sat, 7 Oct 2023 17:47:18 +0200 Subject: [PATCH] Fix some race conditions --- user/addon/history/_main.py | 6 ++- user/addon/startupNewWindow/_main.py | 65 ++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/user/addon/history/_main.py b/user/addon/history/_main.py index 7acbf08..f3548d4 100644 --- a/user/addon/history/_main.py +++ b/user/addon/history/_main.py @@ -4,7 +4,9 @@ browser = mfp.require("browser") config = mfp.require("config") if not os.path.isdir(config.paths[0]): - os.makedirs(config.paths[0]) + try: + os.makedirs(config.paths[0]) + except Exception: pass fh = open(os.path.join(config.paths[0],"history.txt"),"a") @@ -23,4 +25,4 @@ def onTitleChanged(obj,_): def onWebviewCreated(webView): webView.connect("notify::title",onTitleChanged) webView.connect("notify::uri",onUrlChanged) -eventHandler["webview:created"] = onWebviewCreated \ No newline at end of file +eventHandler["webview:created"] = onWebviewCreated diff --git a/user/addon/startupNewWindow/_main.py b/user/addon/startupNewWindow/_main.py index 8d2eed4..decfed2 100644 --- a/user/addon/startupNewWindow/_main.py +++ b/user/addon/startupNewWindow/_main.py @@ -1,32 +1,63 @@ import sys import os import socket -socketFile = mfp.p("/tmp",mfp.programName + ".session") +import fcntl +import time +config = mfp.require("config") -try: # See if socket already exists (A browser process is open) - s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) - s.connect(socketFile) -except Exception: # Otherwise, make our own (No browser process is open) +if not os.path.isdir(config.paths[0]): try: - os.remove(socketFile) - except: pass - s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) - s.bind(socketFile) - s.listen(65535) -else: - if len(sys.argv) < 2: - uri = "about:blank" + os.makedirs(config.paths[0]) + except Exception: + pass + +lockFile = mfp.p(config.paths[0],"session.lock") +socketFile = mfp.p(config.paths[0],"session.socket") + +tries = 1 +while tries <= 10: + print("Opening new window, try " +str(tries)+ " ...") + host = True + try: + lockFileHandle = open(lockFile,"w") + fcntl.flock(lockFileHandle,fcntl.LOCK_EX | fcntl.LOCK_NB) + except Exception as e: + host = False + + if host: + try: + os.remove(socketFile) + except: pass + + s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) + s.bind(socketFile) + s.listen(65535) + break else: - uri = sys.argv[1] - s.send((uri + "\n").encode("utf-8")) - sys.exit(0) + try: + s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) + s.connect(socketFile) + if len(sys.argv) < 2: + uri = "about:blank" + else: + uri = sys.argv[1] + s.send((uri + "\n").encode("utf-8")) + print("Successfully merged into pre-existing process, exiting.") + sys.exit(0) + except Exception as e: + print(e) + tries += 1 + time.sleep(1) + +if tries >= 11: + print("Could not acquire new window from pre-existing process.") + sys.exit(1) import gi gi.require_version('Gtk', '3.0') from gi.repository import GLib import threading import queue -import time browser = mfp.require("browser") browserWindow = browser.module.require(mfp.p("window","browser.py"))