Skip to contentSkip to conversation

python/semanage: empty stdout before exiting on BrokenPipeError

PATCH In review Vit Mojzis 3 participants 4 messages Jan 5, 2021 – Jan 14, 2021 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 python/semanage: empty stdout before exiting on BrokenPipeError

Empty stdout buffer before exiting when BrokenPipeError is encountered. Otherwise python will flush the bufer during exit, which may trigger the exception again.
https://docs.python.org/3/library/signal.html#note-on-sigpipe

Fixes:
#semanage fcontext -l | egrep -q -e '^/home'
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe

Note that the error above only appears occasionally (usually only the first line is printed).

---
 python/semanage/semanage | 8 ++++++++
 1 file changed, 8 insertions(+)
python/semanage/semanage +8 -0
index b2fabea6..ce15983b 100644
@@ -27,6 +27,7 @@ import traceback
 import argparse
 import seobject
 import sys
+import os
 PROGNAME = "policycoreutils"
 try:
     import gettext
@@ -945,6 +946,13 @@ def do_parser():
         args = commandParser.parse_args(make_args(sys.argv))
         args.func(args)
         sys.exit(0)
+    except BrokenPipeError as e:
+        sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
+        # Python flushes standard streams on exit; redirect remaining output
+        # to devnull to avoid another BrokenPipeError at shutdown
+        devnull = os.open(os.devnull, os.O_WRONLY)
+        os.dup2(devnull, sys.stdout.fileno())
+        sys.exit(1)
     except IOError as e:
         sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
         sys.exit(1)
Signature · 1 line
2.29.2
Acked-by on v1 1/1
#1 lore raw
Quoted 25 lines · Vit Mojzis

Empty stdout buffer before exiting when BrokenPipeError is encountered. Otherwise python will flush the bufer during exit, which may trigger the exception again.
https://docs.python.org/3/library/signal.html#note-on-sigpipe

Fixes:
#semanage fcontext -l | egrep -q -e '^/home'
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe

Note that the error above only appears occasionally (usually only the first line is printed).

---
 python/semanage/semanage | 8 ++++++++
 1 file changed, 8 insertions(+)
python/semanage/semanage +0 -0
index b2fabea6..ce15983b 100644

python/semanage/semanage import traceback · line 30

Quoted 4 lines
python/semanage/semanage +1 -0
@@ -27,6 +27,7 @@ import traceback
 import argparse
 import seobject
 import sys
+import os

Hello,
It would be nicer if the imports were imported in alphabetical order (also moving "import traceback" after "import sys"). But there also is an "import re" in the middle of the file, and while at it the Python2-compatibility layer in the gettext things could be dropped... so this should be done in other patches anyway. No need to change this patch.

Quoted 18 lines
python/semanage/semanage +7 -1
 PROGNAME = "policycoreutils"
 try:
     import gettext
@@ -945,6 +946,13 @@ def do_parser():
         args = commandParser.parse_args(make_args(sys.argv))
         args.func(args)
         sys.exit(0)
+    except BrokenPipeError as e:
+        sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
+        # Python flushes standard streams on exit; redirect remaining output
+        # to devnull to avoid another BrokenPipeError at shutdown
+        devnull = os.open(os.devnull, os.O_WRONLY)
+        os.dup2(devnull, sys.stdout.fileno())
+        sys.exit(1)
     except IOError as e:
         sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
         sys.exit(1)
--
Quoted 1 line

2.29.2

Doing open(os.devnull) + dup2 seems strange, but as this is precisely what is documented in the official Python documentation that you linked in the commit description, I am fine with this.

Thanks!
Nicolas

author
#2 lore raw

PATCH 2/2 python/semanage: Sort imports in alphabetical order

---
 python/semanage/semanage | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage +4 -4
index ce15983b..125271df 100644
@@ -23,11 +23,13 @@
 #
 #
 
-import traceback
 import argparse
+import os
+import re
 import seobject
 import sys
-import os
+import traceback
+
 PROGNAME = "policycoreutils"
 try:
     import gettext
@@ -798,8 +800,6 @@ def setupExportParser(subparsers):
     exportParser.add_argument('-f', '--output_file', dest='output_file', action=SetExportFile, help=_('Output file'))
     exportParser.set_defaults(func=handleExport)
 
-import re
-
 
 def mkargv(line):
     dquote = "\""
Signature · 1 line
2.29.2
Acked-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] python/semanage: Sort imports in alphabetical order

Quoted 1 line · Vit Mojzis

Both merged, thanks!

Quoted 35 lines
---
 python/semanage/semanage | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
python/semanage/semanage +4 -4
index ce15983b..125271df 100644
@@ -23,11 +23,13 @@
 #
 #
 
-import traceback
 import argparse
+import os
+import re
 import seobject
 import sys
-import os
+import traceback
+
 PROGNAME = "policycoreutils"
 try:
     import gettext
@@ -798,8 +800,6 @@ def setupExportParser(subparsers):
     exportParser.add_argument('-f', '--output_file', dest='output_file', action=SetExportFile, help=_('Output file'))
     exportParser.set_defaults(func=handleExport)
 
-import re
-
 
 def mkargv(line):
     dquote = "\""
Signature · 1 line
2.29.2