Skip to content

Drain RESP3 attributes so they never reach the kept reply - #882

Merged
fcostaoliveira merged 8 commits into
masterfrom
fix/resp3-attribute-tree
Oct 26, 2026
Merged

fcostaoliveira merged 8 commits into
masterfrom
fix/resp3-attribute-tree

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #458.

redis_protocol::parse_response() treated a RESP3 attribute (|N) like an ordinary aggregate. Its key/value pairs became part of the reply. With set_keep_value(true) that crashes. The three forms in the issue comments all reproduce on master under ASan + UBSan:

  • |0\r\n*1\r\n+a\r\n: the attribute becomes the root of the kept tree, and the next aggregate calls add_new_element() on a NULL cursor (protocol.cpp:764).
  • An attribute inside an attribute hits assert(!m_attribute).
  • An attribute on an array element, *2\r\n|1\r\n+k\r\n+v\r\n+a\r\n*1\r\n+b\r\n: it takes one of the parent's slots, so the reply ends early, and the following aggregate dereferences NULL.

Without keep_value, the attribute's elements were counted as hits and as ArrayPerElementNulls elements, which is the miscount the issue describes. An error inside an attribute also marked the whole reply as an error; it no longer does.

Fix (protocol.cpp only). An attribute is now drained in its own scope, the same way a nested push (>) already is. It takes no slot from its parent, and anything inside it (another attribute, or a push) is drained with it. When it ends, parsing continues as if it weren't there; at top level, the reply goes on. Nothing from an attribute is ever added to the reply tree, so there is nothing to free.

Tests. A new check_attribute_frames() in tests/unit/redis_protocol_test.cpp covers:

  • the three crash forms
  • an attribute before *0 (still empty for EmptyCollection, with and without keep_value)
  • an attribute inside a map
  • an attribute on an array element (gives [miss, hit] for ArrayPerElementNulls)
  • attribute bulks not counted as hits

Against master's protocol.cpp, each crash form aborts under ASan (null member call, or the assertion) and the counting cases give wrong results. With the fix every case passes.

Results (Ubuntu 24.04 container, ASan + UBSan):

  • make check passes, and clang-format is clean.
  • test_resp_response_fuzzer.py, test_read_preference_resp3.py, test_arbitrary_command_misses.py and test_collection_scan.py pass 47/47.

#832 (also in protocol.cpp / redis_protocol_test.cpp) merges cleanly with this, and the merged tree's sanitized unit test passes, including #832's nested-attribute bound checks. Once both land, a comment in #832 saying a nested attribute "takes a parent slot" will be out of date.

Release notes (proposed)

Fixes crashes and miscounts when a RESP3 server sends attribute frames (|): attribute contents are no longer counted in Hits/Misses or per-element miss tracking, and no longer reach the kept reply (#458).

🤖 Generated with Claude Code


Note

Medium Risk
Changes core RESP3 reply parsing and completion logic; incorrect draining could mis-frame replies or miscount benchmark metrics, but behavior is heavily covered by new unit tests.

Overview
Fixes #458 by treating RESP3 attribute frames (|) as drain-only metadata, like nested push (>), instead of ordinary aggregates.

redis_protocol::parse_response() enters push/drain mode when an attribute is seen: it does not consume a parent slot, does not build the kept mbulk tree, and does not affect hits, per-element miss tracking, top-level array length, or reply error state. Nested attributes and pushes inside an attribute are drained with it. response_ended() was updated so finishing an attribute continues parsing the real reply; m_push_outer_attribute was removed as redundant.

Tests: new check_attribute_frames() covers crash cases, keep_value tree shape, EmptyCollection, ArrayPerElementNulls, and updated overflow-bound comments for attributes taking no parent slot.

Reviewed by Cursor Bugbot for commit c22e077. Bugbot is set up for automated code reviews on this repo. Configure here.

An attribute frame (|N) was parsed as an ordinary aggregate: its pairs were
materialized as the root of the kept mbulk tree, counted as hits and as
tracked elements, and a nested attribute took a slot of the enclosing
aggregate. With keep_value the following reply then wrote through a NULL
cursor (|0 *1 +a, or an attribute on an array element), and an attribute
inside an attribute hit the !m_attribute assert. Without keep_value an
attribute on an array element was recorded as two extra elements for
ArrayPerElementNulls, and attribute bulks were counted as hits.

An attribute only decorates the element after it, so drain it in its own
scope with the existing nested-push machinery: it takes no slot of its
parent, writes nothing into the reply, and anything inside it (another
attribute or a push) is drained with it. When it ends the parser resumes
as if it had not been there; at top level the reply goes on. Fixes #458.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. I checked the new | handling against the cases in the description and couldn't find a problem with it:

  • A top-level |0 / |N saves m_total_bulks_count == 0 and drains its pairs. The nested branch of response_ended() then returns false because of was_attribute, so the following *1 is parsed as a fresh top-level aggregate. That fixes the NULL cursor in the kept tree.
  • On an array element, the line[0] != '|' guard stops the attribute from taking the parent's slot. The saved count is restored when the attribute ends, so *2 |1 +k +v +a *1 +b still sees two elements.
  • An attribute that shows up while m_push is already set (inside another attribute, or inside a push) only adds its own pairs to the open scope's count. So one level of m_push_outer_bulks is enough, and dropping m_push_outer_attribute looks safe to me.
  • track_leaf() and the error/status writes are already gated on m_push, so the hit counting and "error inside attribute" changes follow from that.

The tests exercise the three crash shapes plus the EmptyCollection and ArrayPerElementNulls counting cases, and they fail on master. That's the right bar for a parser fix like this.

The visible change for users is that Hits/Misses (and the per-element miss tracking) no longer count attribute contents when the server sends attributes. That fixes the miscount reported in #458; it doesn't break compatibility, but it may be worth a line in the release notes.

You already flagged the #832 comment that says a nested attribute "takes a parent slot". Whichever of the two PRs merges second should fix that comment, so the stale wording doesn't end up in master.

Nitpicking: m_attribute now just records which kind of nested scope is being drained. The updated m_push comment says so, so a rename isn't needed.

The core fix looks solid to me. Since this is core RESP3 framing, it may still be worth a second look from whoever owns the protocol parser before merge.

…in attributes

An attribute is always drained in a nested scope now, which the nested
branch of response_ended() ends; m_attribute is never set past it, so
the was_attribute handling and its comment no longer describe anything.
Add cases for an error inside an attribute (not the reply's error) and
after it (the reply's error).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Confirmed, and removed in 58633aa. With this change m_attribute is only true while m_push && m_push_nested is set, and the nested branch of response_ended() ends that state, restoring m_attribute from m_push_outer_attribute. That field is only saved in the > path, where m_attribute is already false. So was_attribute in the tail was always false. A runtime check that aborts if it is ever true never fired in 200k randomized replies (attributes in every position, mixed with pushes and nested pushes).

58633aa removes the was_attribute local, its clear and the stale comment, and the tail now returns on was_push alone. m_push_outer_attribute itself stays for now. It's always false, so removing it would also be safe, but it's a wider edit to protocol.cpp near #832's changes. Whichever of the two lands second can drop it.

The same commit adds unit cases for an error inside an attribute (|1 +k -ERR x +OK is not an error reply) and one after it (|1 +k +v -ERR x is). That's a side effect of the fix the description didn't mention: an error inside an attribute no longer marks the reply as an error. Sanitized make check passes, the RESP3 integration tests pass 32/32, and with master's protocol.cpp the unit test fails.

An attribute is drained in its own nested scope and only when no push is
being drained, so a push nested in an aggregate never starts while an
attribute is open: the saved flag was always false. Restore false
directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Right, it's dead. Removed in 142b915. An attribute only opens its drain scope when !m_push, and the nested-push branch also requires !m_push, so a push is never nested while an attribute is open. m_push_outer_attribute = m_attribute therefore always saved false. The field is gone, and the nested branch of response_ended() now sets m_attribute = false directly. The field comment now says an attribute is drained the same way and can't enclose a nested scope. Sanitized make check passes, the RESP3 integration tests pass 32/32, and the branch still merges cleanly with #832. On #832's "takes a parent slot" comment: agreed, whichever lands second will update it.

An attribute sets m_push before the top-array-length and miss-tracking
blocks run, so their line[0] != '|' conditions never changed anything.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Right, they were redundant. Removed in 04b2236. An attribute sets m_push before those blocks run, or m_push is already set when the attribute sits inside a push, so !m_push already excludes it. The comment above them now says an attribute or a push has set m_push. On renaming m_push* to m_drain*: agreed it would read better, but it touches most of the parser and #832's lines, so I'd leave it for a follow-up rather than widen this fix. Sanitized make check passes, the RESP3 tests pass 32/32, and the branch still merges cleanly with #832.

An attribute inside a push takes none of the push's slots: a nested push
holding one fills one slot of its aggregate, and a top-level push holding
one is drained whole before the reply. Master ends such a push early and
hands its last element back as the reply; both cases fail there. In the
nested-push branch m_attribute is already false (an attribute sets
m_push), so stop clearing it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Both done in 15cb853. The new cases are *2 >2 +a |1 +k +v +b +c as one reply (the nested push holding the attribute fills one slot of the *2), with and without keep_value, and a top-level >2 +a |1 +k +v +b followed by +OK, where the push is drained whole and the reply is +OK. With master's protocol.cpp and only these cases, both fail, as you predicted: master ends the push early and hands +b back as the reply. With this branch they pass. The m_attribute = false; in the nested > branch is gone too. Sanitized make check passes, clang-format is clean, and the branch still merges cleanly with #832.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Done in a173343, a comment-only change. m_push's declaration now says it also drains attributes (always nested, with m_attribute set), and that attributes only decorate the element after them, so a reader grepping for m_push sees that track_leaf() and the bulk hit/drain paths skip attributes too. The shard_connection.cpp comment stays accurate as is. And yes, whichever of this and #832 lands second fixes #832's "takes a parent slot" comment.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. I've added a proposed release-notes line to the description: attribute contents are no longer counted in Hits/Misses or per-element miss tracking, and no longer reach the kept reply. And noted again: whichever of this and #832 merges second fixes #832's "takes a parent slot" comment.

fcostaoliveira and others added 2 commits October 26, 2026 10:59
#832's bound comment and unit-test comment said a nested attribute takes
a parent slot; with this PR an attribute is drained in its own scope and
takes none. Keep #832's bound and say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit ea40d9d into master Oct 26, 2026
67 of 69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RESP3 attribute (|) prefix pollutes reply mbulk tree

1 participant