From be23d887c8e244f1ef961298fbc9214d0fd0968a Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Wed, 31 Jul 2019 10:49:39 +0100 Subject: hashserv: Switch from threads to multiprocessing There were hard to debug lockups when trying to use threading to start hashserv as a thread. Switch to multiprocessing which doesn't show the same locking problems. Signed-off-by: Richard Purdie --- lib/hashserv/__init__.py | 14 ++++++++++++-- lib/hashserv/tests.py | 8 ++++---- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'lib/hashserv') diff --git a/lib/hashserv/__init__.py b/lib/hashserv/__init__.py index 86aa7e9de..1d5e08ee5 100644 --- a/lib/hashserv/__init__.py +++ b/lib/hashserv/__init__.py @@ -13,6 +13,7 @@ import logging import socketserver import queue import threading +import signal from datetime import datetime logger = logging.getLogger('hashserv') @@ -147,8 +148,14 @@ class ThreadedHTTPServer(HTTPServer): self.handlerthread.daemon = False self.handlerthread.start() + + signal.signal(signal.SIGTERM, self.sigterm_exception) super().serve_forever() + def sigterm_exception(self, signum, stackframe): + self.server_close() + os._exit(0) + def process_request_thread(self): while not self.quit: try: @@ -163,6 +170,7 @@ class ThreadedHTTPServer(HTTPServer): self.handle_error(request, client_address) finally: self.shutdown_request(request) + os._exit(0) def process_request(self, request, client_address): self.requestqueue.put((request, client_address)) @@ -208,6 +216,8 @@ def create_server(addr, dbname, prefix=''): cursor.execute('CREATE INDEX IF NOT EXISTS taskhash_lookup ON tasks_v2 (method, taskhash)') cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup ON tasks_v2 (method, outhash)') - logger.info('Starting server on %s', addr) + ret = ThreadedHTTPServer(addr, Handler) + + logger.info('Starting server on %s\n', ret.server_port) - return ThreadedHTTPServer(addr, Handler) + return ret diff --git a/lib/hashserv/tests.py b/lib/hashserv/tests.py index 15f4a52ee..6845b5388 100644 --- a/lib/hashserv/tests.py +++ b/lib/hashserv/tests.py @@ -6,7 +6,7 @@ # import unittest -import threading +import multiprocessing import sqlite3 import hashlib import urllib.request @@ -21,16 +21,16 @@ class TestHashEquivalenceServer(unittest.TestCase): self.dbfile = tempfile.NamedTemporaryFile(prefix="bb-hashserv-db-") self.server = create_server(('localhost', 0), self.dbfile.name) self.server_addr = 'http://localhost:%d' % self.server.socket.getsockname()[1] - self.server_thread = threading.Thread(target=self.server.serve_forever) + self.server_thread = multiprocessing.Process(target=self.server.serve_forever) + self.server_thread.daemon = True self.server_thread.start() def tearDown(self): # Shutdown server s = getattr(self, 'server', None) if s is not None: - self.server.shutdown() + self.server_thread.terminate() self.server_thread.join() - self.server.server_close() def send_get(self, path): url = '%s/%s' % (self.server_addr, path) -- cgit 1.2.3-korg