Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts: handle BrokenPipeError for python scripts

In the follow-up of commit fb3041d61f68 ("kbuild: fix SIGPIPE error
message for AR=gcc-ar and AR=llvm-ar"), Kees Cook pointed out that
tools should _not_ catch their own SIGPIPEs [1] [2].

Based on his feedback, LLVM was fixed [3].

However, Python's default behavior is to show noisy bracktrace when
SIGPIPE is sent. So, scripts written in Python are basically in the
same situation as the buggy llvm tools.

Example:

$ make -s allnoconfig
$ make -s allmodconfig
$ scripts/diffconfig .config.old .config | head -n1
-ALIX n
Traceback (most recent call last):
File "/home/masahiro/linux/scripts/diffconfig", line 132, in <module>
main()
File "/home/masahiro/linux/scripts/diffconfig", line 130, in main
print_config("+", config, None, b[config])
File "/home/masahiro/linux/scripts/diffconfig", line 64, in print_config
print("+%s %s" % (config, new_value))
BrokenPipeError: [Errno 32] Broken pipe

Python documentation [4] notes how to make scripts die immediately and
silently:

"""
Piping output of your program to tools like head(1) will cause a
SIGPIPE signal to be sent to your process when the receiver of its
standard output closes early. This results in an exception like
BrokenPipeError: [Errno 32] Broken pipe. To handle this case,
wrap your entry point to catch this exception as follows:

import os
import sys

def main():
try:
# simulate large output (your code replaces this loop)
for x in range(10000):
print("y")
# flush output here to force SIGPIPE to be triggered
# while inside this try block.
sys.stdout.flush()
except BrokenPipeError:
# 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

if __name__ == '__main__':
main()

Do not set SIGPIPE’s disposition to SIG_DFL in order to avoid
BrokenPipeError. Doing that would cause your program to exit
unexpectedly whenever any socket connection is interrupted while
your program is still writing to it.
"""

Currently, tools/perf/scripts/python/intel-pt-events.py seems to be the
only script that fixes the issue that way.

tools/perf/scripts/python/compaction-times.py uses another approach
signal.signal(signal.SIGPIPE, signal.SIG_DFL) but the Python
documentation clearly says "Don't do it".

I cannot fix all Python scripts since there are so many.
I fixed some in the scripts/ directory.

[1]: https://lore.kernel.org/all/202211161056.1B9611A@keescook/
[2]: https://github.com/llvm/llvm-project/issues/59037
[3]: https://github.com/llvm/llvm-project/commit/4787efa38066adb51e2c049499d25b3610c0877b
[4]: https://docs.python.org/3/library/signal.html#note-on-sigpipe

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

+40 -10
+12 -1
scripts/checkkconfigsymbols.py
··· 115 115 return args 116 116 117 117 118 - def main(): 118 + def print_undefined_symbols(): 119 119 """Main function of this module.""" 120 120 args = parse_options() 121 121 ··· 465 465 references.append(symbol) 466 466 467 467 return defined, references 468 + 469 + 470 + def main(): 471 + try: 472 + print_undefined_symbols() 473 + except BrokenPipeError: 474 + # Python flushes standard streams on exit; redirect remaining output 475 + # to devnull to avoid another BrokenPipeError at shutdown 476 + devnull = os.open(os.devnull, os.O_WRONLY) 477 + os.dup2(devnull, sys.stdout.fileno()) 478 + sys.exit(1) # Python exits with error code 1 on EPIPE 468 479 469 480 470 481 if __name__ == "__main__":
+14 -7
scripts/clang-tools/run-clang-tools.py
··· 61 61 62 62 63 63 def main(): 64 - args = parse_arguments() 64 + try: 65 + args = parse_arguments() 65 66 66 - lock = multiprocessing.Lock() 67 - pool = multiprocessing.Pool(initializer=init, initargs=(lock, args)) 68 - # Read JSON data into the datastore variable 69 - with open(args.path, "r") as f: 70 - datastore = json.load(f) 71 - pool.map(run_analysis, datastore) 67 + lock = multiprocessing.Lock() 68 + pool = multiprocessing.Pool(initializer=init, initargs=(lock, args)) 69 + # Read JSON data into the datastore variable 70 + with open(args.path, "r") as f: 71 + datastore = json.load(f) 72 + pool.map(run_analysis, datastore) 73 + except BrokenPipeError: 74 + # Python flushes standard streams on exit; redirect remaining output 75 + # to devnull to avoid another BrokenPipeError at shutdown 76 + devnull = os.open(os.devnull, os.O_WRONLY) 77 + os.dup2(devnull, sys.stdout.fileno()) 78 + sys.exit(1) # Python exits with error code 1 on EPIPE 72 79 73 80 74 81 if __name__ == "__main__":
+14 -2
scripts/diffconfig
··· 65 65 else: 66 66 print(" %s %s -> %s" % (config, value, new_value)) 67 67 68 - def main(): 68 + def show_diff(): 69 69 global merge_style 70 70 71 71 # parse command line args ··· 129 129 for config in new: 130 130 print_config("+", config, None, b[config]) 131 131 132 - main() 132 + def main(): 133 + try: 134 + show_diff() 135 + except BrokenPipeError: 136 + # Python flushes standard streams on exit; redirect remaining output 137 + # to devnull to avoid another BrokenPipeError at shutdown 138 + devnull = os.open(os.devnull, os.O_WRONLY) 139 + os.dup2(devnull, sys.stdout.fileno()) 140 + sys.exit(1) # Python exits with error code 1 on EPIPE 141 + 142 + 143 + if __name__ == '__main__': 144 + main()