asyncio: Fix upstream issue 168: StreamReader.read(-1) from pipe may hang if data exceeds buffer limit.

This commit is contained in:
Guido van Rossum 2014-05-12 10:04:37 -07:00
parent a869fd3dc0
commit bf88ffba5e
2 changed files with 47 additions and 6 deletions

View file

@ -419,12 +419,17 @@ class StreamReader:
return b''
if n < 0:
while not self._eof:
self._waiter = self._create_waiter('read')
try:
yield from self._waiter
finally:
self._waiter = None
# This used to just loop creating a new waiter hoping to
# collect everything in self._buffer, but that would
# deadlock if the subprocess sends more than self.limit
# bytes. So just call self.read(self._limit) until EOF.
blocks = []
while True:
block = yield from self.read(self._limit)
if not block:
break
blocks.append(block)
return b''.join(blocks)
else:
if not self._buffer and not self._eof:
self._waiter = self._create_waiter('read')