]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tools: ynl: handle broken pipe gracefully in CLI
authorDonald Hunter <donald.hunter@gmail.com>
Thu, 8 May 2025 11:21:02 +0000 (12:21 +0100)
committerJakub Kicinski <kuba@kernel.org>
Fri, 9 May 2025 23:28:23 +0000 (16:28 -0700)
When sending YNL CLI output into a pipe, closing the pipe causes a
BrokenPipeError. E.g. running the following and quitting less:

./tools/net/ynl/pyynl/cli.py --family rt-link --dump getlink | less
Traceback (most recent call last):
  File "/home/donaldh/net-next/./tools/net/ynl/pyynl/cli.py", line 160, in <module>
    main()
    ~~~~^^
  File "/home/donaldh/net-next/./tools/net/ynl/pyynl/cli.py", line 142, in main
    output(reply)
    ~~~~~~^^^^^^^
  File "/home/donaldh/net-next/./tools/net/ynl/pyynl/cli.py", line 97, in output
    pprint.PrettyPrinter().pprint(msg)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
[...]
BrokenPipeError: [Errno 32] Broken pipe

Consolidate the try block for ops and notifications, and gracefully
handle the BrokenPipeError by adding an exception handler to the
consolidated try block.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
Link: https://patch.msgid.link/20250508112102.63539-1-donald.hunter@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/net/ynl/pyynl/cli.py

index 794e3c7dcc65ec231cfd78924aa33a5f74fbcefa..33ccc5c1843b402ec6e24e3fbdfc0c80fff62c99 100755 (executable)
@@ -144,16 +144,17 @@ def main():
             ops = [ (item[0], json.loads(item[1]), args.flags or []) for item in args.multi ]
             reply = ynl.do_multi(ops)
             output(reply)
-    except NlError as e:
-        print(e)
-        exit(1)
 
-    if args.ntf:
-        try:
+        if args.ntf:
             for msg in ynl.poll_ntf(duration=args.duration):
                 output(msg)
-        except KeyboardInterrupt:
-            pass
+    except NlError as e:
+        print(e)
+        exit(1)
+    except KeyboardInterrupt:
+        pass
+    except BrokenPipeError:
+        pass
 
 
 if __name__ == "__main__":