Commit fbe0586
Fix bad slot used in sharded pubsub unsubscribe (valkey-io#2137)
This commit fixes two issues in `pubsubUnsubscribeChannel` that could
lead to memory corruption:
1. When calculating the slot for a channel, we were using getKeySlot()
which might use the current_client's slot if available. This is
problematic when a client kills another client (e.g., via CLIENT KILL
command) as the slot won't match the channel's actual slot.
2. The `found` variable was not initialized to `NULL`, causing the
serverAssert to potentially pass incorrectly when the hashtable lookup
failed, leading to memory corruption in subsequent operations.
The fix:
- Calculate the slot directly from the channel name using keyHashSlot()
instead of relying on the current client's slot
- Initialize 'found' to NULL
Added a test case that reproduces the issue by having one client kill
another client that is subscribed to a sharded pubsub channel during a
transaction.
Crash log (After initializing the variable 'found' to null, without
initialization, memory corruption could occur):
```
VALKEY BUG REPORT START: Cut & paste starting from here ===
59707:M 24 May 2025 23:10:40.429 # === ASSERTION FAILED CLIENT CONTEXT ===
59707:M 24 May 2025 23:10:40.429 # client->flags = 108086391057154048
59707:M 24 May 2025 23:10:40.429 # client->conn = fd=11
59707:M 24 May 2025 23:10:40.429 # client->argc = 0
59707:M 24 May 2025 23:10:40.429 # === RECURSIVE ASSERTION FAILED ===
59707:M 24 May 2025 23:10:40.429 # ==> pubsub.c:348 'found' is not true
------ STACK TRACE ------
Backtrace:
0 valkey-server 0x0000000104974054 _serverAssertWithInfo + 112
1 valkey-server 0x000000010496c7fc pubsubUnsubscribeChannel + 268
2 valkey-server 0x000000010496cea0 pubsubUnsubscribeAllChannelsInternal + 216
3 valkey-server 0x000000010496c2e0 pubsubUnsubscribeShardAllChannels + 76
4 valkey-server 0x000000010496c1d4 freeClientPubSubData + 60
5 valkey-server 0x00000001048f3cbc freeClient + 792
6 valkey-server 0x0000000104900870 clientKillCommand + 356
7 valkey-server 0x00000001048d1790 call + 428
8 valkey-server 0x000000010496ef4c execCommand + 872
9 valkey-server 0x00000001048d1790 call + 428
10 valkey-server 0x00000001048d3a44 processCommand + 5056
11 valkey-server 0x00000001048fdc20 processCommandAndResetClient + 64
12 valkey-server 0x00000001048fdeac processInputBuffer + 276
13 valkey-server 0x00000001048f2ff0 readQueryFromClient + 148
14 valkey-server 0x0000000104a182e8 callHandler + 60
15 valkey-server 0x0000000104a1731c connSocketEventHandler + 488
16 valkey-server 0x00000001048b5e80 aeProcessEvents + 820
17 valkey-server 0x00000001048b6598 aeMain + 64
18 valkey-server 0x00000001048dcecc main + 4084
19 dyld 0x0000000186b34274 start + 2840
````
---------
Signed-off-by: Uri Yagelnik <[email protected]>
(cherry picked from commit bd5dcb2)1 parent a50b634 commit fbe0586
2 files changed
+28
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
310 | | - | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
311 | 313 | | |
312 | 314 | | |
313 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
56 | 81 | | |
0 commit comments