server/process: Various server startup logging fixes
PATCH 1/1 server/process: Various server startup logging fixes
There were various problems in the server startup loggin:
a) stdout/stderr were not being flushed before forking which could potentially duplicate output
b) there were separate buffers for stdout/stderr leading to confusing logs where the entries could be reordered. This was particularly confusing due to the separator the logs use to idendify new messages
c) an fd wasn't being closed during server startup meaning if the server failed to start, the closed fd wasn't detected as it was held open by the other reference
d) If the pipe was detected as being closed, the code incorrectly retried server startup
e) The event code would remap stdout/stderr without flushing them, leading to lose log messages
--- lib/bb/daemonize.py | 12 +++++++++--- lib/bb/event.py | 3 +++ lib/bb/server/process.py | 20 ++++++++++---------- 3 files changed, 22 insertions(+), 13 deletions(-)
lib/bb/daemonize.py +9 -3
@@ -16,6 +16,10 @@ def createDaemon(function, logfile): background as a daemon, returning control to the caller. """ + # Ensure stdout/stderror are flushed before forking to avoid duplicate output + sys.stdout.flush() + sys.stderr.flush() + try: # Fork a child process so the parent can exit. This returns control to # the command-line or shell. It also guarantees that the child will not
@@ -66,12 +70,14 @@ def createDaemon(function, logfile): try: so = open(logfile, 'a+') - se = so os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(se.fileno(), sys.stderr.fileno()) + os.dup2(so.fileno(), sys.stderr.fileno()) except io.UnsupportedOperation: sys.stdout = open(logfile, 'a+') - sys.stderr = sys.stdout + + # Have stdout and stderr be the same so log output matches chronologically + # and there aren't two seperate buffers + sys.stderr = sys.stdout try: function()
lib/bb/event.py +3 -0
@@ -141,6 +141,9 @@ def print_ui_queue(): logger = logging.getLogger("BitBake") if not _uiready: from bb.msg import BBLogFormatter + # Flush any existing buffered content + sys.stdout.flush() + sys.stderr.flush() stdout = logging.StreamHandler(sys.stdout) stderr = logging.StreamHandler(sys.stderr) formatter = BBLogFormatter("%(levelname)s: %(message)s")
lib/bb/server/process.py +10 -10
@@ -395,11 +395,16 @@ class BitBakeServer(object): bb.daemonize.createDaemon(self._startServer, logfile) self.sock.close() self.bitbake_lock.close() + os.close(self.readypipein) ready = ConnectionReader(self.readypipe) r = ready.poll(30) if r: - r = ready.get() + try: + r = ready.get() + except EOFError: + # Trap the child exitting/closing the pipe and error out + r = None if not r or r != "ready": ready.close() bb.error("Unable to start bitbake server")
@@ -425,21 +430,16 @@ class BitBakeServer(object): bb.error("Server log for this session (%s):\n%s" % (logfile, "".join(lines))) raise SystemExit(1) ready.close() - os.close(self.readypipein) def _startServer(self): print(self.start_log_format % (os.getpid(), datetime.datetime.now().strftime(self.start_log_datetime_format))) server = ProcessServer(self.bitbake_lock, self.sock, self.sockname) self.configuration.setServerRegIdleCallback(server.register_idle_function) + os.close(self.readypipe) writer = ConnectionWriter(self.readypipein) - try: - self.cooker = bb.cooker.BBCooker(self.configuration, self.featureset) - writer.send("ready") - except: - writer.send("fail") - raise - finally: - os.close(self.readypipein) + self.cooker = bb.cooker.BBCooker(self.configuration, self.featureset) + writer.send("ready") + writer.close() server.cooker = self.cooker server.server_timeout = self.configuration.server_timeout server.xmlrpcinterface = self.configuration.xmlrpcinterface
Signature · 1 line
2.17.1
Looks good to me. I have a few (optional) suggestions.
Quoted 51 lines · Richard Purdie
There were various problems in the server startup loggin:
a) stdout/stderr were not being flushed before forking which could potentially
duplicate outputb) there were separate buffers for stdout/stderr leading to confusing logs
where the entries could be reordered. This was particularly
confusing
due to the separator the logs use to idendify new messagesc) an fd wasn't being closed during server startup meaning if the server failed to start, the closed fd wasn't detected as it was
held
open by the other referenced) If the pipe was detected as being closed, the code incorrectly retried
server startupe) The event code would remap stdout/stderr without flushing them, leading
to lose log messagesSigned-off-by Richard Purdie <[email protected]>--- lib/bb/daemonize.py | 12 +++++++++--- lib/bb/event.py | 3 +++ lib/bb/server/process.py | 20 ++++++++++---------- 3 files changed, 22 insertions(+), 13 deletions(-)lib/bb/daemonize.py +1
-0@@ -16,6 +16,10 @@ def createDaemon(function, logfile): background as a daemon, returning control to the caller. """ + # Ensure stdout/stderror are flushed before forking to avoidduplicate output
lib/bb/daemonize.py +3
-0+ sys.stdout.flush() + sys.stderr.flush() + try: # Fork a child process so the parent can exit. This returnscontrol to
# the command-line or shell. It also guarantees that the child will not
lib/bb/daemonize.py def createDaemon(function, logfile): · line 74
Quoted 7 lines
lib/bb/daemonize.py +1
-2@@ -66,12 +70,14 @@ def createDaemon(function, logfile): try: so = open(logfile, 'a+') - se = so os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(se.fileno(), sys.stderr.fileno()) + os.dup2(so.fileno(), sys.stderr.fileno())
I think so "leaks" here. Granted, it was doing that before so perhaps it could be saved for a separate patch:
with open(logfile, 'a+') as so:
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(so.fileno(), sys.stderr.fileno())Quoted 75 lines
lib/bb/daemonize.py +2
-1except io.UnsupportedOperation: sys.stdout = open(logfile, 'a+') - sys.stderr = sys.stdout + + # Have stdout and stderr be the same so log output matcheschronologically
lib/bb/daemonize.py +2
-0+ # and there aren't two seperate buffers + sys.stderr = sys.stdout try: function()lib/bb/event.py +3
-0@@ -141,6 +141,9 @@ def print_ui_queue(): logger = logging.getLogger("BitBake") if not _uiready: from bb.msg import BBLogFormatter + # Flush any existing buffered content + sys.stdout.flush() + sys.stderr.flush() stdout = logging.StreamHandler(sys.stdout) stderr = logging.StreamHandler(sys.stderr) formatter = BBLogFormatter("%(levelname)s: %(message)s")lib/bb/server/process.py +5
-1@@ -395,11 +395,16 @@ class BitBakeServer(object): bb.daemonize.createDaemon(self._startServer, logfile) self.sock.close() self.bitbake_lock.close() + os.close(self.readypipein) ready = ConnectionReader(self.readypipe) r = ready.poll(30) if r: - r = ready.get() + try: + r = ready.get() + except EOFError: + # Trap the child exitting/closing the pipe and errorout
lib/bb/server/process.py +3
-9+ r = None if not r or r != "ready": ready.close() bb.error("Unable to start bitbake server")@@ -425,21 +430,16 @@ class BitBakeServer(object): bb.error("Server log for this session (%s):\n%s" % (logfile, "".join(lines))) raise SystemExit(1) ready.close() - os.close(self.readypipein) def _startServer(self): print(self.start_log_format % (os.getpid(), datetime.datetime.now().strftime(self.start_log_datetime_format))) server = ProcessServer(self.bitbake_lock, self.sock, self.sockname) self.configuration.setServerRegIdleCallback(server.register_ idle_function) + os.close(self.readypipe) writer = ConnectionWriter(self.readypipein) - try: - self.cooker = bb.cooker.BBCooker(self.configuration, self.featureset) - writer.send("ready") - except: - writer.send("fail") - raise - finally: - os.close(self.readypipein) + self.cooker = bb.cooker.BBCooker(self.configuration,self.featureset)
lib/bb/server/process.py class BitBakeServer(object): · line 442
Quoted 2 lines
lib/bb/server/process.py +2
-0+ writer.send("ready") + writer.close()
If we want to ensure that writer is always explicitly closed, would this be better?
import contextlib
with contextlib.closing(ConnectionWriter(self.readypipein)) as writer:
self.cooker = bb.cooker.BBCooker(...) writer.send("ready")
Quoted 6 lines
server.cooker = self.cooker server.server_timeout = self.configuration.server_timeout server.xmlrpcinterface = self.configuration.xmlrpcinterface
Signature · 1 line
2.17.1
Signature · 1 line
Joshua Watt <[email protected]>
Quoted 10 lines · Joshua Watt
Looks good to me. I have a few (optional) suggestions.
lib/bb/daemonize.py def createDaemon(function, logfile): · line 74
Quoted 6 lines · Richard Purdie
try: so = open(logfile, 'a+')lib/bb/daemonize.py +1
-2- se = so os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(se.fileno(), sys.stderr.fileno()) + os.dup2(so.fileno(), sys.stderr.fileno())
Quoted 6 lines
I think so "leaks" here. Granted, it was doing that before so perhaps it could be saved for a separate patch:
with
open(logfile, 'a+')as so:os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(so.fileno(), sys.stderr.fileno())
I'd take a patch for it.
Quoted 19 lines
Quoted 13 lines
lib/bb/server/process.py +1
-2+ os.close(self.readypipe) writer = ConnectionWriter(self.readypipein) - try: - self.cooker = bb.cooker.BBCooker(self.configuration,self.featureset)
lib/bb/server/process.py +1
-6- writer.send("ready") - except: - writer.send("fail") - raise - finally: - os.close(self.readypipein) + self.cooker = bb.cooker.BBCooker(self.configuration,self.featureset)
lib/bb/server/process.py class BitBakeServer(object): · line 442
Quoted 2 lines
lib/bb/server/process.py +2
-0+ writer.send("ready") + writer.close()If we want to ensure that writer is always explicitly closed, would this be better?
Quoted 7 lines
import contextlib
with contextlib.closing(ConnectionWriter(self.readypipein)) as
writer:
self.cooker =bb.cooker.BBCooker(...)writer.send("ready")
We don't want to do that. The closing of the fd triggers the client to decide something went wrong and check the logs so we want that to happen after everything has flushed. The process takedown closing the pipe is therefore a better option in this case. You could argue we should flush here but the general exception handling and so on is later down the exception path so I think what we're doing after this patch is a better option.
Cheers,
Richard