Bug 1073165

Summary: subprocess.Popen.communicate() fails with: IOError: [Errno 4] Interrupted system call
Product: Red Hat Enterprise Linux 6 Reporter: Dan Callaghan <dcallagh>
Component: pythonAssignee: Robert Kuska <rkuska>
Status: CLOSED ERRATA QA Contact: Branislav NĂ¡ter <bnater>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.5CC: bkabrda, bnater, dkutalek, jberan, kdudka, rkuska
Target Milestone: rcKeywords: EasyFix, Patch
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: python-2.6.6-58.el6 Doc Type: Bug Fix
Doc Text:
Cause: subprocess.Popen.communicate() doesn't catch EINTR error if it is called without timeout and if there is only one PIPE (stdout or stderr) Consequence: subprocess.Popen.communicate() fails with IOError Fix: Make subprocess.Popen.communicate() handle EINTR errors if the process has only one pipe Result: subprocess.Popen.communicate() now works properly when only one pipe is used
Story Points: ---
Clone Of: Environment:
Last Closed: 2015-07-22 06:39:40 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Dan Callaghan 2014-03-05 23:26:25 UTC
Description of problem:
Popen.communicate() raises IOError when the underlying read fails with EINTR due to a signal handler firing. It only happens when one of stdout or stderr is piped (therefore a possible workaround is to pipe both stdout and stderr and ignore the one that is not needed).

We see this intermittently in our Beaker self-test suite, where we execute lsof and read its stdout.

Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/nose/suite.py", line 208, in run
    self.setUp()
  File "/usr/lib/python2.6/site-packages/nose/suite.py", line 291, in setUp
    self.setupContext(ancestor)
  File "/usr/lib/python2.6/site-packages/nose/suite.py", line 314, in setupContext
    try_run(context, names)
  File "/usr/lib/python2.6/site-packages/nose/util.py", line 469, in try_run
    return func()
  File "/usr/lib/python2.6/site-packages/bkr/inttest/__init__.py", line 337, in setup_package
    process.start()
  File "/usr/lib/python2.6/site-packages/bkr/inttest/__init__.py", line 148, in start
    self._wait_for_listen(self.listen_port)
  File "/usr/lib/python2.6/site-packages/bkr/inttest/__init__.py", line 158, in _wait_for_listen
    if check_listen(self.listen_port):
  File "/usr/lib/python2.6/site-packages/bkr/inttest/__init__.py", line 109, in check_listen
    stdout=subprocess.PIPE).communicate()
  File "/usr/lib64/python2.6/subprocess.py", line 723, in communicate
    stdout = self.stdout.read()
IOError: [Errno 4] Interrupted system call

The upstream bug is: http://bugs.python.org/issue12493
Fixed upstream in Python 2.7: http://hg.python.org/cpython/rev/6a28ccde2f1b

Version-Release number of selected component (if applicable):
python-2.6.6-52.el6.x86_64

How reproducible:
easily

Steps to Reproduce:
The upstream test suite has a reliable reproducer, here is an example based on that:

Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess, sys, signal
>>> def handler(signum, frame): pass
... 
>>> signal.signal(signal.SIGALRM, handler)
0
>>> p = subprocess.Popen('sleep 20', shell=True, stdout=subprocess.PIPE)
>>> signal.alarm(1) ; p.communicate()
0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/subprocess.py", line 723, in communicate
    stdout = self.stdout.read()
IOError: [Errno 4] Interrupted system call
>>> 

Actual results:
Popen.communicate() call raises IOError due to EINTR.

Expected results:
Popen.communicate() should retry after EINTR and successfully return a result.

Additional info:
Looks related to bug 1065537 (probably the upstream patch for this bug depends on that one).

Comment 2 Bohuslav "Slavek" Kabrda 2014-11-25 07:08:41 UTC
devel_ack+, we can fix this with the referenced patch.

Comment 5 errata-xmlrpc 2015-07-22 06:39:40 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://rhn.redhat.com/errata/RHSA-2015-1330.html