Fix some race conditions

This commit is contained in:
Fierelier 2023-10-07 17:47:18 +02:00
parent 1e8a91d412
commit 44c74dca9a
2 changed files with 52 additions and 19 deletions

View File

@ -4,7 +4,9 @@ browser = mfp.require("browser")
config = mfp.require("config") config = mfp.require("config")
if not os.path.isdir(config.paths[0]): 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") fh = open(os.path.join(config.paths[0],"history.txt"),"a")
@ -23,4 +25,4 @@ def onTitleChanged(obj,_):
def onWebviewCreated(webView): def onWebviewCreated(webView):
webView.connect("notify::title",onTitleChanged) webView.connect("notify::title",onTitleChanged)
webView.connect("notify::uri",onUrlChanged) webView.connect("notify::uri",onUrlChanged)
eventHandler["webview:created"] = onWebviewCreated eventHandler["webview:created"] = onWebviewCreated

View File

@ -1,32 +1,63 @@
import sys import sys
import os import os
import socket 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) if not os.path.isdir(config.paths[0]):
s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.connect(socketFile)
except Exception: # Otherwise, make our own (No browser process is open)
try: try:
os.remove(socketFile) os.makedirs(config.paths[0])
except: pass except Exception:
s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) pass
s.bind(socketFile)
s.listen(65535) lockFile = mfp.p(config.paths[0],"session.lock")
else: socketFile = mfp.p(config.paths[0],"session.socket")
if len(sys.argv) < 2:
uri = "about:blank" 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: else:
uri = sys.argv[1] try:
s.send((uri + "\n").encode("utf-8")) s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
sys.exit(0) 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 import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import GLib from gi.repository import GLib
import threading import threading
import queue import queue
import time
browser = mfp.require("browser") browser = mfp.require("browser")
browserWindow = browser.module.require(mfp.p("window","browser.py")) browserWindow = browser.module.require(mfp.p("window","browser.py"))