Skip to content

Commit

Permalink
Only close the underlying transport on errors
Browse files Browse the repository at this point in the history
The documentation says that the transport passed to `loop.start_tls()` shouldn't
be used after that:
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.start_tls
  • Loading branch information
webknjaz committed Sep 10, 2021
1 parent 31f2222 commit e76ef53
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,25 +1104,28 @@ async def _create_proxy_connection(
message=message,
headers=resp.headers,
)

setattr(
transport, "_start_tls_compatible", True
) # for https://github.com/python/cpython/pull/28073
# Wrap the raw TCP transport with TLS:
tls_transport = await self._loop.start_tls(
# Access the old transport last time before it's closed and forgotten forever:
transport,
self._factory(), # Create a brand new proto for TLS
sslcontext,
server_hostname=req.host,
ssl_handshake_timeout=timeout.total,
)
tls_transport.get_protocol().connection_made(
tls_transport
) # Kick the state machine of the new TLS protocol
return tls_transport, tls_transport.get_protocol()
finally:
except BaseException:
# It shouldn't be closed in finally because it's fed to `loop.start_tls()`
# and the docs say not to touch it after passing there.
transport.close()
raise

setattr(
transport, "_start_tls_compatible", True
) # for https://github.com/python/cpython/pull/28073
# Wrap the raw TCP transport with TLS:
tls_transport = await self._loop.start_tls(
# Access the old transport last time before it's closed and forgotten forever:
transport,
self._factory(), # Create a brand new proto for TLS
sslcontext,
server_hostname=req.host,
ssl_handshake_timeout=timeout.total,
)
tls_transport.get_protocol().connection_made(
tls_transport
) # Kick the state machine of the new TLS protocol
return tls_transport, tls_transport.get_protocol()
finally:
proxy_resp.close()

Expand Down

0 comments on commit e76ef53

Please sign in to comment.