Repository navigation
Fixes server crash when RDMA benchmark clients disconnect - #3448
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #3448 +/- ##
============================================
+ Coverage 76.53% 76.57% +0.03%
============================================
Files 157 157
Lines 79045 79048 +3
============================================
+ Hits 60501 60529 +28
+ Misses 18544 18519 -25
🚀 New features to boost your workflow:
|
|
@pizhenwei take a loot at this? thanks! |
pizhenwei
left a comment
There was a problem hiding this comment.
There are 3 independent fixes in this PR, I suggest that to fix the unexpected crash (src/rdma.c) only in this PR. And open a new PR to fix src/valkey-benchmark.c.
I believe that you point out the root cause of losing wakeup event in the client side, I guess it's also possible to lose wakeup event in current fix (deps/libvalkey/src/rdma.c), I'll also open a new PR to discuss it later.
e4ab963 to
5933bbc
Compare
Per review feedback in PR valkey-io#3448, splitting this PR to focus only on the server-side crash fix in src/rdma.c: - deps/libvalkey/src/rdma.c: Maintainer will address in separate PR - src/valkey-benchmark.c: Will be submitted in new PR for client-side fix This PR now contains only the server-side RDMA connection crash fix.
Per review feedback in PR valkey-io#3448, splitting this PR to focus only on the server-side crash fix in src/rdma.c: - deps/libvalkey/src/rdma.c: Maintainer will address in separate PR - src/valkey-benchmark.c: Will be submitted in new PR for client-side fix This PR now contains only the server-side RDMA connection crash fix. Signed-off-by: Ada-Church-Closure <[email protected]>
5933bbc to
7c96a6f
Compare
When RDMA benchmark clients disconnect (e.g., via Ctrl+C), the server could crash with SIGSEGV due to connections being added to pending_list multiple times, leading to use-after-free. Root Cause: ----------- The same RDMA connection could be added to pending_list multiple times: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again 3. In rdmaProcessPendingData(), the first iteration may free the connection, but the second iteration accesses the freed connection → SIGSEGV at 0x8 The problem was exacerbated because valkey-benchmark creates multiple concurrent connections that disconnect nearly simultaneously on Ctrl+C, and event callbacks can re-add connections before processing completes. Solution: --------- Four targeted fixes in src/rdma.c ensure each connection appears in pending_list at most once by using pending_list_node as both a list pointer and an "already in list" flag: 1. rdmaHandleDisconnect (line 535): Check if pending_list_node is NULL before adding to prevent duplicates 2. connRdmaSetWriteHandler (line 958): Add same check before adding during write handler updates 3. rdmaProcessPendingData (line 1790): Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if connection was re-added 4. rdmaProcessPendingData (line 1783-1790): Call handlers before removing from list to ensure connection structure remains valid Fixes valkey-io#3345 Signed-off-by: Ada-Church-Closure <[email protected]>
7c96a6f to
782f472
Compare
|
Taking a look |
| returns 0, indicating the connection has closed, in which case | ||
| write_handler will be skipped. */ | ||
| if (callHandler(conn, conn->read_handler)) { | ||
| callHandler(conn, conn->write_handler); |
There was a problem hiding this comment.
My understanding is that the callHandler here is readQueryFromClient. This call handler can free the RDMA connection if connection is in disconnect state via this flow: readQueryFromClient -> handleReadResult -> freeClientAsync -> close_asap?
So it's possible that the before the callHandler is completed, the RDMA connection is already free? Then could it be an issue on how are we clearing the pending_list ahead.
There was a problem hiding this comment.
Thanks for the review! I traced through the code path carefully,and if I miss something,please correct me.
freeClientAsync only enqueues the client to server.clients_to_close and sets close_asap = 1. The actual freeClient → connClose → zfree(conn) happens later in freeClientsInAsyncFreeQueue(), which is called after rdmaProcessPendingData() returns in beforeSleep().
Additionally, callHandler would only trigger an immediate connClose if CONN_FLAG_CLOSE_SCHEDULED is set, which only happens when connClose() is called directly while refs > 0. That does not occur in the readQueryFromClient → freeClientAsync path.
Therefore rdma_conn remains valid after callHandler returns, and the subsequent listDelNode pending_list_node = NULL is safe.
There was a problem hiding this comment.
Hi @sarthakaggarwal97, just a gentle ping on this. Please let me know if my previous explanation regarding the freeClientAsync path addresses your concern, or if you have any other questions. Happy to make changes if needed!
|
Which versions do we need to backport this to? Is it 9.1, 9.0 and 8.1? |
This issue was introduced since the initial version, 9.1, 9.0 and 8.1 are needed to fix. |
…3448) Fixes server crash when RDMA benchmark clients disconnect (part of valkey-io#3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
…3448) Fixes server crash when RDMA benchmark clients disconnect (part of valkey-io#3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345). This PR focuses on the server-side crash fix. The benchmark client-side fix will be submitted in a separate PR. ## Server Crash on Client Disconnect When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference). ### Root Cause The same RDMA connection could be added to pending_list multiple times, leading to use-after-free: 1. When a client disconnects, rdmaHandleDisconnect() adds the connection to pending_list and sets conn->state = CONN_STATE_CLOSED 2. Before rdmaProcessPendingData() processes it, event-driven callbacks (e.g., connRdmaSetWriteHandler) could add the same connection again to pending_list 3. In rdmaProcessPendingData(), the first iteration processes and may free the connection, but the second iteration accesses the already freed connection → SIGSEGV at address 0x8 (NULL + offset) The problem was exacerbated because: - valkey-benchmark creates multiple concurrent connections - On Ctrl+C, these connections disconnect nearly simultaneously - Event callbacks can re-add connections before processing completes **Stack trace from crash:** handleReadResult+0x100 readQueryFromClient+0x63 rdmaProcessPendingData (0x1b1eb9) beforeSleep+0x82 ### Solution Four targeted fixes in `src/rdma.c`: 1. `rdmaHandleDisconnect (line 535)`: Check if pending_list_node is NULL before adding to pending_list to prevent duplicates ```C /* we can't close connection now, let's mark this connection as closed state */ if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } ``` 2. `connRdmaSetWriteHandler (line 956)`: Add same check before adding to pending_list to prevent duplicates during write handler updates ```C /* does this connection has pending write data? */ if (func) { if (rdma_conn->pending_list_node == NULL) { listAddNodeTail(pending_list, conn); rdma_conn->pending_list_node = listLast(pending_list); } } else if (rdma_conn->pending_list_node) { listDelNode(pending_list, rdma_conn->pending_list_node); rdma_conn->pending_list_node = NULL; } ``` 3. `rdmaProcessPendingData (line 1786)`: Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-added ```C listDelNode(pending_list, ln); // Use ln, not rdma_conn->pending_list_node rdma_conn->pending_list_node = NULL; ``` 4. `rdmaProcessPendingData (line 1782-1787)`: Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler execution ```C if (conn->state == CONN_STATE_ERROR || conn->state == CONN_STATE_CLOSED) { /* Invoke handlers first, then remove from list */ if (callHandler(conn, conn->read_handler)) { callHandler(conn, conn->write_handler); } listDelNode(pending_list, ln); rdma_conn->pending_list_node = NULL; ++processed; continue; } ``` The fix uses **pending_list_node** as both a list pointer and a "**already in list**" flag: - NULL = not in list, safe to add - non-NULL = already in list, skip adding This ensures each connection appears in pending_list at most once, preventing use-after-free crashes. Signed-off-by: Ada-Church-Closure <[email protected]>
Fixes server crash when RDMA benchmark clients disconnect (part of #3345).
This PR focuses on the server-side crash fix. The benchmark
client-side fix will be submitted in a separate PR.
Server Crash on Client Disconnect
When interrupting valkey-benchmark with Ctrl+C, the server would crash with a segmentation fault in handleReadResult, accessing address 0x8 (NULL pointer dereference).
Root Cause
The same RDMA connection could be added to pending_list multiple times, leading to use-after-free:
The problem was exacerbated because:
Stack trace from crash:
Solution
Four targeted fixes in
src/rdma.c:rdmaHandleDisconnect (line 535): Check if pending_list_node is NULL before adding to pending_list to prevent duplicatesconnRdmaSetWriteHandler (line 956): Add same check before adding to pending_list to prevent duplicates during write handler updatesrdmaProcessPendingData (line 1786): Use iterator node 'ln' instead of rdma_conn->pending_list_node when deleting, as the latter may have been overwritten if the connection was re-addedrdmaProcessPendingData (line 1782-1787): Reorder operations to call handlers before deleting from list, ensuring connection structure remains valid during handler executionThe fix uses pending_list_node as both a list pointer and a "already in list" flag:
This ensures each connection appears in pending_list at most once, preventing use-after-free crashes.
Testing
I use
./src/valkey-benchmark --rdma -h 10.0.0.1 -t get -n 100000 -c 1to test this: