Blame SOURCES/0033-python-semanage-empty-stdout-before-exiting-on-Broke.patch

76f137
From 0bed778c53a4f93b1b092b3db33e8c36aabfa39d Mon Sep 17 00:00:00 2001
76f137
From: Vit Mojzis <vmojzis@redhat.com>
76f137
Date: Tue, 5 Jan 2021 17:00:21 +0100
76f137
Subject: [PATCH] python/semanage: empty stdout before exiting on
76f137
 BrokenPipeError
76f137
76f137
Empty stdout buffer before exiting when BrokenPipeError is
76f137
encountered. Otherwise python will flush the bufer during exit, which
76f137
may trigger the exception again.
76f137
https://docs.python.org/3/library/signal.html#note-on-sigpipe
76f137
76f137
Fixes:
76f137
   #semanage fcontext -l | egrep -q -e '^/home'
76f137
   BrokenPipeError: [Errno 32] Broken pipe
76f137
   Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
76f137
   BrokenPipeError: [Errno 32] Broken pipe
76f137
76f137
Note that the error above only appears occasionally (usually only the
76f137
first line is printed).
76f137
76f137
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
76f137
Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
76f137
---
76f137
 python/semanage/semanage | 8 ++++++++
76f137
 1 file changed, 8 insertions(+)
76f137
76f137
diff --git a/python/semanage/semanage b/python/semanage/semanage
76f137
index b2bd9df9..1abe3536 100644
76f137
--- a/python/semanage/semanage
76f137
+++ b/python/semanage/semanage
76f137
@@ -26,6 +26,7 @@
76f137
 import traceback
76f137
 import argparse
76f137
 import sys
76f137
+import os
76f137
 PROGNAME = "selinux-python"
76f137
 try:
76f137
     import gettext
76f137
@@ -953,6 +954,13 @@ def do_parser():
76f137
         args = commandParser.parse_args(make_args(sys.argv))
76f137
         args.func(args)
76f137
         sys.exit(0)
76f137
+    except BrokenPipeError as e:
76f137
+        sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
76f137
+        # Python flushes standard streams on exit; redirect remaining output
76f137
+        # to devnull to avoid another BrokenPipeError at shutdown
76f137
+        devnull = os.open(os.devnull, os.O_WRONLY)
76f137
+        os.dup2(devnull, sys.stdout.fileno())
76f137
+        sys.exit(1)
76f137
     except IOError as e:
76f137
         sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
76f137
         sys.exit(1)
76f137
-- 
76f137
2.29.2
76f137