From 5b362b65cee17550866d4951841a06bcdc4558ce Mon Sep 17 00:00:00 2001 From: "Galantsev, Dmitrii" Date: Thu, 14 Sep 2023 11:58:49 -0500 Subject: [PATCH] rocm_smi.py: Fix pipe into head error When piping rocm_smi into 'head' it failed with "Broken pipe" error. The error can be safely ignored. head closes the pipe early which causes calls a SIGPIPE signal to be raised. https://docs.python.org/3/library/signal.html#note-on-sigpipe Change-Id: I4a589c6ed9a8c5b50de84b33e28115c6b510045f Signed-off-by: Galantsev, Dmitrii [ROCm/amdsmi commit: 094c98a74fa9d770dc5f714de512eedd4ab78971] --- projects/amdsmi/python_smi_tools/rocm_smi.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/projects/amdsmi/python_smi_tools/rocm_smi.py b/projects/amdsmi/python_smi_tools/rocm_smi.py index a8b3e5a74a..4d1a5bad41 100755 --- a/projects/amdsmi/python_smi_tools/rocm_smi.py +++ b/projects/amdsmi/python_smi_tools/rocm_smi.py @@ -638,10 +638,21 @@ def printLog(device, metricName, value=None, extraSpace=False, useItalics=False) lock.acquire() if useItalics: logstr = italics + logstr + end - if extraSpace: - print('\n' + logstr + '\n', end='', flush=True) - else: - print(logstr + '\n', end='', flush=True) + try: + if extraSpace: + print('\n', end='') + print(logstr + '\n', end='') + sys.stdout.flush() + # when piped into programs like 'head' - print throws an error. + # silently ignore instead + except(BrokenPipeError, IOError): + # https://docs.python.org/3/library/signal.html#note-on-sigpipe + # Python flushes standard streams on exit; redirect remaining output + # to devnull to avoid another BrokenPipeError at shutdown + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + sys.exit(1) # Python exits with error code 1 on EPIPE + lock.release()