Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.

Strip every repeated LIMIT when rewriting XADD/XTRIM for propagation - #108

Closed
madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-stream-strip-repeated-limit
Closed

madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-stream-strip-repeated-limit

Conversation

@madolson

Copy link
Copy Markdown
Owner

XADD and XTRIM accept LIMIT more than once and keep the last value, but the propagation rewrite from #4063 strips only the last LIMIT. XADD s MAXLEN ~ 55 LIMIT 5 LIMIT 30 * f v propagates as MAXLEN = 71 LIMIT 5, which a client that is not mustObeyClient, such as a migration tool replaying the stream, rejects. This records the first LIMIT position and drops every LIMIT pair in the options. Replicas and AOF loading are unaffected because they ignore LIMIT.

AI generated details

Problem

streamParseAddOrTrimArgsOrReply overwrote limit_arg_idx on every LIMIT (src/t_stream.c:975), so streamRewriteStripLimit removed only the last pair. Whatever LIMIT is left ends up next to the rewritten MAXLEN = / MINID =, and a non-obeyed replay fails:

ERR syntax error, LIMIT cannot be used without the special ~ option

Introduced by #4063 (c03970a), first in 9.2.0-rc1. The repeated-LIMIT parsing goes back to #8169. Before #4063, every explicit LIMIT was propagated.

Command Propagated before After
XADD s MAXLEN ~ 55 LIMIT 5 LIMIT 30 * f v MAXLEN = 71 LIMIT 5 MAXLEN = 71
XADD s LIMIT 5 MAXLEN ~ 55 LIMIT 30 * f v LIMIT 5 MAXLEN = 71 MAXLEN = 71
XTRIM s MAXLEN ~ 50 LIMIT 5 LIMIT 30 MAXLEN = 70 LIMIT 5 MAXLEN = 70

The existing test from #4063 passes a single LIMIT, so it never hit this.

Fix

The parser records the first LIMIT (src/t_stream.c:977). streamRewriteStripAllLimits (src/t_stream.c:2099) scans from there to the end of the options (idpos for XADD, argc for XTRIM) and calls the existing streamRewriteStripLimit on each LIMIT token. Matching by string is safe because every option value has already parsed as an integer, a stream ID, ~ or =, and none of those can be limit. The key and the fields are outside the scanned range. The rewritten trim argument can be integer encoded, hence the sdsEncodedObject() check.

Alternative considered

Rejecting repeated LIMIT at parse time, as repeated MAXLEN already is. That makes a command that has been accepted since LIMIT was introduced fail, which is a compatibility change and not a fix to the rewrite.

Testing

New test in tests/unit/type/stream.tcl that covers LIMIT before and after the trim strategy, with NOMKSTREAM, for XADD and XTRIM. With src/t_stream.c reverted:

[err]: XADD/XTRIM strip every repeated LIMIT when rewriting for propagation in tests/unit/type/stream.tcl
Expected '-1' to be equal to '6883' (context: type eval line 20 cmd {assert_equal -1 [string first "LIMIT" $blob]} proc ::test)

This was generated by AI but verified, with love, by a human.

LIMIT may be given more than once, and the parser keeps only the last
value. The propagation rewrite added in #4063 recorded only the last
LIMIT position, so XADD s MAXLEN ~ 10 LIMIT 5 LIMIT 6 * f v propagated as
MAXLEN = <len> LIMIT 5. A client that is not mustObeyClient, such as a
migration tool replaying the stream, gets "LIMIT cannot be used without
the special ~ option" for it.

Record the first LIMIT position and drop every LIMIT pair between it and
the end of the options.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

promoted Change has been promoted upstream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant