Kill stale tor process

This commit is contained in:
Micah Lee 2020-11-17 18:06:36 -08:00
parent 9f21d505de
commit 894fb7c070
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73

View File

@ -28,6 +28,7 @@ import tempfile
import subprocess
import time
import shlex
import getpass
import psutil
from distutils.version import LooseVersion as Version
@ -115,13 +116,6 @@ class TorTooOld(Exception):
pass
class BundledTorNotSupported(Exception):
"""
This exception is raised if onionshare is set to use the bundled Tor binary,
but it's not supported on that platform, or in dev mode.
"""
class BundledTorTimeout(Exception):
"""
This exception is raised if onionshare is set to use the bundled Tor binary,
@ -242,6 +236,23 @@ class Onion(object):
raise OSError("OnionShare port not available")
self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")
# If there is an existing OnionShare tor process, kill it
for proc in psutil.process_iter(["pid", "name", "username"]):
if proc.username() == getpass.getuser():
cmdline = proc.cmdline()
if (
cmdline[0] == self.tor_path
and cmdline[1] == "-f"
and cmdline[2] == self.torrc
):
self.common.log(
"Onion",
"connect",
"found a stale tor process, killing it",
)
proc.terminate()
proc.wait()
if self.common.platform == "Windows" or self.common.platform == "Darwin":
# Windows doesn't support unix sockets, so it must use a network port.
# macOS can't use unix sockets either because socket filenames are limited to