flush stdout/stderr to log file
cover flush stdout/stderr to log file
This patch fixes the bug https://bugzilla.yoctoproject.org/show_bug.cgi?id=10785
It was observed that some messages may not make it into the log file. Alternatively, they may end up in the log file but in wrong order. It is fairly simple to demostrate this, please see the bug description.
Simplified, the logging code (in proccess.py) looks like this:
def _logged_communicate(pipe, log, input, extrafiles):
<...>
while ProcessOpen
log fifo(s)
log stdout
log stderr
# Process closed....
log fifo(s)
The messages are written in a round-robin fashion and if he "Process" is closed somewhere in the loop, some of the pipes may have still data in them. Also, the "Process" may be already closed before the code reaches the "while" loop and hence the pipes do not get a chance to be logged/drained. Note after "Process" is closed we do make effort to drain/log fifos but not stdout/stderr.
So the fix is this:
while ProcessOpen
log fifo(s)
log stdout
log stderr
# Drain pipes after process closed
log fifo()
log stdout
log stderr
Which can be refactored as:
def log_all_pipes:
log fifo(s)
log stdout
log stderr
while ProcessOpen
log_all_pipes
# Drain all pipes after process closed
log_all_pipes
I extensively tested the patch with the following test cases:
do_test1() {
bbdebug 1 "breakage"
echo "this will not be printed"
printf "nor this\n"
}
do_test2() {
echo "this will not be printed"
bbdebug 1 "breakage"
printf "nor this\n"
}
do_test3() {
echo "line1"
bbdebug 1 "breakage1 line2"
echo "this will not be printed (line3)"
printf "nor this (line4)\n"
bbdebug 1 "breakage2 line5"
}
do_test4() {
bbdebug 1 "breakage1 line2"
bbdebug 1 "breakage2 line2"
}
Juro Bystricky (1):
bitbake:process: flush stderr/stdout to log
bitbake/lib/bb/process.py | 81 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 37 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/1 bitbake:process: flush stderr/stdout to log
When a process terminates, some messages may still remain in stdout or stderr and do not make it into the log file.
In addition, the messages that do make it to the log file may end up in the log file in incorrect order.
This patch flushes all messages into the log file after the process terminates. Some additional log flushing is also needed to keep the various messages showing up in the log file in proper order.
[YOCTO#10785]
--- bitbake/lib/bb/process.py | 81 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 37 deletions(-)
bitbake/lib/bb/process.py +44 -37
@@ -94,45 +94,52 @@ def _logged_communicate(pipe, log, input, extrafiles): if data is not None: func(data) + def read_all_pipes(log, rin, outdata, errdata): + rlist = rin + stdoutbuf = b"" + stderrbuf = b"" + + try: + r,w,e = select.select (rlist, [], [], 1) + except OSError as e: + if e.errno != errno.EINTR: + raise + + readextras(r) + + if pipe.stdout in r: + data = stdoutbuf + pipe.stdout.read() + if data is not None and len(data) > 0: + try: + data = data.decode("utf-8") + outdata.append(data) + log.write(data) + log.flush() + stdoutbuf = b"" + except UnicodeDecodeError: + stdoutbuf = data + + if pipe.stderr in r: + data = stderrbuf + pipe.stderr.read() + if data is not None and len(data) > 0: + try: + data = data.decode("utf-8") + errdata.append(data) + log.write(data) + log.flush() + stderrbuf = b"" + except UnicodeDecodeError: + stderrbuf = data + try: + # Read all pipes while the process is open while pipe.poll() is None: - rlist = rin - stdoutbuf = b"" - stderrbuf = b"" - try: - r,w,e = select.select (rlist, [], [], 1) - except OSError as e: - if e.errno != errno.EINTR: - raise - - if pipe.stdout in r: - data = stdoutbuf + pipe.stdout.read() - if data is not None and len(data) > 0: - try: - data = data.decode("utf-8") - outdata.append(data) - log.write(data) - stdoutbuf = b"" - except UnicodeDecodeError: - stdoutbuf = data - - if pipe.stderr in r: - data = stderrbuf + pipe.stderr.read() - if data is not None and len(data) > 0: - try: - data = data.decode("utf-8") - errdata.append(data) - log.write(data) - stderrbuf = b"" - except UnicodeDecodeError: - stderrbuf = data - - readextras(r) - - finally: - log.flush() - - readextras([fobj for fobj, _ in extrafiles]) + read_all_pipes(log, rin, outdata, errdata) + + # Pocess closed, drain all pipes... + read_all_pipes(log, rin, outdata, errdata) + finally: + log.flush if pipe.stdout is not None: pipe.stdout.close()
Signature · 1 line
2.7.4
Re: [PATCH 1/1] bitbake:process: flush stderr/stdout to log
bitbake/lib/bb/process.py def _logged_communicate(pipe, log, input, extrafiles): · line 142
Quoted 11 lines · Juro Bystricky
bitbake/lib/bb/process.py +6
-4- finally: - log.flush() - - readextras([fobj for fobj, _ in extrafiles]) + read_all_pipes(log, rin, outdata, errdata) + + # Pocess closed, drain all pipes... + read_all_pipes(log, rin, outdata, errdata) + finally: + log.flush
You forgot a (), so you aren’t actually running flush here.
Signature · 4 lines
Christopher Larson kergoth at gmail dot com Founder - BitBake, OpenEmbedded, OpenZaurus Senior Software Engineer, Mentor Graphics
Re: [PATCH 1/1] bitbake:process: flush stderr/stdout to log
Thanks, good catch. There are various log flushes already there, so the "final" flush not being executed went undetected in testing, may not even be needed any more. I am surprised I did not get any python warnings though.
________________________________
From: Christopher Larson [[email protected]]
Sent: Tuesday, June 13, 2017 8:46 AM
To: Bystricky, Juro
Cc: [email protected]; Purdie, Richard; Juro Bystricky; [email protected]
Subject: Re: [bitbake-devel] [PATCH 1/1] bitbake:process: flush stderr/stdout to log
On Mon, Jun 12, 2017 at 10:12 AM, Juro Bystricky <[email protected]<mailto:[email protected]>> wrote:
- finally:
- log.flush()
-
- readextras([fobj for fobj, _ in extrafiles])
+ read_all_pipes(log, rin, outdata, errdata)
+
+ # Pocess closed, drain all pipes...
+ read_all_pipes(log, rin, outdata, errdata)
+ finally:
+ log.flush
You forgot a (), so you aren’t actually running flush here. --
Christopher Larson
kergoth at gmail dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Senior Software Engineer, Mentor Graphics