Make sure to shutdown tor client if we started it

This commit is contained in:
shortcutme 2018-04-28 21:53:12 +02:00
parent a0451065a2
commit 13019941b3
No known key found for this signature in database
GPG Key ID: 5B63BAE6CB9613AE
1 changed files with 10 additions and 1 deletions

View File

@ -98,6 +98,9 @@ class TorManager(object):
time.sleep(wait * 0.5)
self.enabled = True
if self.connect():
tor_started = self.tor_process.poll() is None
if tor_started:
self.request("TAKEOWNERSHIP") # Shut down Tor client when controll connection closed
break
# Terminate on exit
atexit.register(self.stopTor)
@ -109,7 +112,13 @@ class TorManager(object):
def stopTor(self):
self.log.debug("Stopping...")
try:
self.tor_process.terminate()
tor_started = self.tor_process.poll() is None
if tor_started:
self.request("SIGNAL SHUTDOWN")
if sys.platform.startswith("win"):
subprocess.call(['taskkill', '/F', '/T', '/PID', str(self.tor_process.pid)]) # Also kill sub-processes
else:
self.tor_process.terminate()
except Exception, err:
self.log.error("Error stopping Tor: %s" % err)