Skip to content

Commit 87c7ee6

Browse files
committed
scripts: handle BrokenPipeError for python scripts
In the follow-up of commit fb3041d ("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]: llvm/llvm-project#59037 [3]: llvm/llvm-project@4787efa [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>
1 parent 8d9acfc commit 87c7ee6

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

scripts/checkkconfigsymbols.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def parse_options():
115115
return args
116116

117117

118-
def main():
118+
def print_undefined_symbols():
119119
"""Main function of this module."""
120120
args = parse_options()
121121

@@ -467,5 +467,16 @@ def parse_kconfig_file(kfile):
467467
return defined, references
468468

469469

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
479+
480+
470481
if __name__ == "__main__":
471482
main()

scripts/clang-tools/run-clang-tools.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,21 @@ def run_analysis(entry):
6161

6262

6363
def main():
64-
args = parse_arguments()
64+
try:
65+
args = parse_arguments()
6566

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
7279

7380

7481
if __name__ == "__main__":

scripts/diffconfig

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def print_config(op, config, value, new_value):
6565
else:
6666
print(" %s %s -> %s" % (config, value, new_value))
6767

68-
def main():
68+
def show_diff():
6969
global merge_style
7070

7171
# parse command line args
@@ -129,4 +129,16 @@ def main():
129129
for config in new:
130130
print_config("+", config, None, b[config])
131131

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()

0 commit comments

Comments
 (0)