Skip to content

Commit 89fd9f3

Browse files
authored
Wrap cli functions in catching broken pipes (#278)
1 parent 2fea467 commit 89fd9f3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

python/hdfs_native/cli.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# PYTHON_ARGCOMPLETE_OK
2-
32
import functools
43
import glob
54
import os
@@ -1391,7 +1390,15 @@ def show_help(args: Namespace):
13911390

13921391
autocomplete(parser)
13931392
args = parser.parse_args(in_args)
1394-
args.func(args)
1393+
1394+
# Catch broken pipes in case the output is being piped to a command that exits early
1395+
try:
1396+
args.func(args)
1397+
except BrokenPipeError:
1398+
# Python flushes standard streams on exit; redirect remaining output
1399+
# to devnull to avoid another BrokenPipeError at shutdown
1400+
devnull = os.open(os.devnull, os.O_WRONLY)
1401+
os.dup2(devnull, sys.stdout.fileno())
13951402

13961403

13971404
if __name__ == "__main__":

0 commit comments

Comments
 (0)