Skip to content

Close the socket when a reply is only partly read - #2961

Closed
SnowySailor wants to merge 4 commits into
phpredis:developfrom
SnowySailor:close-socket-on-partial-reply
Closed

SnowySailor wants to merge 4 commits into
phpredis:developfrom
SnowySailor:close-socket-on-partial-reply

Conversation

@SnowySailor

@SnowySailor SnowySailor commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2960

When a read fails or times out partway through a reply, phpredis throws but leaves the socket open. The rest of the reply arrives later, and the next command reads it as its own reply, so the connection stays out of step. This PR disconnects wherever phpredis gives 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()

A second commit also disconnects when a bulk length or terminator is invalid, because phpredis then no longer knows where the next reply starts:

  • an invalid bulk length in redis_sock_read_bulk_reply(), redis_sock_read_bulk_zstr() and redis_read_variant_bulk()
  • a bulk body that isn't 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()

Testing

  • The repro from Socket stays open after a read timeout in the middle of a reply, so every later command reads an earlier command's reply #2960 (Valkey 9.0 behind Toxiproxy), for Redis and RedisCluster, with binary and CRLF-text values: after the change, the command after the timeout reconnects and gets its own reply.
  • Two more delayed-reply cases that this fixes: a node-directed RedisCluster::echo(), and Redis::rawCommand() with a timeout before the type byte.
  • The same cases with TLS, persistent connections, and the igbinary serializer.
  • tests/TestRedis.php --class Redis and --class RedisCluster (6-node cluster with the phpredis ACL user, as in CI) give the same results with and without the change. The only failures are session tests, testFailedTransactions, and testQueuedReplyModes, which fail in my local environment on develop too.
  • For the second commit: a small fake server that sends a bulk body longer than its length, $-5, or $abc, called through get(), rawCommand() and mget(). Before the change, the next commands read the leftover bytes or an earlier command's reply. After it, the next command reconnects and gets its own reply. --class Redis (Valkey 9.0) and --class RedisCluster (one-node cluster) give the same results with and without this commit.

There is no new unit test, because the bug needs a server that stops partway through a reply. I can add one if you have a preferred way to do that in the test suite.

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 phpredis#2960
SnowySailor and others added 3 commits November 14, 2026 19:37
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.
@michael-grunder

Copy link
Copy Markdown
Member

Thanks for the PR!

It's got merge conflicts and I wasn't able to resolve them in GitHub's UI. Going to merge your commits in a new PR.

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