summaryrefslogtreecommitdiff
path: root/onionshare_gui
diff options
context:
space:
mode:
authorMicah Lee <micah@micahflee.com>2020-08-20 18:37:20 -0400
committerMicah Lee <micah@micahflee.com>2020-08-20 18:37:20 -0400
commit7927ede85b355cd532506b2024b53230f37d29a0 (patch)
tree393fd49f1b1de9cc07e1bac7169b3fffb99cc22d /onionshare_gui
parentdc1588308090120dc228fb982c470b11e7912f05 (diff)
downloadonionshare-7927ede85b355cd532506b2024b53230f37d29a0.tar.gz
onionshare-7927ede85b355cd532506b2024b53230f37d29a0.zip
Delete lock file if it's stale
Diffstat (limited to 'onionshare_gui')
-rw-r--r--onionshare_gui/__init__.py47
1 files changed, 32 insertions, 15 deletions
diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py
index d083f741..c2605e81 100644
--- a/onionshare_gui/__init__.py
+++ b/onionshare_gui/__init__.py
@@ -24,6 +24,8 @@ import platform
import argparse
import signal
import json
+import psutil
+import getpass
from PyQt5 import QtCore, QtWidgets
from onionshare.common import Common
@@ -125,23 +127,38 @@ def main():
with open(common.gui.lock_filename, "r") as f:
existing_pid = int(f.read())
- print(f"Opening tab in existing OnionShare window (pid {existing_pid})")
-
- # Make an event for the existing OnionShare window
- if filenames:
- obj = {"type": "new_share_tab", "filenames": filenames}
+ # Is this process actually still running?
+ still_running = True
+ if not psutil.pid_exists(existing_pid):
+ still_running = False
else:
- obj = {"type": "new_tab"}
-
- # Write that event to disk
- with open(common.gui.events_filename, "a") as f:
- f.write(json.dumps(obj) + "\n")
- return
+ for proc in psutil.process_iter(["pid", "name", "username"]):
+ if proc.pid == existing_pid:
+ if (
+ proc.username() != getpass.getuser()
+ or "onionshare" not in " ".join(proc.cmdline()).lower()
+ ):
+ still_running = False
+
+ if still_running:
+ print(f"Opening tab in existing OnionShare window (pid {existing_pid})")
+
+ # Make an event for the existing OnionShare window
+ if filenames:
+ obj = {"type": "new_share_tab", "filenames": filenames}
+ else:
+ obj = {"type": "new_tab"}
+
+ # Write that event to disk
+ with open(common.gui.events_filename, "a") as f:
+ f.write(json.dumps(obj) + "\n")
+ return
+ else:
+ os.remove(common.gui.lock_filename)
- else:
- # Write the lock file
- with open(common.gui.lock_filename, "w") as f:
- f.write(f"{os.getpid()}\n")
+ # Write the lock file
+ with open(common.gui.lock_filename, "w") as f:
+ f.write(f"{os.getpid()}\n")
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
def signal_handler(s, frame):