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

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