Fix some race conditions
This commit is contained in:
parent
1e8a91d412
commit
44c74dca9a
|
@ -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
|
||||
eventHandler["webview:created"] = onWebviewCreated
|
||||
|
|
|
@ -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"))
|
||||
|
||||
|
|
Loading…
Reference in New Issue