Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tornado/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,12 @@ def start_tls(
ssl_stream._ssl_connect_future = future
ssl_stream.max_buffer_size = self.max_buffer_size
ssl_stream.read_chunk_size = self.read_chunk_size

def _on_cancel(fut: Future[SSLIOStream]) -> None:
if fut.cancelled():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test to make sure we are in fact closing the stream here. I think that when tornado.gen.with_timeout reaches its timeout, the Future is terminated with a TimeoutError, but not cancelled, so this condition is never true (the call to tls_future.cancel() in tcpclient is a no-op because the future has already been terminated).

What we probably want to do here is close the stream in a done_callback if fut.exception() is None.

ssl_stream.close()

future.add_done_callback(_on_cancel) # type: ignore[arg-type]
return future

def _handle_connect(self) -> None:
Expand Down
18 changes: 9 additions & 9 deletions tornado/tcpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ async def connect(
# information here and re-use it on subsequent connections to
# the same host. (http://tools.ietf.org/html/rfc6555#section-4.2)
if ssl_options is not None:
tls_future = stream.start_tls(
False, ssl_options=ssl_options, server_hostname=host
)
if timeout is not None:
stream = await gen.with_timeout(
timeout,
stream.start_tls(
False, ssl_options=ssl_options, server_hostname=host
),
)
try:
stream = await gen.with_timeout(timeout, tls_future)
except TimeoutError:
tls_future.cancel()
raise
else:
stream = await stream.start_tls(
False, ssl_options=ssl_options, server_hostname=host
)
stream = await tls_future
return stream

def _create_stream(
Expand Down
Loading