]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Graceful behavior for unclosed HTTPS connections 40/head
authorTom Christie <tom@tomchristie.com>
Wed, 8 May 2019 09:47:22 +0000 (10:47 +0100)
committerTom Christie <tom@tomchristie.com>
Wed, 8 May 2019 09:47:22 +0000 (10:47 +0100)
httpcore/backends/default.py

index b6acb7643ff777f4ed19554121cb51f7c84c0856..1e8996fba7691f624f440f4987c052eb88d07c9f 100644 (file)
@@ -20,6 +20,23 @@ from ..interfaces import BasePoolSemaphore, BaseReader, BaseWriter
 OptionalTimeout = typing.Optional[TimeoutConfig]
 
 
+# Monky-patch for https://bugs.python.org/issue36709
+#
+# This prevents console errors when outstanding HTTPS connections
+# still exist at the point of exiting.
+#
+# Clients which have been opened using a `with` block, or which have
+# had `close()` closed, will not exhibit this issue in the first place.
+
+_write = asyncio.selector_events._SelectorSocketTransport.write
+
+def _fixed_write(self, exc):
+    if not self._loop.is_closed():
+        _write(self, exc)
+
+asyncio.selector_events._SelectorSocketTransport.write = _fixed_write
+
+
 class Reader(BaseReader):
     def __init__(
         self, stream_reader: asyncio.StreamReader, timeout: TimeoutConfig
@@ -58,7 +75,7 @@ class Writer(BaseWriter):
 
         self.stream_writer.write(data)
         try:
-            data = await asyncio.wait_for(  # type: ignore
+            await asyncio.wait_for(  # type: ignore
                 self.stream_writer.drain(), timeout.write_timeout
             )
         except asyncio.TimeoutError:
@@ -121,6 +138,8 @@ async def connect(
         if ident is None:
             ident = ssl_object.selected_npn_protocol()
 
+    stream_writer.transport.set_write_buffer_limits(high=0, low=0)
+
     reader = Reader(stream_reader=stream_reader, timeout=timeout)
     writer = Writer(stream_writer=stream_writer, timeout=timeout)
     protocol = Protocol.HTTP_2 if ident == "h2" else Protocol.HTTP_11