Skip to content

Close socket on partial reply - #3005

Merged
michael-grunder merged 3 commits into
developfrom
close-socket-on-partial-reply
Nov 15, 2026
Merged

michael-grunder merged 3 commits into
developfrom
close-socket-on-partial-reply

Conversation

@michael-grunder

Copy link
Copy Markdown
Member

This is just #2961 that has been rebased to fix merge conflicts.

Fixes #2960

SnowySailor and others added 3 commits November 14, 2026 15:31
When a read fails or times out in the middle of a reply, the rest of
the reply can still arrive. If the socket stays open, the next command
reads it as its own reply and the connection stays out of step.

Disconnect wherever we give up before the end of a reply, the same way
redis_sock_gets() already does:

* a short read in redis_sock_read_bulk_reply() and
  redis_sock_read_bulk_zstr()
* a timeout before the reply type byte in redis_read_reply_type()
* an unknown reply type byte in redis_sock_read() and
  redis_sock_read_zstr()
* any failed read in cluster_check_response(), which now also rejects
  unknown reply type bytes
* a mismatched reply in cluster_send_direct(), and a mismatched reply
  with an unread payload in cluster_send_slot()

Fixes #2960
A bulk length that doesn't parse or is out of range, or a bulk body that
doesn't end in \r\n, means we no longer know where the next reply
starts. Throwing or returning NULL with the socket still open lets the
next command read the rest of this reply as its own.

Disconnect in these cases as well:

* an invalid bulk length in redis_sock_read_bulk_reply(),
  redis_sock_read_bulk_zstr() and redis_read_variant_bulk()
* a bulk body not followed by \r\n in redis_sock_read_bulk_reply() and
  redis_sock_read_bulk_zstr()
* a `$` length that doesn't parse or is out of range in
  redis_sock_read() and redis_sock_read_zstr()
* a length that doesn't parse or is out of range in
  redis_read_reply_type()

Thanks to cyppe for finding the invalid bulk length and missing \r\n
checks in redis_sock_read_bulk_reply().
Previously we could leak our running transaction data if we disconnected
during a transaction.

Also don't accept partial cluster transaction replies.

Signed-off-by: michael-grunder <michael.grunder@gmail.com>
@michael-grunder
michael-grunder merged commit f32c87e into develop Nov 15, 2026
94 checks passed
@michael-grunder
michael-grunder deleted the close-socket-on-partial-reply branch November 15, 2026 00:12
@SnowySailor SnowySailor mentioned this pull request Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socket stays open after a read timeout in the middle of a reply, so every later command reads an earlier command's reply

2 participants