prserv/serv: Improve process exit handling
PATCH 1/2 prserv/serv: Improve process exit handling
The server shutdown is currenlty laggy and race prone. This patch:
* adds a waitpid so that no zombie server is left around if its not running in daemon mode.
* adds a quit "sentinal" using a pipe so that we're not sitting in a socket poll() until timeout in order just to quit.
* use a select() call to poll the socket and the pipe for a quit signal.
The net result of this change is that the prserv exits with the cooker server and it does so immediately and doesn't wait for the select/poll calls to timeout. This makes bitbake a lot more responsive for startup/shutdown and doesn't cause UI timeout errors as often when prserv is used.
--- lib/prserv/serv.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
lib/prserv/serv.py +17 -2
@@ -10,6 +10,7 @@ import bb.server.xmlrpcclient import prserv import prserv.db import errno +import select logger = logging.getLogger("BitBake.PRserv")
@@ -59,6 +60,8 @@ class PRServer(SimpleXMLRPCServer): self.register_function(self.importone, "importone") self.register_introspection_functions() + self.quitpipein, self.quitpipeout = os.pipe() + self.requestqueue = queue.Queue() self.handlerthread = threading.Thread(target = self.process_request_thread) self.handlerthread.daemon = False
@@ -153,11 +156,16 @@ class PRServer(SimpleXMLRPCServer): def quit(self): self.quit=True + os.write(self.quitpipeout, b"q") + os.close(self.quitpipeout) return def work_forever(self,): self.quit = False - self.timeout = 0.5 + # This timeout applies to the poll in TCPServer, we need the select + # below to wake on our quit pipe closing. We only ever call into handle_request + # if there is data there. + self.timeout = 0.01 bb.utils.set_process_name("PRServ")
@@ -170,11 +178,16 @@ class PRServer(SimpleXMLRPCServer): self.handlerthread.start() while not self.quit: - self.handle_request() + ready = select.select([self.fileno(), self.quitpipein], [], [], 30) + if self.quit: + break + if self.fileno() in ready[0]: + self.handle_request() self.handlerthread.join() self.db.disconnect() logger.info("PRServer: stopping...") self.server_close() + os.close(self.quitpipein) return def start(self):
@@ -182,6 +195,7 @@ class PRServer(SimpleXMLRPCServer): pid = self.daemonize() else: pid = self.fork() + self.pid = pid # Ensure both the parent sees this and the child from the work_forever log entry above logger.info("Started PRServer with DBfile: %s, IP: %s, PORT: %s, PID: %s" %
@@ -472,6 +486,7 @@ def auto_shutdown(d=None): PRServerConnection(host, port).terminate() except: logger.critical("Stop PRService %s:%d failed" % (host,port)) + os.waitpid(singleton.prserv.pid, 0) singleton = None def ping(host, port):
Signature · 1 line
2.7.4
PATCH 2/2 cooker/process: Drop server_main function
Now that there is only one server, this abstraction is no longer needed and causes indrection/confusion. The server shutdown is also broken with the cooker post_server calls happening too late, leading to "lock held" warnings in the logs if PRServ is enabled.
Remove the abstraction and put the shutdown calls in the right order with respect to the locking.
--- lib/bb/cooker.py | 22 ---------------------- lib/bb/server/process.py | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 23 deletions(-)
lib/bb/cooker.py +0 -22
@@ -1585,28 +1585,6 @@ class BBCooker: self.databuilder.reset() self.data = self.databuilder.data -def server_main(cooker, func, *args): - cooker.pre_serve() - - if cooker.configuration.profile: - try: - import cProfile as profile - except: - import profile - prof = profile.Profile() - - ret = profile.Profile.runcall(prof, func, *args) - - prof.dump_stats("profile.log") - bb.utils.process_profilelog("profile.log") - print("Raw profiling information saved to profile.log and processed statistics to profile.log.processed") - - else: - ret = func(*args) - - cooker.post_serve() - - return ret class CookerExit(bb.event.Event): """
lib/bb/server/process.py +21 -1
@@ -103,9 +103,27 @@ class ProcessServer(multiprocessing.Process): except: pass - bb.cooker.server_main(self.cooker, self.main) + if self.cooker.configuration.profile: + try: + import cProfile as profile + except: + import profile + prof = profile.Profile() + + ret = profile.Profile.runcall(prof, self.main) + + prof.dump_stats("profile.log") + bb.utils.process_profilelog("profile.log") + print("Raw profiling information saved to profile.log and processed statistics to profile.log.processed") + + else: + ret = self.main() + + return ret def main(self): + self.cooker.pre_serve() + bb.utils.set_process_name("Cooker") ready = []
@@ -184,6 +202,8 @@ class ProcessServer(multiprocessing.Process): except: pass + self.cooker.post_serve() + # Remove the socket file so we don't get any more connections to avoid races os.unlink(self.sockname) self.sock.close()
Signature · 1 line
2.7.4