summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/utils/httpserver.py
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2018-11-16 09:33:28 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2018-11-24 22:02:47 +0000
commit1c88c939f8448527ad908d3fecad549bfd65a161 (patch)
treeab27da2c3c1dba05529804c1a66edf8dd670810b /meta/lib/oeqa/utils/httpserver.py
parent7692a65189c71748af1d792dda7ee08fde7e561a (diff)
downloadpoky-1c88c939f8448527ad908d3fecad549bfd65a161.tar.gz
oeqa/utils/httpserver: Rework to avoid hangs and improve logging
testimage.bbclass installs a SIGTERM handler which conflicts with the use of multiprocessing here. This is paritcularly problematic if the http service is terminated before its started and hence before its had a chance to reset the default signal handler (as the code was written). Instead, temporarily remove testimage's handler whilst forking the http process which means the correct handler is installed and won't deadlock. Also take the opportunity to add in some log messages about the server start and shutdown so that future debugging is easier and its clearer what the code is doing. (From OE-Core rev: cc0471439aa0085ca87deccf061c5b676ef12388) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oeqa/utils/httpserver.py')
-rw-r--r--meta/lib/oeqa/utils/httpserver.py37
1 files changed, 29 insertions, 8 deletions
diff --git a/meta/lib/oeqa/utils/httpserver.py b/meta/lib/oeqa/utils/httpserver.py
index 7d12331453..a48d4994fd 100644
--- a/meta/lib/oeqa/utils/httpserver.py
+++ b/meta/lib/oeqa/utils/httpserver.py
@@ -1,13 +1,13 @@
1import http.server 1import http.server
2import multiprocessing 2import multiprocessing
3import os 3import os
4import traceback
5import signal
4from socketserver import ThreadingMixIn 6from socketserver import ThreadingMixIn
5 7
6class HTTPServer(ThreadingMixIn, http.server.HTTPServer): 8class HTTPServer(ThreadingMixIn, http.server.HTTPServer):
7 9
8 def server_start(self, root_dir): 10 def server_start(self, root_dir, logger):
9 import signal
10 signal.signal(signal.SIGTERM, signal.SIG_DFL)
11 os.chdir(root_dir) 11 os.chdir(root_dir)
12 self.serve_forever() 12 self.serve_forever()
13 13
@@ -18,19 +18,40 @@ class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
18 18
19class HTTPService(object): 19class HTTPService(object):
20 20
21 def __init__(self, root_dir, host=''): 21 def __init__(self, root_dir, host='', logger=None):
22 self.root_dir = root_dir 22 self.root_dir = root_dir
23 self.host = host 23 self.host = host
24 self.port = 0 24 self.port = 0
25 self.logger = logger
25 26
26 def start(self): 27 def start(self):
28 if not os.path.exists(self.root_dir):
29 self.logger.info("Not starting HTTPService for directory %s which doesn't exist" % (self.root_dir))
30 return
31
27 self.server = HTTPServer((self.host, self.port), HTTPRequestHandler) 32 self.server = HTTPServer((self.host, self.port), HTTPRequestHandler)
28 if self.port == 0: 33 if self.port == 0:
29 self.port = self.server.server_port 34 self.port = self.server.server_port
30 self.process = multiprocessing.Process(target=self.server.server_start, args=[self.root_dir]) 35 self.process = multiprocessing.Process(target=self.server.server_start, args=[self.root_dir, self.logger])
36
37 # The signal handler from testimage.bbclass can cause deadlocks here
38 # if the HTTPServer is terminated before it can restore the standard
39 #signal behaviour
40 orig = signal.getsignal(signal.SIGTERM)
41 signal.signal(signal.SIGTERM, signal.SIG_DFL)
31 self.process.start() 42 self.process.start()
43 signal.signal(signal.SIGTERM, orig)
44
45 if self.logger:
46 self.logger.info("Started HTTPService on %s:%s" % (self.host, self.port))
47
32 48
33 def stop(self): 49 def stop(self):
34 self.server.server_close() 50 if hasattr(self, "server"):
35 self.process.terminate() 51 self.server.server_close()
36 self.process.join() 52 if hasattr(self, "process"):
53 self.process.terminate()
54 self.process.join()
55 if self.logger:
56 self.logger.info("Stopped HTTPService on %s:%s" % (self.host, self.port))
57