Repository navigation
Bound the SSL handshake by socket_connect_timeout - #4471
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
βΉοΈ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with π while any review is running, comments if it has suggestions, and reacts with π once all reviews finish with no findings. |
chrikrah
left a comment
There was a problem hiding this comment.
@kmecpp against a local TLS listener that waits 0.5 s before its handshake, socket_timeout=0.2, socket_connect_timeout=2 now connects at f8e16d3, where 547efc3 times out after 0.2 s. Your test fails against the old connection.py, and nothing below blocks a merge.
non-blocking: the description says nothing changes when socket_connect_timeout is unset, because it falls back to socket_timeout. That fallback only fires for an explicit None. Since #4082 both default to 5 on their own, so Redis(ssl=True, socket_timeout=8) now gets a 5 s handshake budget, and socket_timeout=None stops meaning an unbounded handshake. That matches the asyncio client, and the change should stay. It still belongs in the release notes, because a slow TLS terminator behind a long socket_timeout will start to see handshake timeouts after an upgrade.
$ .venv/bin/python probe4471.py c.pem k.pem # SSLConnection._connect() against a local TLS listener, Python 3.12.6
# head f8e16d3
handshake 0.5s, socket_timeout=0.2 connect_timeout=2 -> ok, timeout after=0.2 (0.5s)
handshake 6s, socket_timeout=8 connect_timeout unset -> TimeoutError: _ssl.c:983: The handshake operation timed out (5.0s)
# base 547efc3
handshake 0.5s, socket_timeout=0.2 connect_timeout=2 -> TimeoutError: _ssl.c:983: The handshake operation timed out (0.2s)
handshake 6s, socket_timeout=8 connect_timeout unset -> ok, timeout after=8.0 (6.0s)
$ .venv/bin/python -m pytest tests/test_connection.py -q -p no:randomly -m fixed_client # f8e16d3, no Redis server
3 failed, 320 passed, 12 skipped, 10 deselected, 2 errors
# connection.py from 547efc3, test kept: 4 failed, 319 passed. The extra one is
# test_ssl_handshake_uses_socket_connect_timeout: assert [0.05] == [4]
# the other 3 failures and 2 errors are Connection refused on localhost:6379 in both runs
$ .venv/bin/ruff check redis/connection.py tests/test_connection.py # ruff 0.9.6, the dev_requirements pin
All checks passed!
@petyaslavova, #4082 set the two 5 s defaults that make this a visible change. Is a line in the next release notes enough for you, or must callers who never pass socket_connect_timeout keep the old handshake budget?
petyaslavova
left a comment
There was a problem hiding this comment.
Hey @kmecpp, thank you for your contribution!
This is a real gap: the sync TCP connect, the Unix socket connect and the whole async SSL connect are already bounded by socket_connect_timeout, and only the sync TLS handshake ran under socket_timeout. The fix follows the same pattern as the TCP connect, and the test covers it.
@chrikrah, thanks for the probe. A line in the release notes is enough. I don't want to keep the old handshake budget for callers who never pass socket_connect_timeout: their TCP connect already uses that budget, and keeping the old behavior would leave sync and async different. I'll label this as a bug fix so the timeout change shows up in the release notes.
Description of change
SSLConnectioncurrently runs the TLS handshake undersocket_timeoutinstead ofsocket_connect_timeout. This means every new connection hasto finish a full TLS handshake inside a budget sized for a single command which can be problematic with a low
socket_timeoutThis change sets
socket_connect_timeouton the socket beforewrap_socket()and restores
socket_timeoutafterwards, so the handshake is bounded by the connect timeout while commands keep the read timeout. A low socket_timeout can then be used to cap command latency without making new connections fail during the handshake.The asyncio client already bounds the handshake with
socket_connect_timeout.This change makes the sync client match:
_wrap_socket_with_ssl()setssocket_connect_timeoutbeforewrap_socket()and restoressocket_timeoutafterwards. Nothing changes when
socket_connect_timeoutis unset, since itdefaults to
socket_timeout.Pull Request check-list
Please make sure to review and check all of these items:
Note
Low Risk
Small, localized change to SSL connect timing with a regression test; behavior is unchanged when connect timeout defaults to read timeout.
Overview
Sync
SSLConnectionnow limits the TLS handshake withsocket_connect_timeoutinstead ofsocket_timeout.In
_wrap_socket_with_ssl(), the plain socket is set tosocket_connect_timeoutbeforewrap_socket()(handshake), then the wrapped socket is reset tosocket_timeoutfor command I/O. That matches plain TCP connect behavior and the asyncio client, so a short read timeout no longer causes new TLS connections to fail during handshake.A unit test asserts the connect timeout is active during wrap and
socket_timeoutis applied on the SSL socket afterward.Reviewed by Cursor Bugbot for commit f8e16d3. Bugbot is set up for automated code reviews on this repo. Configure here.