From d6eabe9f12ad4f9289ed7f6ed9f7e7c63454e6ca Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 9 Jan 2019 10:54:35 -0600 Subject: [PATCH] catch sigterm properly --- onionr/communicator2.py | 2 +- onionr/onionr.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index cf04a755..d63bb9db 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -95,7 +95,7 @@ class OnionrCommunicatorDaemon: peerPoolTimer = OnionrCommunicatorTimers(self, self.getOnlinePeers, 60, maxThreads=1) OnionrCommunicatorTimers(self, self.runCheck, 1) OnionrCommunicatorTimers(self, self.lookupBlocks, self._core.config.get('timers.lookupBlocks'), requiresPeer=True, maxThreads=1) - OnionrCommunicatorTimers(self, self.getBlocks, self._core.config.get('timers.getBlocks'), requiresPeer=True) + OnionrCommunicatorTimers(self, self.getBlocks, self._core.config.get('timers.getBlocks'), requiresPeer=True, maxThreads=2) OnionrCommunicatorTimers(self, self.clearOfflinePeer, 58) OnionrCommunicatorTimers(self, self.daemonTools.cleanOldBlocks, 65) OnionrCommunicatorTimers(self, self.lookupAdders, 60, requiresPeer=True) diff --git a/onionr/onionr.py b/onionr/onionr.py index ab184a1f..e4f4b9f1 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -25,7 +25,7 @@ if sys.version_info[0] == 2 or sys.version_info[1] < 5: print('Error, Onionr requires Python 3.5+') sys.exit(1) import os, base64, random, getpass, shutil, subprocess, requests, time, platform, datetime, re, json, getpass, sqlite3 -import webbrowser, uuid +import webbrowser, uuid, signal from threading import Thread import api, core, config, logger, onionrplugins as plugins, onionrevents as events import onionrutils @@ -51,6 +51,7 @@ class Onionr: In general, external programs and plugins should not use this class. ''' self.userRunDir = os.getcwd() # Directory user runs the program from + self.killed = False try: os.chdir(sys.path[0]) except FileNotFoundError: @@ -73,6 +74,8 @@ class Onionr: self.clientAPIInst = '' # Client http api instance self.publicAPIInst = '' # Public http api instance + signal.signal(signal.SIGTERM, self.exitSigterm) + # Handle commands self.debug = False # Whole application debugging @@ -252,6 +255,9 @@ class Onionr: self.execute(command) return + + def exitSigterm(self, signum, frame): + self.killed = True ''' THIS SECTION HANDLES THE COMMANDS @@ -748,17 +754,20 @@ class Onionr: events.event('daemon_start', onionr = self) try: while True: - time.sleep(5) - + time.sleep(3) # Break if communicator process ends, so we don't have left over processes if communicatorProc.poll() is not None: break + if self.killed: + break # Break out if sigterm for clean exit except KeyboardInterrupt: + pass + finally: self.onionrCore.daemonQueueAdd('shutdown') self.onionrUtils.localCommand('shutdown') + net.killTor() time.sleep(3) self.deleteRunFiles() - net.killTor() return def killDaemon(self):