summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2019-05-07 20:53:19 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-05-07 20:53:19 (GMT)
commit1cc0ee7d9f6a2817918fafd24c18d8bb093a85d3 (patch)
tree03a2e1f32fb51d8fc1c8e8ce8ebd64f672de8709 /Lib/asyncio
parente19a91e45fd54a56e39c2d12e6aaf4757030507f (diff)
downloadcpython-1cc0ee7d9f6a2817918fafd24c18d8bb093a85d3.zip
cpython-1cc0ee7d9f6a2817918fafd24c18d8bb093a85d3.tar.gz
cpython-1cc0ee7d9f6a2817918fafd24c18d8bb093a85d3.tar.bz2
bpo-36801: Fix waiting in StreamWriter.drain for closing SSL transport (GH-13098)
https://bugs.python.org/issue36801
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/streams.py21
-rw-r--r--Lib/asyncio/subprocess.py9
2 files changed, 22 insertions, 8 deletions
diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py
index c9b1f32..79adf02 100644
--- a/Lib/asyncio/streams.py
+++ b/Lib/asyncio/streams.py
@@ -199,6 +199,9 @@ class FlowControlMixin(protocols.Protocol):
self._drain_waiter = waiter
await waiter
+ def _get_close_waiter(self, stream):
+ raise NotImplementedError
+
class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
"""Helper class to adapt between Protocol and StreamReader.
@@ -315,6 +318,9 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
return False
return True
+ def _get_close_waiter(self, stream):
+ return self._closed
+
def __del__(self):
# Prevent reports about unhandled exceptions.
# Better than self._closed._log_traceback = False hack
@@ -376,7 +382,7 @@ class StreamWriter:
return self._transport.is_closing()
async def wait_closed(self):
- await self._protocol._closed
+ await self._protocol._get_close_waiter(self)
def get_extra_info(self, name, default=None):
return self._transport.get_extra_info(name, default)
@@ -394,13 +400,12 @@ class StreamWriter:
if exc is not None:
raise exc
if self._transport.is_closing():
- # Yield to the event loop so connection_lost() may be
- # called. Without this, _drain_helper() would return
- # immediately, and code that calls
- # write(...); await drain()
- # in a loop would never call connection_lost(), so it
- # would not see an error when the socket is closed.
- await sleep(0, loop=self._loop)
+ # Wait for protocol.connection_lost() call
+ # Raise connection closing error if any,
+ # ConnectionResetError otherwise
+ fut = self._protocol._get_close_waiter(self)
+ await fut
+ raise ConnectionResetError('Connection lost')
await self._protocol._drain_helper()
async def aclose(self):
diff --git a/Lib/asyncio/subprocess.py b/Lib/asyncio/subprocess.py
index fa58e1e..d34b611 100644
--- a/Lib/asyncio/subprocess.py
+++ b/Lib/asyncio/subprocess.py
@@ -26,6 +26,7 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
self._transport = None
self._process_exited = False
self._pipe_fds = []
+ self._stdin_closed = self._loop.create_future()
def __repr__(self):
info = [self.__class__.__name__]
@@ -80,6 +81,10 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
if pipe is not None:
pipe.close()
self.connection_lost(exc)
+ if exc is None:
+ self._stdin_closed.set_result(None)
+ else:
+ self._stdin_closed.set_exception(exc)
return
if fd == 1:
reader = self.stdout
@@ -106,6 +111,10 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
self._transport.close()
self._transport = None
+ def _get_close_waiter(self, stream):
+ if stream is self.stdin:
+ return self._stdin_closed
+
class Process:
def __init__(self, transport, protocol, loop, *, _asyncio_internal=False):