From 8fb2f5cb553010ef373cdbdcc062f193cc350509 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Wed, 23 May 2018 11:14:42 +1000 Subject: [PATCH] Save and close stdout on exit Redirecting our output through outfilter.py is inherently a bit racy, since the disk-image-create process will exit, and then you might get outfilter.py flushing any remaining output as it closes. On an interactive prompt this might lead to final output overwriting the prompt, etc. This can be a bit confusing when you start running things in a loop. If we save the original fd, then on the exit path close the redirected fd's and wait a little bit for final output (as a result of the close), we get a more consistent output. Change-Id: I8efe57ab421c1941e99bdecab62c6e21a87e4584 --- diskimage_builder/lib/disk-image-create | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/diskimage_builder/lib/disk-image-create b/diskimage_builder/lib/disk-image-create index e22549cd..189a0798 100644 --- a/diskimage_builder/lib/disk-image-create +++ b/diskimage_builder/lib/disk-image-create @@ -197,6 +197,10 @@ if [[ -n "${LOGFILE}" ]]; then echo "Output logs going to: ${LOGFILE}" _LOGFILE_FLAG="-o ${LOGFILE}" fi + +# Save the existing stdout to fd3 +exec 3>&1 + exec 1> >( ${DIB_PYTHON_EXEC:-python} $_LIB/outfilter.py ${_TS_FLAG} ${_QUIET_FLAG} ${_LOGFILE_FLAG} ) 2>&1 @@ -550,5 +554,18 @@ fi # Remove the leftovers, i.e. the temporary image directory. cleanup_image_dir +# Restore fd 1&2 from the outfilter.py redirect back to the original +# saved fd. Note small hack that we can't really wait properly for +# outfilter.py so put in a sleep (might be possible to use coproc for +# this...?) +# +# TODO(ianw): probably better to cleanup the exit handler a bit for +# this? We really want some helper functions that append to the exit +# handler so we can register multiple things. +set +o xtrace +echo "Build completed successfully" +exec 1>&3 2>&3 +sleep 1 + # All done! trap EXIT