Skip to content

Commit

Permalink
Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosslagerwall committed Apr 5, 2011
1 parent 45fdb45 commit 4f61b02
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
45 changes: 34 additions & 11 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ class Popen(args, bufsize=0, executable=None,
import traceback
import gc
import signal
import errno

# Exception classes used by this module.
class CalledProcessError(Exception):
Expand Down Expand Up @@ -358,7 +359,6 @@ class pywintypes:
else:
import select
_has_poll = hasattr(select, 'poll')
import errno
import fcntl
import pickle

Expand Down Expand Up @@ -699,7 +699,11 @@ def communicate(self, input=None):
stderr = None
if self.stdin:
if input:
self.stdin.write(input)
try:
self.stdin.write(input)
except IOError as e:
if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
raise
self.stdin.close()
elif self.stdout:
stdout = self.stdout.read()
Expand Down Expand Up @@ -929,7 +933,11 @@ def _communicate(self, input):

if self.stdin:
if input is not None:
self.stdin.write(input)
try:
self.stdin.write(input)
except IOError as e:
if e.errno != errno.EPIPE:
raise
self.stdin.close()

if self.stdout:
Expand Down Expand Up @@ -1290,9 +1298,16 @@ def close_unregister_and_remove(fd):
for fd, mode in ready:
if mode & select.POLLOUT:
chunk = input[input_offset : input_offset + _PIPE_BUF]
input_offset += os.write(fd, chunk)
if input_offset >= len(input):
close_unregister_and_remove(fd)
try:
input_offset += os.write(fd, chunk)
except OSError as e:
if e.errno == errno.EPIPE:
close_unregister_and_remove(fd)
else:
raise
else:
if input_offset >= len(input):
close_unregister_and_remove(fd)
elif mode & select_POLLIN_POLLPRI:
data = os.read(fd, 4096)
if not data:
Expand Down Expand Up @@ -1334,11 +1349,19 @@ def _communicate_with_select(self, input):

if self.stdin in wlist:
chunk = input[input_offset : input_offset + _PIPE_BUF]
bytes_written = os.write(self.stdin.fileno(), chunk)
input_offset += bytes_written
if input_offset >= len(input):
self.stdin.close()
write_set.remove(self.stdin)
try:
bytes_written = os.write(self.stdin.fileno(), chunk)
except OSError as e:
if e.errno == errno.EPIPE:
self.stdin.close()
write_set.remove(self.stdin)
else:
raise
else:
input_offset += bytes_written
if input_offset >= len(input):
self.stdin.close()
write_set.remove(self.stdin)

if self.stdout in rlist:
data = os.read(self.stdout.fileno(), 1024)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,25 @@ def test_handles_closed_on_exception(self):
self.assertFalse(os.path.exists(ofname))
self.assertFalse(os.path.exists(efname))

def test_communicate_epipe(self):
# Issue 10963: communicate() should hide EPIPE
p = subprocess.Popen([sys.executable, "-c", 'pass'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
self.addCleanup(p.stdout.close)
self.addCleanup(p.stderr.close)
self.addCleanup(p.stdin.close)
p.communicate(b"x" * 2**20)

def test_communicate_epipe_only_stdin(self):
# Issue 10963: communicate() should hide EPIPE
p = subprocess.Popen([sys.executable, "-c", 'pass'],
stdin=subprocess.PIPE)
self.addCleanup(p.stdin.close)
time.sleep(2)
p.communicate(b"x" * 2**20)

#
# POSIX tests
#
Expand Down
2 changes: 2 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Core and Builtins
Library
-------

- Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.

- Issue #11696: Fix ID generation in msilib.

- Issue #9696: Fix exception incorrectly raised by xdrlib.Packer.pack_int when
Expand Down

0 comments on commit 4f61b02

Please sign in to comment.