crbench//

← all cases

Implement Write Barriers for HiredisConnection

Tier 2 medium ruby · local · real merged PR from redis-rb/redis-client #99

Ground-truth defects

d1 · correctness · high
hiredis_read_internal stores the address of its stack-local reader_state in connection->context->reader->privdata. Ruby's YJIT can move execution to a different native stack while hiredis retains that pointer, so reply_append later dereferences invalid stack memory. Parsing a Redis response can then read a corrupt task_index pointer and crash the process with SIGSEGV.
d2 · correctness · medium
The rewritten reply builder uses one array for two incompatible purposes: a depth-indexed slot array written by index, and a LIFO stash for RESP3 map keys pushed and popped. When a map key is itself an aggregate, the push lands at the same index the slot write uses and the key's children grow the array, so the following value pops the key's last child instead of the key. The map is returned with a wrong key and no error is raised.

The change under review

diff
diff --git a/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c b/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c
index 4e476e231a31ec66186b48fa8b0366c01acafd36..44795cef01a99bc9267fadd70e99740ee90cdef0 100644
--- a/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c
+++ b/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c
@@ -99,9 +99,6 @@ static const rb_data_type_t hiredis_ssl_context_data_type = {
         .dmark = NULL,
         .dfree = hiredis_ssl_context_free,
         .dsize = hiredis_ssl_context_memsize,
-#ifdef HAS_GC_COMPACT
-        .dcompact = NULL
-#endif
     },
     .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
 };
@@ -135,10 +132,18 @@ static VALUE hiredis_ssl_context_init(VALUE self, VALUE ca_file, VALUE ca_path,
     return Qnil;
 }
 
+typedef struct {
+    VALUE stack;
+    int *task_index;
+} hiredis_reader_state_t;
+
 static void *reply_append(const redisReadTask *task, VALUE value) {
-    if (task && task->parent) {
-        VALUE parent = (VALUE)task->parent->obj;
+    hiredis_reader_state_t *state = (hiredis_reader_state_t *)task->privdata;
+    int task_index = *state->task_index;
 
+    if (task->parent) {
+        RUBY_ASSERT(task_index > 0);
+        VALUE parent = rb_ary_entry(state->stack, task_index - 1);
         switch (task->parent->type) {
             case REDIS_REPLY_ARRAY:
             case REDIS_REPLY_SET:
@@ -147,11 +152,10 @@ static void *reply_append(const redisReadTask *task, VALUE value) {
                 break;
             case REDIS_REPLY_MAP:
                 if (task->idx % 2) {
-                    VALUE key = (VALUE)task->parent->privdata;
-                    task->parent->privdata = NULL;
+                    VALUE key = rb_ary_pop(state->stack);
                     rb_hash_aset(parent, key, value);
                 } else {
-                    task->parent->privdata = (void*)value;
+                    rb_ary_push(state->stack, value);
                 }
                 break;
             default:
@@ -159,6 +163,7 @@ static void *reply_append(const redisReadTask *task, VALUE value) {
                 break;
         }
     }
+    rb_ary_store(state->stack, task_index, value);
     return (void*)value;
 }
 
@@ -314,37 +319,6 @@ typedef struct {
     struct timeval write_timeout;
 } hiredis_connection_t;
 
-
-#ifdef HAS_GC_COMPACT
-static void hiredis_connection_compact(void *ptr) {
-    hiredis_connection_t *connection = ptr;
-    if (connection->context) {
-        redisReader *reader = connection->context->reader;
-        // reader->ridx == -1 when there is no active task
-        for (int index = 0; index <= reader->ridx; index++) {
-            redisReadTask *task = reader->task[index];
-            if (task->obj) { task->obj = (void *)rb_gc_location((VALUE)task->obj); }
-            if (task->privdata) { task->privdata = (void *)rb_gc_location((VALUE)task->privdata); }
-        }
-    }
-}
-#else
-#define rb_gc_mark_movable rb_gc_mark
-#endif
-
-static void hiredis_connection_mark(void *ptr) {
-    hiredis_connection_t *connection = ptr;
-    if (connection->context) {
-        redisReader *reader = connection->context->reader;
-        // reader->ridx == -1 when there is no active task
-        for (int index = 0; index <= reader->ridx; index++) {
-            redisReadTask *task = reader->task[index];
-            if (task->obj) { rb_gc_mark_movable((VALUE)task->obj); }
-            if (task->privdata) { rb_gc_mark_movable((VALUE)task->privdata); }
-        }
-    }
-}
-
 static void hiredis_connection_free(void *ptr) {
     hiredis_connection_t *connection = ptr;
     if (connection) {
@@ -379,14 +353,11 @@ static size_t hiredis_connection_memsize(const void *ptr) {
 static const rb_data_type_t hiredis_connection_data_type = {
     .wrap_struct_name = "redis-client:hiredis_connection",
     .function = {
-        .dmark = hiredis_connection_mark,
+        .dmark = NULL,
         .dfree = hiredis_connection_free,
         .dsize = hiredis_connection_memsize,
-#ifdef HAS_GC_COMPACT
-        .dcompact = hiredis_connection_compact
-#endif
     },
-    .flags = RUBY_TYPED_FREE_IMMEDIATELY
+    .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
 };
 
 static VALUE hiredis_alloc(VALUE klass) {
@@ -717,6 +688,16 @@ static int hiredis_read_internal(hiredis_connection_t *connection, VALUE *reply)
     void *redis_reply = NULL;
     int wdone = 0;
 
+    // This struct being on the stack, the GC won't move nor collect that `stack` RArray.
+    // We use that to avoid having to have a `mark` function with write barriers.
+    // Not that it would be too hard, but if we mark the response objects, we'll likely end up
+    // promoting them to the old generation which isn't desirable.
+    hiredis_reader_state_t reader_state = {
+        .stack = rb_ary_new(),
+        .task_index = &connection->context->reader->ridx,
+    };
+    connection->context->reader->privdata = &reader_state;
+
     /* Try to read pending replies */
     if (redisGetReplyFromReader(connection->context, &redis_reply) == REDIS_ERR) {
         return HIREDIS_FATAL_CONNECTION_ERROR; // Protocol error

Leaderboard for this case

medium · 5 of 9 bots fully caught it (56% solve rate) · 72% avg coverage.

#BotResultCaughtCoverageFindingsFalse alarms
1 cursor solved 2/2 100.0% 2 0
2 Drape (Deep Review) solved 2/2 100.0% 4 0
3 greptile solved 2/2 100.0% 2 0
4 Mjolnir (Opus 5.5) solved 2/2 100.0% 4 0
5 Mjolnir (Sonnet 5.5) solved 2/2 100.0% 3 0
6 Drape (Review) partial 1/2 66.7% 1 0
7 qodo partial 1/2 66.7% 1 0
8 Claude Code (Opus 5.5) partial 1/2 33.3% 2 0
9 Cubic miss 0/2 0.0% 1 0

Bot results

Claude Code (Opus 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✗ —
d2 medium ✓ off by one
valid · impact high
On HIREDIS_CLIENT_TIMEOUT the reader keeps its partial tasks (ridx >= 0), but `reader_state.stack` is a fresh `rb_ary_new()` local to each call. Hiredis_read raises the timeout without disconnecting, which is the PubSub case. Because the mark function was removed, nothing keeps the partial objects alive, so the GC can free them. On the next read the parent lookup on the new empty stack returns nil, and `rb_ary_store` or `rb_hash_aset` is then called on nil. Before the change, the parent came from `task->parent->obj` and the mark function kept it alive, so this change introduces the bug. [grounded: diff] [consensus 3/3: valid=3]
valid · matches d2 · impact medium
raw comments (2)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:694
The commit removes the mark and compact functions and keeps partially built replies alive only through `reader_state.stack`, which is a new `rb_ary_new()` on the C stack of each `hiredis_read_internal` call. If a read times out partway through an aggregate reply (HIREDIS_CLIENT_TIMEOUT while waiting for the socket), the redisReader still has in-progress tasks whose `task->obj` points at Ruby objects. Once the function returns, nothing references those objects, so the GC can free them. This is a normal case for `PubSub#next_event(timeout)`, which treats a read timeout as 'no event' and keeps the same connection. On the next read a fresh, empty stack is created while `reader->ridx > 0`. `reply_append` then gets `parent = rb_ary_entry(stack, task_index - 1)`, which is `Qnil`, and calls `rb_ary_push`/`rb_hash_aset` on nil. Hiredis may also return a top-level reply object that was already freed. The result is a segfault or memory corruption. The old mark function kept these objects alive, and the old code read the parent from `task->parent->obj`, so this worked before. The stack needs to persist across calls, for example stored in the connection and marked, or be rebuilt.
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:155
Pending map keys are now kept with push/pop at the end of the shared depth-indexed stack, and `rb_ary_store(state->stack, task_index, value)` writes to the same array. If a map key is itself an aggregate (an array or map key, which RESP3 allows), its children store at `task_index + 1`. That can overwrite the pushed key, or put the child at the end of the array, so the later `rb_ary_pop` returns the wrong element. The reply then ends up with a wrong key/value pairing. Before this change the key was kept separately in `task->parent->privdata`.
Cubic · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✗ —
d2 medium ✗ —
valid · impact high
Each read builds a new stack-allocated reader_state with an empty Ruby `stack`. Hiredis copies `r->privdata` into each task only when it creates the task (vendor/read.c:514, 708). A timeout raises ReadTimeoutError without disconnecting, and the caller may read again (e.g. PubSub#next_event). On that next read, a resumed reply mixes two kinds of tasks. Tasks created before the timeout hold a pointer to a stack frame that no longer exists. Tasks created afterwards use a new stack that lacks the parent objects. So `rb_ary_entry(state->stack, task_index - 1)` returns nil, or the code reads freed stack memory. Before this change, `task->obj` kept the parent objects (marked by the removed dmark), so a resumed reply was built correctly. [grounded: diff] [consensus 5/5: valid=5]
raw comments (2)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:699
<!-- cubic:v=28548205-a8ce-40a6-b0e0-ff70ba3685ba --> <!-- cubic:review-run=58383b79-e3df-5d6b-a940-372cdbbab518 --> <!-- metadata:{"confidence":10} --> P1: When a nested reply is split across a read timeout, the next `read` resumes active Hiredis tasks with the previous per-call state, while this assignment points only new tasks at the new state. The new stack has none of the already-created parent objects, so `reply_append` cannot assemble the reply and may raise or crash; persist the reader state and its Ruby stack across partial reads, clearing it only after `ridx` returns to `-1`. <details> <summary>Prompt for AI agents</summary> ```text Check if this issue is valid — if so, understand the root cause and fix it. At hiredis-client/ext/redis_client/hiredis/hiredis_connection.c, line 699: <comment>When a nested reply is split across a read timeout, the next `read` resumes active Hiredis tasks with the previous per-call state, while this assignment points only new tasks at the new state. The new stack has none of the already-created parent objects, so `reply_append` cannot assemble the reply and may raise or crash; persist the reader state and its Ruby stack across partial reads, clearing it only after `ridx` returns to `-1`.</comment> <file context> @@ -717,6 +688,16 @@ static int hiredis_read_internal(hiredis_connection_t *connection, VALUE *reply) + .stack = rb_ary_new(), + .task_index = &connection->context->reader->ridx, + }; + connection->context->reader->privdata = &reader_state; + /* Try to read pending replies */ </file context> ``` </details>
PR-level
<!-- cubic:review-summary:start --> **1 issue found** across 1 file <!-- cubic:review-summary:end --> <details> <summary>Prompt for AI agents (unresolved issues)</summary> ```text Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately. <file name="hiredis-client/ext/redis_client/hiredis/hiredis_connection.c"> <violation number="1" location="hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:699"> P1: When a nested reply is split across a read timeout, the next `read` resumes active Hiredis tasks with the previous per-call state, while this assignment points only new tasks at the new state. The new stack has none of the already-created parent objects, so `reply_append` cannot assemble the reply and may raise or crash; persist the reader state and its Ruby stack across partial reads, clearing it only after `ridx` returns to `-1`.</violation> </file> ``` </details> <sub>Reply with feedback, questions, or to request a fix.<br /><br />[Re-trigger cubic](https://www.cubic.dev/action/re-review/pr/drape-io/crbench-cubic-2026-09/70/ai_pr_review_github_delivery_c4396234882d4b7ec1ed67c26ae88bf50a9a5f78de38463d43c504e027541c1d?returnTo=https%3A%2F%2Fgithub.com%2Fdrape-io%2Fcrbench-cubic-2026-09%2Fpull%2F70)</sub> <!-- cubic:review-post:ai_pr_review_github_delivery_c4396234882d4b7ec1ed67c26ae88bf50a9a5f78de38463d43c504e027541c1d:7feafd0f7eabba31509e6cc5a23202e2536ecad8:58383b79-e3df-5d6b-a940-372cdbbab518 -->
cursor · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
d2 medium ✓ exact
valid · matches d1 · impact high
valid · matches d2 · impact medium
raw comments (3)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:699
### Reader state dies across read calls **High Severity** <!-- DESCRIPTION START --> Read timeouts can leave hiredis mid-parse, but `reader_state` is discarded when `_read` returns. The next `_read` then follows a dangling `task->privdata` pointer and an empty stack, which can crash or corrupt the reply. `PubSub#next_event` keeps the connection after timeout. <!-- DESCRIPTION END --> <!-- BUGBOT_BUG_ID: c18662a7-181d-4398-a703-26bb188bc919 --> <!-- LOCATIONS START hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L690-L699 hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L139-L141 hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L352-L360 LOCATIONS END --> <details> <summary>Additional Locations (2)</summary> - [`hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L139-L141`](https://github.com/drape-io/crbench-cursor-2026-09/blob/401b2727a2fe456afc58685e37ff187999c156a8/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L139-L141) - [`hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L352-L360`](https://github.com/drape-io/crbench-cursor-2026-09/blob/401b2727a2fe456afc58685e37ff187999c156a8/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L352-L360) </details> <div><a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9DVVJTT1IiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OmFiZDBjZDA0LWZjOTUtNDMwYy05NjI0LWQ1OTUwZWNlNDMyMyIsImVuY3J5cHRpb25LZXkiOiJZZWhIZTdNY2xuU2t4b2pPNjRUZjV4MUhweTBPbmFpY01tSTdST3Z0Q3ZBIiwiYnJhbmNoIjoidDItcmVkaXMtY2xpZW50LTk5IiwicmVwb093bmVyIjoiZHJhcGUtaW8iLCJyZXBvTmFtZSI6ImNyYmVuY2gtY3Vyc29yLTIwMjYtMDkiLCJwcm92aWRlciI6ImdpdGh1YiJ9fQ" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a>&nbsp;<a href="https://cursor.com/agents?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9XRUIiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OmFiZDBjZDA0LWZjOTUtNDMwYy05NjI0LWQ1OTUwZWNlNDMyMyIsImVuY3J5cHRpb25LZXkiOiJZZWhIZTdNY2xuU2t4b2pPNjRUZjV4MUhweTBPbmFpY01tSTdST3Z0Q3ZBIiwiYnJhbmNoIjoidDItcmVkaXMtY2xpZW50LTk5IiwicmVwb093bmVyIjoiZHJhcGUtaW8iLCJyZXBvTmFtZSI6ImNyYmVuY2gtY3Vyc29yLTIwMjYtMDkiLCJwck51bWJlciI6NzIsImNvbW1pdFNoYSI6IjQwMWIyNzI3YTJmZTQ1NmFmYzU4Njg1ZTM3ZmYxODc5OTljMTU2YTgiLCJwcm92aWRlciI6ImdpdGh1YiJ9fQ" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-web-light.png"><img alt="Fix in Web" width="99" height="28" src="https://cursor.com/assets/images/fix-in-web-dark.png"></picture></a></div> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 401b2727a2fe456afc58685e37ff187999c156a8. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:158
### Nested map keys corrupt parse stack **Medium Severity** <!-- DESCRIPTION START --> Map keys are pushed onto the same `stack` used for nested containers. When a map key is a non-empty array or map, child values remain on `stack` after the key is built, so the later `rb_ary_pop` can pick up a nested child instead of the key and insert the wrong entry in the hash. <!-- DESCRIPTION END --> <!-- BUGBOT_BUG_ID: 93eeed18-e6e9-45d2-a96d-d33052ba5a14 --> <!-- LOCATIONS START hiredis-client/ext/redis_client/hiredis/hiredis_connection.c#L152-L159 LOCATIONS END --> <div><a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9DVVJTT1IiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjJiMzI5NDZkLTdjY2YtNGM0MC1hMWI3LTFmNzNmZWJjMWU5NSIsImVuY3J5cHRpb25LZXkiOiJ0OC1NcGRiaEQ0TlpJV2x4a3dXNDlDRjV3YTlRWUJUOTJvTDlIY0RiSlBFIiwiYnJhbmNoIjoidDItcmVkaXMtY2xpZW50LTk5IiwicmVwb093bmVyIjoiZHJhcGUtaW8iLCJyZXBvTmFtZSI6ImNyYmVuY2gtY3Vyc29yLTIwMjYtMDkiLCJwcm92aWRlciI6ImdpdGh1YiJ9fQ" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a>&nbsp;<a href="https://cursor.com/agents?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9XRUIiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjJiMzI5NDZkLTdjY2YtNGM0MC1hMWI3LTFmNzNmZWJjMWU5NSIsImVuY3J5cHRpb25LZXkiOiJ0OC1NcGRiaEQ0TlpJV2x4a3dXNDlDRjV3YTlRWUJUOTJvTDlIY0RiSlBFIiwiYnJhbmNoIjoidDItcmVkaXMtY2xpZW50LTk5IiwicmVwb093bmVyIjoiZHJhcGUtaW8iLCJyZXBvTmFtZSI6ImNyYmVuY2gtY3Vyc29yLTIwMjYtMDkiLCJwck51bWJlciI6NzIsImNvbW1pdFNoYSI6IjQwMWIyNzI3YTJmZTQ1NmFmYzU4Njg1ZTM3ZmYxODc5OTljMTU2YTgiLCJwcm92aWRlciI6ImdpdGh1YiJ9fQ" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-web-light.png"><img alt="Fix in Web" width="99" height="28" src="https://cursor.com/assets/images/fix-in-web-dark.png"></picture></a></div> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 401b2727a2fe456afc58685e37ff187999c156a8. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
PR-level
<!-- BUGBOT_REVIEW --> Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues. <!-- BUGBOT_FIX_ALL --> <a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9BTExfSU5fQ1VSU09SIiwiZGF0YSI6eyJyZWRpc0tleSI6ImJ1Z2JvdC1tdWx0aTo3ZTZlMjNmMi1kZDJiLTRmZTEtYmMxZi02N2E5YTdjMGI3NzgiLCJlbmNyeXB0aW9uS2V5Ijoiay02WWptN190VGJwa0ZjSGJILTZ5c2VIek8zNmNtUFlXbjlIbmF6UDQwWSIsImJyYW5jaCI6InQyLXJlZGlzLWNsaWVudC05OSIsInJlcG9Pd25lciI6ImRyYXBlLWlvIiwicmVwb05hbWUiOiJjcmJlbmNoLWN1cnNvci0yMDI2LTA5IiwicHJvdmlkZXIiOiJnaXRodWIifX0" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix All in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a> <!-- /BUGBOT_FIX_ALL --> <!-- BUGBOT_AUTOFIX_REVIEW_FOOTNOTE_BEGIN --> <sup>❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).</sup> <!-- BUGBOT_AUTOFIX_REVIEW_FOOTNOTE_END --> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 401b2727a2fe456afc58685e37ff187999c156a8. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
Drape (Deep Review) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ off by one
d2 medium ✓ exact
valid · impact high
The mark function that kept task->obj alive between calls was removed. The only remaining root is a stack-local RArray that dies when hiredis_read_internal returns on HIREDIS_CLIENT_TIMEOUT. A partial reply kept by the incremental reader after a timeout is therefore unmarked and can be collected. [grounded: diff] [consensus 3/3: valid=3]
valid · impact high
After a timeout the reader keeps ridx>0 with a partial aggregate. The next call creates a new empty stack, so rb_ary_entry(state->stack, task_index - 1) returns nil and the parent container is lost, which corrupts the reply or crashes. [grounded: diff] [consensus 3/3: valid=3]
valid · matches d1 · impact high
valid · matches d2 · impact medium
raw comments (4)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:356
**Partially read reply can be garbage-collected** With `hiredis_connection_mark` removed, nothing marks a half-built reply once a read times out and returns. `r->reply` and `task->obj` still point to it, so the next read may return or append to freed objects. Keep the in-progress stack reachable from the connection object, marked and with write barriers. ``` hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:356 .dmark = NULL, hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:335 (base) static void hiredis_connection_mark(void *ptr) { hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:336 (base) hiredis_connection_t *connection = ptr; hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:337 (base) if (connection->context) { hiredis-client/ext/redis_client/hiredis/vendor/read.c:521 if (root) r->reply = obj; hiredis-client/ext/redis_client/hiredis/vendor/read.c:732 *reply = r->reply; ```
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:146
**Resumed reply loses its parent containers** Each `hiredis_read_internal` call starts a fresh empty `stack`, but a reply resumed after a timeout needs the parent arrays/hashes from the earlier call. `rb_ary_entry` then returns nil (or a wrong object) as the parent. Persist the stack across reads (on the connection) until the reply completes. ``` hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:146 VALUE parent = rb_ary_entry(state->stack, task_index - 1); hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:695 hiredis_reader_state_t reader_state = { hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:696 .stack = rb_ary_new(), hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:697 .task_index = &connection->context->reader->ridx, hiredis-client/ext/redis_client/hiredis/vendor/read.c:256 if (cur->idx == prv->elements-1) { hiredis-client/ext/redis_client/hiredis/vendor/read.c:257 r->ridx--; hiredis-client/ext/redis_client/hiredis/vendor/read.c:258 } else { hiredis-client/ext/redis_client/hiredis/vendor/read.c:259 /* Reset the type because the next item can be anything */ hiredis-client/ext/redis_client/hiredis/vendor/read.c:260 assert(cur->idx < prv->elements); hiredis-client/ext/redis_client/hiredis/vendor/read.c:261 cur->type = -1; ```
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:699
**Resumed partial reply reads a dead stack frame** Tasks copy `reader->privdata` once at setup. After a mid-reply timeout, resumed tasks still point at the previous call's stack `reader_state`, which `reply_append` dereferences. Keep the reader state on the connection (heap, marked) instead of the C stack. ``` hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:695 hiredis_reader_state_t reader_state = { hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:696 .stack = rb_ary_new(), hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:697 .task_index = &connection->context->reader->ridx, hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:698 }; hiredis-client/ext/redis_client/hiredis/vendor/read.c:505 if (elements > 0) { hiredis-client/ext/redis_client/hiredis/vendor/read.c:506 cur->elements = elements; hiredis-client/ext/redis_client/hiredis/vendor/read.c:507 cur->obj = obj; hiredis-client/ext/redis_client/hiredis/vendor/read.c:508 r->ridx++; hiredis-client/ext/redis_client/hiredis/vendor/read.c:509 r->task[r->ridx]->type = -1; hiredis-client/ext/redis_client/hiredis/vendor/read.c:510 r->task[r->ridx]->elements = -1; ```
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:155
**Wrong hash key for aggregate map keys** If a map key is itself an array or map, its children are stored past the pushed key, so `rb_ary_pop` returns the key's last child instead of the key. Keep pending map keys in a separate per-depth slot instead of push/pop on the shared stack. ``` hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:154 if (task->idx % 2) { hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:155 VALUE key = rb_ary_pop(state->stack); hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:156 rb_hash_aset(parent, key, value); hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:157 } else { hiredis-client/ext/redis_client/hiredis/vendor/read.c:505 if (elements > 0) { hiredis-client/ext/redis_client/hiredis/vendor/read.c:506 cur->elements = elements; hiredis-client/ext/redis_client/hiredis/vendor/read.c:507 cur->obj = obj; hiredis-client/ext/redis_client/hiredis/vendor/read.c:508 r->ridx++; hiredis-client/ext/redis_client/hiredis/vendor/read.c:509 r->task[r->ridx]->type = -1; hiredis-client/ext/redis_client/hiredis/vendor/read.c:510 r->task[r->ridx]->elements = -1; ```
Drape (Review) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
d2 medium ✗ —
valid · matches d1 · impact high
raw comments (1)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:695
**Resumed partial reply uses dead stack state** The reply stack and `reader_state` exist for one `hiredis_read_internal` call, but hiredis keeps half-parsed tasks between calls. After a read timeout mid-reply, the retry sees an empty stack and `privdata` pointing at a dead stack frame. Keep the stack on the connection and mark it. ``` hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:695 hiredis_reader_state_t reader_state = { hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:696 .stack = rb_ary_new(), hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:697 .task_index = &connection->context->reader->ridx, hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:698 }; hiredis-client/ext/redis_client/hiredis/vendor/read.c:514 r->task[r->ridx]->privdata = r->privdata; hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:356 .dmark = NULL, ```
greptile · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
d2 medium ✓ off by one
valid · matches d2 · impact medium
valid · matches d1 · impact high
raw comments (3)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:155
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> **Aggregate map keys break** When a RESP3 map key is an aggregate such as an array, parsing its children leaves deeper entries at the end of `state->stack`. The map value then pops the key's last child instead of the aggregate stored at the parent task depth, so the resulting Ruby Hash contains the wrong key. The pending key must be retrieved by its task depth, or descendant entries must be removed before it is consumed.
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:699
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> **Reader state becomes dangling** `reader->privdata` points to the function-local `reader_state`, but an incomplete aggregate reply can remain in hiredis after a read timeout. Because the timeout deliberately leaves the connection reusable, active tasks retain this pointer after the stack frame is destroyed. Continuing the reply on the next read dereferences dangling state in `reply_append`, which can crash or corrupt the reply. The reader state must remain alive while an incremental reply exists, or the partial reader state must be discarded before returning.
PR-level
<h3>Greptile Summary</h3> This PR refactors hiredis reply construction so temporary Ruby objects are held in a per-read array rather than in hiredis task object fields, and removes the connection’s custom GC mark/compaction callbacks. - Adds `hiredis_reader_state_t` and depth-indexed aggregate tracking. - Changes RESP3 map-key retention to use the shared Ruby array. - Marks the wrapped connection as write-barrier protected. - The new state management mishandles aggregate map keys and does not survive partial replies across reusable read timeouts. <h3>Confidence Score: 3/5</h3> This PR is not safe to merge until aggregate RESP3 map keys and reader-state lifetime across reusable timeouts are corrected. The shared array pops descendant values for aggregate map keys, and active hiredis tasks can retain a pointer to expired stack state when an incomplete reply times out. **Files Needing Attention:** hiredis-client/ext/redis_client/hiredis/hiredis_connection.c <details><summary><h3>Important Files Changed</h3></summary> | Filename | Overview | |----------|----------| | hiredis-client/ext/redis_client/hiredis/hiredis_connection.c | Refactors reply-object GC retention and aggregate assembly, but introduces incorrect aggregate map-key selection and dangling parser state across partial reads. | </details> <details open><summary><h3>Sequence Diagram</h3></summary> ```mermaid sequenceDiagram participant Caller participant Read as hiredis_read_internal participant Reader as redisReader Caller->>Read: read() Read->>Read: create stack-local reader_state Read->>Reader: "privdata = &reader_state" Reader->>Reader: parse part of aggregate reply Reader-->>Read: incomplete reply Read-->>Caller: raise read timeout Note over Read: reader_state lifetime ends Caller->>Read: read() again on same connection Read->>Reader: continue retained active task Reader->>Read: reply_append(task) Note over Reader,Read: task->privdata still references expired state ``` </details> <!-- greptile_other_comments_section --> <sub>Reviews (1): Last reviewed commit: ["Update hiredis-client/ext/redis\_client/h..."](https://github.com/drape-io/crbench-greptile-2026-09/commit/47d720e299619e5d08876dae07ed18f26262f682) | [Re-trigger Greptile](https://app.greptile.com/api/retrigger?id=64284784)</sub>
Mjolnir (Opus 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
d2 medium ✓ off by one
valid · matches d1 · impact high
valid · matches d2 · impact medium
valid · impact high
Each read creates a new stack, but a client timeout leaves hiredis's partial task state in place without disconnecting. The mark function is removed, so the partial objects can be collected while hiredis still points at them. The resumed read also looks up its parent in an empty stack, gets nil, and stores into it. [grounded: diff] [consensus 3/3: valid=3]
valid · impact low
Tracing {[1] => "v"}: the stack ends up as [map, arr, 1] because the key's child is stored at slot 2, so the pop returns 1 instead of the array key. The old privdata-based code did not have this problem. [grounded: diff] [consensus 3/3: valid=3]
raw comments (4)
PR-level
[P1] Replies that span a read timeout now crash or return freed objects hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:691-699 The new code builds the reader state, including the `stack` RArray, on the C stack for each `hiredis_read_internal` call and stores its address in `reader-›privdata`. But hiredis keeps partially parsed tasks between calls. If an aggregate reply arrives in chunks and the read times out partway through, the function returns `HIREDIS_CLIENT_TIMEOUT`. `hiredis_read` then raises `ReadTimeoutError` and deliberately leaves the connection open (for example, for `PubSub#next_event`). That leaves three problems. First, existing tasks keep a `privdata` pointer to a stack frame that no longer exists. Second, the partly built Ruby arrays and hashes were only reachable through the old `stack` array, and the mark function was removed, so GC can collect them while `task-›obj` and `r-›reply` still point to them. Third, on the next read, new child tasks get a fresh, empty `stack`, so `rb_ary_entry(state-›stack, task_index - 1)` returns `Qnil`, and `rb_ary_store`/`rb_hash_aset` is called on `nil`. The old code handled this through the `obj`/`privdata` fields on each task plus the mark/compact functions. The result is a crash or memory corruption whenever a pubsub (or other) read times out in the middle of a multi-chunk reply and is then retried.
PR-level
[P3] RESP3 map keys that are aggregates now get the wrong value as the key hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:153-166 A pending map key is now kept by `rb_ary_push` onto the shared `stack`, which is also indexed by task depth through `rb_ary_store(state-›stack, task_index, value)`. If the key is itself an aggregate (an array or map key), its children are stored at `task_index + 1`, which overwrites or follows the pushed key. When the map value arrives, `rb_ary_pop` returns the key's last child instead of the key. For example, the map `{[a, b] =› v}` ends up as `{b =› v}`. The previous implementation kept the key in `task-›parent-›privdata` and handled this case correctly.
PR-level
[P1] Fresh reader stack per call breaks replies that span a client timeout hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:695-699 `reader_state.stack` is created again with `rb_ary_new()` on every `hiredis_read_internal` call, but hiredis keeps partially parsed task state (`ridx`, `task[]-›obj`, `r-›reply`) between calls. On `HIREDIS_CLIENT_TIMEOUT` the connection is deliberately left open so the caller can decide what to do (see the comment in `hiredis_read`, e.g. `PubSub#next_event` with a timeout). If only part of an aggregate reply has arrived when that timeout fires, two things go wrong. First, the old stack array is now unreachable, and with the mark function removed nothing else keeps the half-built reply objects alive, so GC can free them while hiredis still holds raw pointers to them. That leads to a use-after-free when the root is finally returned. Second, the next read resumes with `ridx › 0` against an empty stack, so `rb_ary_entry(state-›stack, task_index - 1)` returns `nil` and `rb_ary_store(nil, ...)` or `rb_hash_aset(nil, ...)` raises from inside the hiredis parser callback. The removed `hiredis_connection_mark` covered exactly this case.
PR-level
[P2] Map keys that are aggregates get the wrong value from the stack pop hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:154-159 A map key is saved with `rb_ary_push` and read back with `rb_ary_pop`, which assumes nothing touches the end of `state-›stack` between the key and its value. When the key is itself an aggregate (a RESP3 map whose key is an array, set or map), the key's children are written with `rb_ary_store(state-›stack, task_index, value)` at depth `task_index + 1`. That slot can be the one the key was pushed into, or beyond it. Take `{[1] =› "v"}` as an example: the stack becomes `[map, arr, 1]`, so the pop returns `1` and the result is `{1 =› "v"}` instead of `{[1] =› "v"}`. Whether this happens depends on how long the stack was left by earlier siblings. The old code kept the key in `task-›parent-›privdata`, which did not have this problem.
Mjolnir (Sonnet 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
d2 medium ✓ off by one
valid · impact high
The parse stack is created fresh in every hiredis_read_internal call, but HIREDIS_CLIENT_TIMEOUT returns without closing the connection or resetting the reader, so a partially parsed aggregate keeps its task state (ridx). On the next read, reply_append calls rb_ary_entry on the new empty stack and gets nil as the parent, then calls rb_ary_store or rb_hash_aset on nil. The partial objects are also no longer GC-marked because this change removed the mark function. Before the change, task->obj/privdata were marked, so this is a regression. [grounded: diff] [consensus 3/3: valid=3]
valid · matches d1 · impact high
valid · matches d2 · impact medium
raw comments (3)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:695
[P1] Parse stack is recreated on every read call, so a timed-out partial reply is corrupted on resume `hiredis_read_internal` now allocates a fresh `rb_ary_new()` stack on every call, but the hiredis reader keeps its partial task state (`ridx`, task counts) across calls. `HIREDIS_CLIENT_TIMEOUT` is explicitly recoverable: the caller may keep the connection open, e.g. `PubSub#next_event` with a timeout. Suppose a large multi-element reply (array or map) is only partly received when the read times out, and the caller later calls read again. - The reader resumes mid-aggregate, but the new stack is empty, so `rb_ary_entry(state-›stack, task_index - 1)` returns `nil`. `reply_append` then calls `rb_ary_store`/`rb_hash_aset` on `nil`, which is undefined behavior and likely a crash or `TypeError`. - The partially built parent objects were only referenced by the previous stack. They are no longer marked, because the connection's mark function was removed, so the GC can free them. The reader's root `obj` can then be a dangling VALUE, which is a use-after-free. Before this change the in-progress objects lived in the reader tasks and were marked by `hiredis_connection_mark`. The stack therefore needs to survive across calls, for example by keeping it in the connection and marking it. Alternatively, a mid-parse timeout must be made fatal so the connection is closed and the reader reset.
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:691
[P1] Partial-reply state is lost across timed-out reads, which can crash or corrupt replies `reader_state` and its `stack` array are now created fresh on every `hiredis_read_internal` call. The hiredis reader's partially built task stack (`ridx`, `task[]`) persists in the connection. `HIREDIS_CLIENT_TIMEOUT` returns without closing the connection, and `hiredis_read` explicitly lets the caller keep it, for example `PubSub#next_event` with a timeout. Suppose a timeout fires mid-aggregate, such as an array or map whose elements have only partly arrived. The next read then resumes with `ridx › 0` but an empty `stack`. `rb_ary_entry(state-›stack, task_index - 1)` returns nil, so `rb_ary_store(nil, ...)` or `rb_hash_aset(nil, ...)` raises or corrupts the reply, and the already-parsed elements are lost. Resumed tasks may also still hold `task-›privdata` pointing at the previous call's dead stack frame, because hiredis copies `reader-›privdata` into a task only when the task is initialised. Dereferencing that in `reply_append` is a use-after-return. `reader-›privdata` is also left dangling after every return. The old design kept this state in the reader and marked it, so this is a regression for partial reads.
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:155
[P3] Aggregate map keys leave `rb_ary_pop` returning the wrong stack entry The map key is pushed at the end of `stack`, and the value path then uses `rb_ary_pop` to retrieve it. This assumes the key is the last element. Stale child entries from nested replies are never popped, and the extra `rb_ary_store(state-›stack, task_index, value)` adds more. If a map key is itself an aggregate, for example an array key with children, the children sit above the key in `stack`. The pop then returns a child element instead of the key, so the hash gets the wrong key. For string or scalar keys it only works because the key is pushed last. Keeping the key at a fixed per-depth slot, or truncating `stack` to `task_index` before pushing and popping, would avoid depending on that.
qodo · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
d2 medium ✗ —
valid · matches d1 · impact high
raw comments (3)
hiredis-client/ext/redis_client/hiredis/hiredis_connection.c:699
<img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required"> 1\. Timed-out replies break later reads <code>🐞 Bug</code> <code>☼ Reliability</code> <pre> <b><i>hiredis_read_internal</i></b> installs a stack-local <b><i>reader_state</i></b> on every call, while hiredis persists active tasks whose <b><i>privdata</i></b> still points to the prior invocation and the connection now has no marker for their Ruby objects. When an aggregate response is partially parsed before a recoverable read timeout, the next read resumes through that dangling state or a fresh empty stack, so parent and map-key lookup can fail and a GC between calls can reclaim the in-progress reply. </pre> <details> <summary><strong>Agent Prompt</strong></summary> ``` ## Issue description The hiredis parser preserves active tasks across recoverable read timeouts, but `hiredis_read_internal` gives those tasks a pointer to invocation-local state. Partial Ruby replies also become invisible to GC because the connection no longer marks them. ## Fix Focus Areas - hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[135-167] - hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[353-360] - hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[687-699] ## Recommended Fix Store the reader state in stable connection-owned memory and retain the same Ruby stack while `reader->ridx` represents a partial reply. Restore GC marking and compaction handling for every Ruby value retained through the reader, clear or replace the state only after the reply completes, and ensure all existing and newly created tasks reference the stable state. ``` <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code> </details>
PR-level
<h3>PR Summary by Qodo</h3> Avoid promoting hiredis replies during GC marking <code>✨ Enhancement</code> <code>🕐 20-40 Minutes</code> <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <details> <summary>AI Description</summary> <dl> <dd> <br/> ><pre> >• Roots in-progress RESP reply objects in a Ruby array during hiredis reads. >• Builds nested collections from indexed reader state instead of hiredis task pointers. >• Removes custom mark and compaction callbacks to avoid promoting short-lived replies. ></pre> </dd> </dl> </details> <details> <summary>Diagram</summary> <dl> <dd> <br/> ```mermaid graph TD A["Redis Socket"] -->|RESP bytes| B["hiredis Reader"] -->|reply callbacks| C["Reader State"] -->|assembled values| D["Ruby Reply"] E["Ruby GC"] -.->|stack roots| C ``` </dd> </dl> </details> <details> <summary>High-Level Assessment</summary> <dl> <dd> <br/> >The following are alternative approaches to this PR: <details> <summary><b>1. Retain mark and compact callbacks</b></summary> <dl> <dd> - ➕ Preserves intermediate reply objects across interrupted reader operations - ➕ Uses Ruby&#x27;s established typed-data GC integration - ➖ May promote short-lived reply objects into the old generation - ➖ Requires maintaining compaction relocation logic for hiredis task pointers </dd> </dl> </details> <details> <summary><b>2. Persist reader state on the connection</b></summary> <dl> <dd> - ➕ Keeps partial replies rooted across read timeouts and subsequent calls - ➕ Centralizes parser lifetime state instead of storing stack addresses in hiredis - ➖ Requires explicit write barriers or a marked Ruby container - ➖ Adds connection lifecycle and cleanup complexity </dd> </dl> </details> >**Recommendation:** The stack-rooted approach reduces GC overhead and removes fragile relocation code, making it preferable when reply parsing completes within one `hiredis_read_internal` invocation. Because hiredis can retain partial aggregate tasks, reviewers should verify timeout paths cannot outlive the stack-local state; otherwise, persistent per-connection reader state is the safer design. </dd> </dl> </details> <details> <summary> Files changed (1) <code> +25 / -44 </code> </summary> <dl> <dd> <br/> <details> <summary>Other (1) <code> +25 / -44 </code></summary> <dl> <dd> <details> <summary>hiredis_connection.c<code>Root hiredis reply assembly in stack-local Ruby state</code> <code>+25/-44</code></summary> <br/> >Root hiredis reply assembly in stack-local Ruby state > ><pre> >• Introduces reader state containing a Ruby array and active task index, then uses it to assemble nested arrays, sets, pushes, and maps. Removes connection mark and compaction callbacks, marks typed data as write-barrier protected, and initializes the reader state for each internal read. ></pre> > ><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/66/files#diff-acac80492782f9a778f40ebe6e63de4fdeeb330e707e79e14d78e153f259774c'>hiredis-client/ext/redis_client/hiredis/hiredis_connection.c</a> </details> </dd> </dl> </details> </dd> </dl> </details>
PR-level
<h3>Code Review by Qodo</h3> <code>🐞 Bugs (1)</code> <code>📘 Rule violations (0)</code> <code>📜 Skill insights (0)</code> <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <br/> <img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required"> <details> <summary> 1. Timed-out replies break later reads <code>🐞 Bug</code> <code>☼ Reliability</code></summary> <br/> > <details open> ><summary>Description</summary> ><br/> > ><pre> ><b><i>hiredis_read_internal</i></b> installs a stack-local <b><i>reader_state</i></b> on every call, while hiredis persists >active tasks whose <b><i>privdata</i></b> still points to the prior invocation and the connection now has no >marker for their Ruby objects. When an aggregate response is partially parsed before a recoverable >read timeout, the next read resumes through that dangling state or a fresh empty stack, so parent >and map-key lookup can fail and a GC between calls can reclaim the in-progress reply. ></pre> ></details> > <details> ><summary>Code</summary> ><br/> > ><code>[hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[R696-699]](https://github.com/drape-io/crbench-qodo-2026-09/pull/66/files#diff-acac80492782f9a778f40ebe6e63de4fdeeb330e707e79e14d78e153f259774cR696-R699)</code> > >```diff >+ .stack = rb_ary_new(), >+ .task_index = &connection->context->reader->ridx, >+ }; >+ connection->context->reader->privdata = &reader_state; >``` ></details> > <details> ><summary>Evidence</summary> ><br/> > ><pre> >Aggregate parsing stores the callback object and current <b><i>privdata</i></b> in hiredis tasks, then leaves >those tasks active when more input is required. The read loop can return a recoverable timeout >without resetting that parser state, but the next invocation creates another local state; meanwhile >the connection data type has no mark callback, so the raw Ruby references retained by hiredis do not >keep their objects alive. ></pre> > > <code>[hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[140-167]](https://github.com/drape-io/crbench-qodo-2026-09/blob/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c/#L140-L167)</code> > <code>[hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[353-360]](https://github.com/drape-io/crbench-qodo-2026-09/blob/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c/#L353-L360)</code> > <code>[hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[687-699]](https://github.com/drape-io/crbench-qodo-2026-09/blob/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c/#L687-L699)</code> > <code>[hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[729-755]](https://github.com/drape-io/crbench-qodo-2026-09/blob/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1/hiredis-client/ext/redis_client/hiredis/hiredis_connection.c/#L729-L755)</code> > <code>[hiredis-client/ext/redis_client/hiredis/vendor/read.c[504-522]](https://github.com/drape-io/crbench-qodo-2026-09/blob/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1/hiredis-client/ext/redis_client/hiredis/vendor/read.c/#L504-L522)</code> > <code>[hiredis-client/ext/redis_client/hiredis/vendor/read.c[701-715]](https://github.com/drape-io/crbench-qodo-2026-09/blob/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1/hiredis-client/ext/redis_client/hiredis/vendor/read.c/#L701-L715)</code> ></details> > <details> ><summary>Agent prompt</summary> ><br/> > >``` >The issue below was found during a code review. Follow the provided context and guidance below and implement a solution > >## Issue description >The hiredis parser preserves active tasks across recoverable read timeouts, but `hiredis_read_internal` gives those tasks a pointer to invocation-local state. Partial Ruby replies also become invisible to GC because the connection no longer marks them. > >## Fix Focus Areas >- hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[135-167] >- hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[353-360] >- hiredis-client/ext/redis_client/hiredis/hiredis_connection.c[687-699] > >## Recommended Fix >Store the reader state in stable connection-owned memory and retain the same Ruby stack while `reader->ridx` represents a partial reply. Restore GC marking and compaction handling for every Ruby value retained through the reader, clear or replace the state only after the reply completes, and ensure all existing and newly created tasks reference the stable state. >``` > <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code> ></details> <hr/> </details> <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <!-- qodo-context:start --> <details><summary><strong>Context sources</strong></summary> <div>Review mode: <code>⚖️ Balanced</code>: This changes native C reply parsing, Ruby GC behavior, and memory/lifetime handling across multiple paths, creating substantial correctness risk despite being confined to one file.</div> <!-- qodo-context:end --> </details> <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <!-- qodo-daily-tip:start --> <details> <summary><strong>Tip of the day</strong></summary> <br/> <pre>💡 Did you know, you can reply &#x27;qodo&#x27; on any finding to push back, ask questions, or dig deeper</pre> <a href="https://docs.qodo.ai/tips-and-tricks">More tips ↗</a> | <a href="https://app.qodo.ai/configurations?tab=display-preferences">Customize Qodo ↗</a> | <a href="https://docs.qodo.ai">Qodo docs ↗</a> </details> <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <!-- qodo-daily-tip:end --> <!-- https://github.com/drape-io/crbench-qodo-2026-09/commit/8dcc3ff12b78da8a8bcbe1afd976ae5cf983ead1 --> <a href="https://www.qodo.ai"><img src="https://www.qodo.ai/wp-content/uploads/2025/03/qodo-logo.svg" width="80" alt="Qodo Logo"></a>