Skip to content

Bound the SSL handshake by socket_connect_timeout - #4471

Merged
petyaslavova merged 3 commits into
redis:masterfrom
kmecpp:master
Oct 22, 2026
Merged

petyaslavova merged 3 commits into
redis:masterfrom
kmecpp:master

Conversation

@kmecpp

@kmecpp kmecpp commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Description of change

SSLConnection currently runs the TLS handshake under socket_timeout instead of
socket_connect_timeout. This means every new connection has
to finish a full TLS handshake inside a budget sized for a single command which can be problematic with a low socket_timeout

This change sets socket_connect_timeout on the socket before wrap_socket()
and restores socket_timeout afterwards, 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() sets
socket_connect_timeout before wrap_socket() and restores socket_timeout
afterwards. Nothing changes when socket_connect_timeout is unset, since it
defaults to socket_timeout.

Pull Request check-list

Please make sure to review and check all of these items:

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

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 SSLConnection now limits the TLS handshake with socket_connect_timeout instead of socket_timeout.

In _wrap_socket_with_ssl(), the plain socket is set to socket_connect_timeout before wrap_socket() (handshake), then the wrapped socket is reset to socket_timeout for 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_timeout is 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-10-22T09:09:58.450142Z 656edff New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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 petyaslavova left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@petyaslavova
petyaslavova merged commit 04472ca into redis:master Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants