Skip to content

Commit

Permalink
If TLS fails, then close the socket (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Jan 5, 2022
1 parent 55d03fc commit da99b46
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
20 changes: 12 additions & 8 deletions httpcore/backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ async def start_tls(
anyio.BrokenResourceError: ConnectError,
}
with map_exceptions(exc_map):
with anyio.fail_after(timeout):
ssl_stream = await anyio.streams.tls.TLSStream.wrap(
self._stream,
ssl_context=ssl_context,
hostname=server_hostname,
standard_compatible=False,
server_side=False,
)
try:
with anyio.fail_after(timeout):
ssl_stream = await anyio.streams.tls.TLSStream.wrap(
self._stream,
ssl_context=ssl_context,
hostname=server_hostname,
standard_compatible=False,
server_side=False,
)
except Exception as exc: # pragma: nocover
await self.aclose()
raise exc
return AsyncIOStream(ssl_stream)

def get_extra_info(self, info: str) -> typing.Any:
Expand Down
10 changes: 8 additions & 2 deletions httpcore/backends/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ def start_tls(
) -> NetworkStream:
exc_map = {socket.timeout: ConnectTimeout, socket.error: ConnectError}
with map_exceptions(exc_map):
self._sock.settimeout(timeout)
sock = ssl_context.wrap_socket(self._sock, server_hostname=server_hostname)
try:
self._sock.settimeout(timeout)
sock = ssl_context.wrap_socket(
self._sock, server_hostname=server_hostname
)
except Exception as exc: # pragma: nocover
self.close()
raise exc
return SyncStream(sock)

def get_extra_info(self, info: str) -> typing.Any:
Expand Down
8 changes: 6 additions & 2 deletions httpcore/backends/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ async def start_tls(
server_side=False,
)
with map_exceptions(exc_map):
with trio.fail_after(timeout_or_inf):
await ssl_stream.do_handshake()
try:
with trio.fail_after(timeout_or_inf):
await ssl_stream.do_handshake()
except Exception as exc: # pragma: nocover
await self.aclose()
raise exc
return TrioStream(ssl_stream)

def get_extra_info(self, info: str) -> typing.Any:
Expand Down

0 comments on commit da99b46

Please sign in to comment.