242 Commits

Author SHA1 Message Date
Rasmus Lerdorf
5ebb853e1a Fix segfault during Redis Cluster failover
When a Redis Cluster failover occurs, the client detects that the
redirected node was a replica of the old master and calls
cluster_map_keyspace() to remap the cluster topology.

If cluster_map_keyspace() fails (e.g., due to network issues during
the remap), it frees all node objects via zend_hash_clean(c->nodes)
and zeros the master array via memset(c->master, 0, ...).

The bug was that the return value of cluster_map_keyspace() was being
ignored in the failover detection path. This caused the code to
continue with NULL socket pointers, leading to segfaults when
dereferencing c->cmd_sock later.

This fix:
1. Checks the return value of cluster_map_keyspace() in failover
   detection and returns FAILURE if it fails
2. Adds defense-in-depth NULL checks after MOVED and ASK redirections
   to prevent segfaults if slots become NULL for any reason

Fixes production crashes observed during Redis Cluster failovers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-05 21:13:23 -07:00
Christian Flothmann
25e6d5fcc2 fix typo (s/sees/seeds/) 2025-09-07 14:34:05 -07:00
michael-grunder
c4b2ea6ca5 Fix VEMB reply handling in RedisCluster 2025-09-01 09:41:12 -07:00
michael-grunder
d80b725824 Implement VGETATTR command 2025-09-01 09:41:12 -07:00
michael-grunder
7f9b1f416e Implement VLINKS command 2025-09-01 09:41:12 -07:00
michael-grunder
1deca62841 Implement VRANDMEMBER
`VRANDMEMBER` has the exact same semantics of `SRANDMEMBER` so make
`SRANDMEMBER` a keyword based command and use it for `VRANDMEMBER`.

See #2543
2025-09-01 09:41:12 -07:00
michael-grunder
0fda9f293b Implement VCARD, VDIM, and VINFO
All of these commands have the same form `<cmd> key`. `VINFO` is a bit
of an outlier however that uses simple strings as opposed to bulk
strings for the key names, meaning we had to create a custom handler.

See #2543
2025-09-01 09:41:12 -07:00
michael-grunder
0ed0fc0562 Add Redis::REDIS_VECTORSET type.
Redis >= 8.0 has a new type `vectorset` that we should support like all
the other types.
2025-08-28 09:34:07 -07:00
Jacob Brown
b0ba827be2 Fixing segfault in cluster_update_slot
When the slot's->slaves was null, it was dereferencing null, causing
segfault.
This happens in weird scenario when some of the nodes in cluster are
down or having changed IP addresses without knowing about it.
2025-08-22 11:59:38 -07:00
michael-grunder
03837f0230 Remove pointless casts
You never have to explicitly cast between `void*` and any other pointer
type.
2025-08-21 08:53:51 -07:00
michael-grunder
8be2306e4f Fix several issues surfaced by gcc -fanzlyze
Mostly null pointer derefs or use of uninitialized values. Some were
probably false positives since hte analyzer can't fully reason about how
the zend internals use `zval` structs but the fixes don't really have
any downside.
2025-08-15 07:52:21 -07:00
Michael Grunder
ce5b0facc2 Implement HGETEX, HSETEX, HGETDEL, and refactor HMGET (#2667)
* Rework HMGET and implement HGETEX

Instead of using a bespoke NULL terminated `zval**` array for the
context array we can use a `HashTable`. This might be a tiny bit more
expensive but Zend hashtables are quite efficient and this should also
be less error prone.

* Rework our `HashTable` context array to store keys

Instead of sending an array of values we can instead add the fields as
keys to our context array. That way when we combine the keys with the
Redis provided values we can do it in-place and then just give the
HashTable to the user to then do with what they want.

* Implement HGETDEL command.

* Fix edge cases to abide by legacy behavior.

Previously we coerced integer strings into integer keys when zipping
`HMGET` responses. This commit adds logic so we continue to do this and
do not change semantics.

* Implement `HGETDEL` and `HGETEX` for `RedisCluster`.

This commit implements the new commands and reworks the `HMGET` reply
handler to use the new context `HashTable`.

* Fix an edge case where we get zero multiblk elements

* Tests for `HGETEX` and `HGETDEL`

* Minor logic improvement

We don't need to check if `c->reply_len > 0` in the last else block
since we have already determined it must be.

* Implement `HSETEX` for `Redis` and `RedisCluster`

* Use `zval_get_tmp_string` ro populating non-long keys
2025-07-16 16:46:09 -07:00
michael-grunder
b48aa0d471 Fix an unused variable warning 2025-04-20 10:18:30 -07:00
michael-grunder
5208818e8c We can use zval_get_tmp_string here 2025-04-02 13:02:52 -07:00
Jakub Onderka
0a85bd824a Simplify redis_unpack method calling
This method always unpack given string to zval, so it is not necessary to check output value
2025-04-02 13:02:52 -07:00
Michael Grunder
0445e683e7 Refactor getWithMeta logic (#2643)
* Refactor `getWithMeta`

* Consolidate `getWithMeta()` test.

* Review comments
2025-03-31 12:42:29 -07:00
Pavlo Yatsukhnenko
9036ffca6a Add getWithMeta method 2025-02-25 16:27:10 +02:00
James Kennedy
c7b8784310 Invalidate slot cache on failed cluster connections 2025-01-20 15:34:53 -08:00
michael-grunder
085d61ecfb Create a strncmp wrapper
On some glibc implementations strncmp is a macro. This commit simply creates a
`redis_strncmp` static inline wrapper function so we can `ZEND_STRL` instead of
manually counting the length or using `sizeof(s)-1` each time.

Fixes #2565
2024-10-15 10:40:15 -07:00
michael-grunder
a75a7e5a36 Fix SIGABRT in PHP 8.4
PHP switched from `ZEND_ASSUME` to `ZEND_ASSERT` when making sure
`Z_PTR_P(zv)` was nonnull in `zend_hash_str_update_ptr`.

This commit just switches to `zend_hash_str_add_empty_element` which
is semantically more correct anyway.

Fixes #2539
2024-09-19 22:41:28 -07:00
michael-grunder
f8c762e70b Use ZEND_STRL where appropriate.
Use the `ZEND_STRL` macro in several places rather than manually sending
a static string and its length as a constant.
2024-06-01 13:41:26 -07:00
Jozsef Koszo
eb7f31e7af Fix random connection timeouts with Redis Cluster
When a node timeout occurs, then phpredis will try to connect to another
node, whose answer probably will be MOVED redirect. After this we need
more time to accomplish the redirection, otherwise we get "Timed out
attempting to find data in the correct node" error message.

Fixes #795 #888 #1142 #1385 #1633 #1707 #1811 #2407
2024-03-25 12:04:06 -07:00
michael-grunder
2612d444e5 Update RedisCluster scan logic for large SCAN cursors.
We also need to update the `RedisCluster` logic to handle very large
curosr values, in addition to handling them for the `Redis` and
`RedisArray` classes.

See #2454, #2458
2024-03-18 11:54:02 -07:00
Viktor Szépe
37c5f8d451 Fix typos 2024-02-21 13:16:12 -08:00
thomaston
a3327d9d2b Fix bug: the pipeline mode socket return an unexpected result after reconnecting (#2358)
* fix bug: the pipeline mode socket return an unexpected result after reconnecting

* fix typos: pipeline is right

---------

Co-authored-by: marcofu <marcofu@tencent.com>
2023-07-20 11:48:53 -07:00
michael-grunder
ccd419a4c8 Small refactor of some methods
* Use our `redis_cmd_append_sstr_key_*` and `redis_cmd_append_sstr_zval`
  wrappers, which handle key prefixing and serialization transparently.

* Rework ZADD so it can handle the bulk double response from the `INCR`
  options.
2023-03-01 11:45:47 -08:00
michael-grunder
02c91d59cb Fix RPOP to unserialize/decompress data.
Fixes #2329
2023-02-24 12:01:43 -08:00
michael-grunder
acb5db7661 Refactor OBJECT command. 2022-12-06 12:42:29 -08:00
michael-grunder
f62363c2a3 Refactor SRANDMEMBER command. 2022-12-06 10:46:32 -08:00
michael-grunder
27900f39d2 Implement new ZSET commands for cluster
* Implement `ZDIFF`, `ZINTER`, `ZUNION`, `ZMSCORE`, and
  `ZRANDMEMBER` for `RedisCluster`.
* Refactor `ZUNIONSTORE` command and switch to using our centralized
  zset option parsing handler.

See #1894
2022-12-02 09:11:00 -08:00
michael-grunder
e222b85ecf Implement HRANDFIELD for RedisCluster
See #1894
2022-12-01 22:12:19 -08:00
Michael Grunder
fa5d1af9ff Implement GEOSEARCH and GEOSEARCHSTORE for RedisCluster. (#2277)
* Implement GEOSEARCH and GEOSEARCHSTORE for RedisCluster.

See #1894
2022-12-01 21:54:15 -08:00
michael-grunder
7121aaae5c Implement LPOS for RedisCluster
See #1894
2022-12-01 14:05:43 -08:00
michael-grunder
90828019de Refactor network IO tracking.
* Create inline wrappers of the low-level php_stream_* functions that
  also keep track of the number of bytes written/read.

* Change the logic to aggregate network traffic until the user
  explicitly "resets" it.  I think this will be a more common use-case
  (running many commands and then seeing overall network IO).

See #2106
2022-11-25 13:41:47 -08:00
michael-grunder
0b7bd83f57 Add more docblocks and fix XAUTOCLAIM response handler.
- Finish adding docblocks with examples for all of the stream commands.
- Fix XAUTOCLAIM response handler (the reply has a slightly different
  structure to XCLAIM.
2022-11-15 23:29:45 -08:00
Pavlo Yatsukhnenko
1d6c52ee39 Small cosmetic changes in cluster_library 2022-10-26 12:16:32 -07:00
Pavlo Yatsukhnenko
e0a88b7bdf Issue #2106
Expose the transferred number of bytes
2022-10-24 09:18:19 +03:00
michael-grunder
71bcbcb973 Implement ZRANGESTORE and add ZRANGE options
* Add ZRANGESTORE command.

* Add Redis 6.2's `REV`, `BYLEX`, and `BYSCORE` to ZRANGE options.

* Refactor several ZRANGE family commands into a single reply and
  options handler, using PHP's new argument parsing macros.

* Extend our tests to use the new ZRANGE options.

See #1894
2022-10-22 12:46:01 -07:00
michael-grunder
6ea978eb72 [B]LMPOP and [B]ZMPOP commands
Implement the new Redis 7.0.0 commands to pop multiple elements from one
or more lists/zsets.

Additionally, remove INTERNAL_FUNCTION_PARAMETERS from the
redis_sock_read_multibulk_reply_zval helper function as it wasn't
actually being used.
2022-10-08 09:23:13 -07:00
michael-grunder
ab4ce4ab6f Allow negative timeout/read_timeout and fix doc
Co-authored-by: twosee <twose@qq.com>
2022-10-07 11:42:38 -07:00
michael-grunder
6430050808 SINTERCARD and ZINTERCARD commands
Implement Redis 7.0.0 commands SINTERCARD and ZINTERCARD.
2022-10-01 10:42:23 -07:00
Marius Adam
f2bfd72357 Issue #2080: avoid registering the same replicas multiple times in case the master handles multiple slot ranges 2022-09-19 12:09:35 -07:00
Michael Grunder
b3ce048669 Fix non standards conforming prototypes. (#2150)
These now generate warnings with GCC 13
2022-09-14 22:54:22 -07:00
Pavlo Yatsukhnenko
3c9e159c7e Refactor subscribe/unsubscribe 2022-06-28 18:18:38 +03:00
Pavlo Yatsukhnenko
df97cc3531 Issue #1894
Add the COUNT argument to LPOP and RPOP
2022-06-07 18:51:55 +03:00
Pavlo Yatsukhnenko
5a269ab6d0 Don't allow reconnect on read response 2022-04-03 19:52:18 +03:00
Michael Grunder
a64a0c3785 Fix fallthrough warnings and refactor adding a score (#2049)
* Suppress implicit fallthrough warnings by using an attribute if we
  have it and a do { } while(0) if we don't.

* Move duplicated logic for appending a ZSET score to one utility
  function.
2022-01-20 10:54:57 -08:00
Michael Grunder
cfc81c90fd Fix typo/bug in cluster_scan_resp (#2045) 2021-12-18 11:32:31 -08:00
michael-grunder
53db4b3e2f Whitespace 2021-11-09 13:23:47 -08:00
Bar Shaul
12bf3fddac Added support for remapping the cluster's keyspace on a failover 2021-10-28 12:10:55 +03:00