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

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