Delete lock file if it's stale

This commit is contained in:
Micah Lee 2020-08-20 18:37:20 -04:00
parent 06d2b75f3e
commit 79f8465407
3 changed files with 57 additions and 15 deletions

View file

@ -24,6 +24,8 @@ import platform
import argparse import argparse
import signal import signal
import json import json
import psutil
import getpass
from PyQt5 import QtCore, QtWidgets from PyQt5 import QtCore, QtWidgets
from onionshare.common import Common from onionshare.common import Common
@ -125,6 +127,20 @@ def main():
with open(common.gui.lock_filename, "r") as f: with open(common.gui.lock_filename, "r") as f:
existing_pid = int(f.read()) existing_pid = int(f.read())
# Is this process actually still running?
still_running = True
if not psutil.pid_exists(existing_pid):
still_running = False
else:
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})") print(f"Opening tab in existing OnionShare window (pid {existing_pid})")
# Make an event for the existing OnionShare window # Make an event for the existing OnionShare window
@ -137,8 +153,9 @@ def main():
with open(common.gui.events_filename, "a") as f: with open(common.gui.events_filename, "a") as f:
f.write(json.dumps(obj) + "\n") f.write(json.dumps(obj) + "\n")
return return
else: else:
os.remove(common.gui.lock_filename)
# Write the lock file # Write the lock file
with open(common.gui.lock_filename, "w") as f: with open(common.gui.lock_filename, "w") as f:
f.write(f"{os.getpid()}\n") f.write(f"{os.getpid()}\n")

26
poetry.lock generated
View file

@ -312,6 +312,17 @@ version = ">=0.12"
[package.extras] [package.extras]
dev = ["pre-commit", "tox"] dev = ["pre-commit", "tox"]
[[package]]
category = "main"
description = "Cross-platform lib for process and system monitoring in Python."
name = "psutil"
optional = false
python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
version = "5.7.2"
[package.extras]
test = ["ipaddress", "mock", "unittest2", "enum34", "pywin32", "wmi"]
[[package]] [[package]]
category = "dev" category = "dev"
description = "library with cross-python path, ini-parsing, io, code, log facilities" description = "library with cross-python path, ini-parsing, io, code, log facilities"
@ -590,7 +601,7 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"]
testing = ["jaraco.itertools", "func-timeout"] testing = ["jaraco.itertools", "func-timeout"]
[metadata] [metadata]
content-hash = "04baf07d49586f9567f78935d759b4245d75ed42cba8bdfe0bdb6a3bc9533aee" content-hash = "b5a36d265c4247b98b493fac989ecdd3c11920bd4c81d47f2af9e64edb5fd5bb"
lock-version = "1.0" lock-version = "1.0"
python-versions = "^3.7" python-versions = "^3.7"
@ -755,6 +766,19 @@ pluggy = [
{file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"},
{file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"},
] ]
psutil = [
{file = "psutil-5.7.2-cp27-none-win32.whl", hash = "sha256:f2018461733b23f308c298653c8903d32aaad7873d25e1d228765e91ae42c3f2"},
{file = "psutil-5.7.2-cp27-none-win_amd64.whl", hash = "sha256:66c18ca7680a31bf16ee22b1d21b6397869dda8059dbdb57d9f27efa6615f195"},
{file = "psutil-5.7.2-cp35-cp35m-win32.whl", hash = "sha256:5e9d0f26d4194479a13d5f4b3798260c20cecf9ac9a461e718eb59ea520a360c"},
{file = "psutil-5.7.2-cp35-cp35m-win_amd64.whl", hash = "sha256:4080869ed93cce662905b029a1770fe89c98787e543fa7347f075ade761b19d6"},
{file = "psutil-5.7.2-cp36-cp36m-win32.whl", hash = "sha256:d8a82162f23c53b8525cf5f14a355f5d1eea86fa8edde27287dd3a98399e4fdf"},
{file = "psutil-5.7.2-cp36-cp36m-win_amd64.whl", hash = "sha256:0ee3c36428f160d2d8fce3c583a0353e848abb7de9732c50cf3356dd49ad63f8"},
{file = "psutil-5.7.2-cp37-cp37m-win32.whl", hash = "sha256:ff1977ba1a5f71f89166d5145c3da1cea89a0fdb044075a12c720ee9123ec818"},
{file = "psutil-5.7.2-cp37-cp37m-win_amd64.whl", hash = "sha256:a5b120bb3c0c71dfe27551f9da2f3209a8257a178ed6c628a819037a8df487f1"},
{file = "psutil-5.7.2-cp38-cp38-win32.whl", hash = "sha256:10512b46c95b02842c225f58fa00385c08fa00c68bac7da2d9a58ebe2c517498"},
{file = "psutil-5.7.2-cp38-cp38-win_amd64.whl", hash = "sha256:68d36986ded5dac7c2dcd42f2682af1db80d4bce3faa126a6145c1637e1b559f"},
{file = "psutil-5.7.2.tar.gz", hash = "sha256:90990af1c3c67195c44c9a889184f84f5b2320dce3ee3acbd054e3ba0b4a7beb"},
]
py = [ py = [
{file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"}, {file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"},
{file = "py-1.9.0.tar.gz", hash = "sha256:9ca6883ce56b4e8da7e79ac18787889fa5206c79dcc67fb065376cd2fe03f342"}, {file = "py-1.9.0.tar.gz", hash = "sha256:9ca6883ce56b4e8da7e79ac18787889fa5206c79dcc67fb065376cd2fe03f342"},

View file

@ -31,6 +31,7 @@ Werkzeug = "*"
flask-socketio = "^4.3.0" flask-socketio = "^4.3.0"
eventlet = "^0.25.2" eventlet = "^0.25.2"
qrcode = "^6.1" qrcode = "^6.1"
psutil = "^5.7.2"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
atomicwrites = "*" atomicwrites = "*"