fix(tcpclient, iostream): close SSLIOStream on TLS handshake timeout to prevent fd leak#3636
Open
armorbreak001 wants to merge 1 commit into
Open
Conversation
…to prevent fd leak (tornado#3614) When TCPClient.connect() times out during the TLS handshake phase, start_tls() has already transferred socket ownership to a new SSLIOStream and set the original stream's socket to None. The caller's TimeoutError handler could only call stream.close(), which is a no-op since the socket is already gone. Meanwhile the SSLIOStream (holding the real fd) remains registered on the IOLoop forever — leaking the file descriptor. Fix: - In IOStream.start_tls(): store the new SSLIOStream as self._tls_stream so callers can reach it after timeout - In TCPClient.connect(): on gen.with_timeout TimeoutError during start_tls(), close stream._tls_stream to release the fd - Add regression test: TestTLSHandshakeTimeoutCleanup verifies no file descriptor leak after TLS handshake timeout This is a targeted fix with minimal API surface change. The _tls_stream attribute is only set during start_tls() and cleared after cleanup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (tornado#3614)
When
TCPClient.connect()is called with bothssl_optionsandtimeout, a TLS handshake timeout causes permanent file descriptor leak.Root Cause
start_tls()transfers socket ownership in three steps:IOStream→self.socket = NoneSSLIOStream(local variable only)Future[SSLIOStream]that resolves when handshake completesWhen
gen.with_timeout()fires:TimeoutError.socketis alreadyNone→close()is no-opSSLIOStream(holding the real fd) is only reachable through the inner Futuregen.with_timeoutexplicitly does not cancel the wrapped FutureImpact
Every timed-out TLS connection leaks one file descriptor. Long-running services that retry connections (e.g. HTTP clients with short timeouts) will eventually hit the OS fd limit (
EMFILE/ "too many open files").Fix
Two files, minimal change:
tornado/iostream.pyIn
start_tls(): store the SSLIOStream asself._tls_streambefore returning the future.tornado/tcpclient.pyIn
connect(): onTimeoutErrorduringstart_tls(), close_tls_stream.Testing
New test:
TestTLSHandshakeTimeoutCleanup.test_tls_handshake_timeout_closes_streamTCPClient.connect()with SSL + 0.01s timeoutTimeoutError/proc/self/fdcountAll existing tests pass:
tcpclient_test.py: ✅ 27 passed, 4 skippediostream_test.py: ✅ 142 passed, 61 skippedFixes #3614