From 45e75dc68ef608ec4078c876bac592395eb64b8d Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 22:18:46 +0530 Subject: [PATCH 1/5] fix(streams): name missing groups and tighten XINFO STREAM shapes --- CHANGELOG.md | 4 +++ docs/COMPATIBILITY.md | 6 ++-- src/stream_groups.rs | 26 ++++++++++++++---- tests/stream_groups.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faf294f..5b99c43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,10 @@ Because ReRu is pre-1.0, minor versions may include breaking changes. - `GETEX` validates option syntax first, then the key and type, and only then the expiry value, so a missing key replies nil and a wrong type replies WRONGTYPE without value errors. +- `XINFO STREAM` argument-shape errors and per-group `NOGROUP` texts match + Redis: `XGROUP`/`XINFO CONSUMERS` name the missing group, `XPENDING` keeps the + combined key/group text, and malformed `XINFO STREAM` shapes report the + subcommand error. - Hash field deadlines are capped at the 46-bit millisecond range Redis uses: larger `HEXPIRE`/`HPEXPIRE`/`HEXPIREAT`/`HPEXPIREAT`/`HSETEX`/`HGETEX` values report `invalid expire time in '' command`. `HTTL` rounds the remaining diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index a793487..1fa2e00 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -392,10 +392,8 @@ subcommands are reported. A few stream keyword comparisons remain exact rather than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` options with a NUL suffix), `XAUTOCLAIM`'s start ID, `XGROUP SETID`, and `XREAD` IDs do not accept `+`/`-`, `XGROUP DESTROY` on a missing key replies 0, `ENTRIESREAD` uses the generic -integer error and its reporting differs from Redis, `XINFO STREAM ... COUNT` -without `FULL` reports a syntax error, an existing key with a missing group uses -the combined NOGROUP text, and some `XTRIM`/`XREAD` arity and syntax texts still -differ. `XAUTOCLAIM` implements bounded scanning +integer error and its reporting differs from Redis, and some `XTRIM`/`XREAD` +arity and syntax texts still differ. `XAUTOCLAIM` implements bounded scanning (10 x COUNT), a COUNT shared by claims and deleted pending entries, JUSTID, the Redis 7 deleted-ID reply, and registers its consumer even when it claims nothing. These provide at-least-once processing primitives, not exactly-once diff --git a/src/stream_groups.rs b/src/stream_groups.rs index a5a23d0..667463d 100644 --- a/src/stream_groups.rs +++ b/src/stream_groups.rs @@ -130,10 +130,15 @@ fn stream_mut<'a>(entries: &'a mut HashMap, Entry>, key: &[u8]) -> Resul } } fn group_mut<'a>(stream: &'a mut Stream, key: &[u8], name: &[u8]) -> Result<&'a mut Group> { - stream - .groups - .get_mut(name) - .ok_or_else(|| nogroup(key, name, false)) + stream.groups.get_mut(name).ok_or_else(|| { + let message = format!( + "No such consumer group '{}' for key name '{}'", + String::from_utf8_lossy(name), + String::from_utf8_lossy(key) + ) + .replace(['\r', '\n'], " "); + format!("-NOGROUP {message}\r\n").into_bytes() + }) } fn entry_reply(id: StreamId, entry: Option<&StreamEntry>) -> Vec { array(vec![ @@ -620,7 +625,11 @@ fn pending(c: &[Vec], store: &Store) -> Result> { if !entries.contains_key(&c[1]) { return Err(nogroup(&c[1], &c[2], false)); } - let group = group_mut(stream_mut(&mut entries, &c[1])?, &c[1], &c[2])?; + let stream = stream_mut(&mut entries, &c[1])?; + let group = stream + .groups + .get_mut(&c[2]) + .ok_or_else(|| nogroup(&c[1], &c[2], false))?; if let Some(((start, exclusive_start), (end, exclusive_end), count, consumer, idle)) = range { let now = now_ms(); return Ok(array( @@ -851,7 +860,12 @@ fn stream_info(c: &[Vec], store: &Store) -> Result> { 3 => 10, 4 if full => 10, 6 if full && c[4].eq_ignore_ascii_case(b"COUNT") => number(&c[5])? as usize, - _ => return syntax(), + _ => { + return Err(protocol::error(&format!( + "unknown subcommand or wrong number of arguments for '{}'. Try XINFO HELP.", + String::from_utf8_lossy(&c[1]) + ))) + } }; let limit = if count == 0 { usize::MAX } else { count }; let mut entries = store.entries.lock().unwrap(); diff --git a/tests/stream_groups.rs b/tests/stream_groups.rs index 3f58e78..d88a6f1 100644 --- a/tests/stream_groups.rs +++ b/tests/stream_groups.rs @@ -1641,3 +1641,65 @@ fn xclaim_option_names_are_c_strings() { array(vec![]) ); } + +#[test] +fn stream_group_texts_name_the_group_and_xinfo_stream_shapes() { + let server = Server::start(); + let mut c = Client::new(&server); + assert!(matches!( + c.call(&[b"XADD", b"s", b"1-0", b"f", b"v"]), + Reply::Bulk(Some(_)) + )); + + let missing_group = + || Reply::Error(b"NOGROUP No such consumer group 'nogrp' for key name 's'".to_vec()); + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"nogrp", b"0"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XGROUP", b"CREATECONSUMER", b"s", b"nogrp", b"c"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XGROUP", b"DELCONSUMER", b"s", b"nogrp", b"c"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XINFO", b"CONSUMERS", b"s", b"nogrp"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XPENDING", b"s", b"nogrp"]), + Reply::Error(b"NOGROUP No such key 's' or consumer group 'nogrp'".to_vec()) + ); + + let shape = |token: &str| { + Reply::Error( + format!( + "ERR unknown subcommand or wrong number of arguments for '{token}'. Try XINFO HELP." + ) + .into_bytes(), + ) + }; + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"COUNT", b"1"]), + shape("STREAM") + ); + assert_eq!( + c.call(&[b"XINFO", b"stream", b"s", b"count", b"1"]), + shape("stream") + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"FULL", b"COUNT", b"1", b"x"]), + shape("STREAM") + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"FULL", b"BAD"]), + shape("STREAM") + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"COUNT"]), + shape("STREAM") + ); +} From 7b0132a41d65dd2e9d5dda110e26cec7df97c619 Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 22:33:04 +0530 Subject: [PATCH 2/5] fix(streams): resolve keys before XINFO shapes and groups before SETID IDs Check the key and type before the XINFO STREAM shape, resolve the group before parsing XGROUP SETID IDs, echo raw non-UTF-8 names, and cut NUL tokens from the shape error. --- CHANGELOG.md | 4 +- src/stream_groups.rs | 102 ++++++++++++++++++++++++----------------- tests/stream_groups.rs | 51 +++++++++++++++++++++ 3 files changed, 115 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b99c43..996c099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,7 +72,9 @@ Because ReRu is pre-1.0, minor versions may include breaking changes. - `XINFO STREAM` argument-shape errors and per-group `NOGROUP` texts match Redis: `XGROUP`/`XINFO CONSUMERS` name the missing group, `XPENDING` keeps the combined key/group text, and malformed `XINFO STREAM` shapes report the - subcommand error. + subcommand error after the key and type are resolved. `XGROUP SETID` resolves + the group before parsing the ID, and non-UTF-8 key/group names are echoed as + raw bytes. - Hash field deadlines are capped at the 46-bit millisecond range Redis uses: larger `HEXPIRE`/`HPEXPIRE`/`HEXPIREAT`/`HPEXPIREAT`/`HSETEX`/`HGETEX` values report `invalid expire time in '' command`. `HTTL` rounds the remaining diff --git a/src/stream_groups.rs b/src/stream_groups.rs index 667463d..383cd19 100644 --- a/src/stream_groups.rs +++ b/src/stream_groups.rs @@ -104,19 +104,33 @@ fn syntax() -> Result { fn wrongtype() -> Vec { b"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".to_vec() } +fn push_quoted(out: &mut Vec, bytes: &[u8]) { + out.extend(bytes.iter().map(|byte| match byte { + b'\r' | b'\n' => b' ', + _ => *byte, + })); +} + fn nogroup(key: &[u8], group: &[u8], read: bool) -> Vec { - let msg = format!( - "No such key '{}' or consumer group '{}'{}", - String::from_utf8_lossy(key), - String::from_utf8_lossy(group), - if read { - " in XREADGROUP with GROUP option" - } else { - "" - } - ) - .replace(['\r', '\n'], " "); - format!("-NOGROUP {msg}\r\n").into_bytes() + let mut reply = b"-NOGROUP No such key '".to_vec(); + push_quoted(&mut reply, key); + reply.extend_from_slice(b"' or consumer group '"); + push_quoted(&mut reply, group); + reply.extend_from_slice(if read { + b"' in XREADGROUP with GROUP option\r\n" + } else { + b"'\r\n" + }); + reply +} + +fn missing_group(key: &[u8], group: &[u8]) -> Vec { + let mut reply = b"-NOGROUP No such consumer group '".to_vec(); + push_quoted(&mut reply, group); + reply.extend_from_slice(b"' for key name '"); + push_quoted(&mut reply, key); + reply.extend_from_slice(b"'\r\n"); + reply } fn stream_mut<'a>(entries: &'a mut HashMap, Entry>, key: &[u8]) -> Result<&'a mut Stream> { remove_expired(entries, key); @@ -130,15 +144,10 @@ fn stream_mut<'a>(entries: &'a mut HashMap, Entry>, key: &[u8]) -> Resul } } fn group_mut<'a>(stream: &'a mut Stream, key: &[u8], name: &[u8]) -> Result<&'a mut Group> { - stream.groups.get_mut(name).ok_or_else(|| { - let message = format!( - "No such consumer group '{}' for key name '{}'", - String::from_utf8_lossy(name), - String::from_utf8_lossy(key) - ) - .replace(['\r', '\n'], " "); - format!("-NOGROUP {message}\r\n").into_bytes() - }) + stream + .groups + .get_mut(name) + .ok_or_else(|| missing_group(key, name)) } fn entry_reply(id: StreamId, entry: Option<&StreamEntry>) -> Vec { array(vec![ @@ -177,9 +186,9 @@ pub fn valid_arity(command: &[Vec]) -> Option { b"XPENDING" => n == 3 || n >= 6, b"XCLAIM" | b"XAUTOCLAIM" => n >= 6, b"XINFO" => match command.get(1) { - Some(sub) if xinfo_sub(sub, b"GROUPS") => n == 3, - Some(sub) if xinfo_sub(sub, b"CONSUMERS") => n == 4, - Some(sub) if xinfo_sub(sub, b"STREAM") => n >= 3, + Some(sub) if sub.eq_ignore_ascii_case(b"GROUPS") => n == 3, + Some(sub) if sub.eq_ignore_ascii_case(b"CONSUMERS") => n == 4, + Some(sub) if sub.eq_ignore_ascii_case(b"STREAM") => n >= 3, _ => false, }, b"XSETID" => n >= 3, @@ -191,12 +200,6 @@ pub fn is_mutating(command: &[Vec]) -> bool { && !command[0].eq_ignore_ascii_case(b"XPENDING") && !command[0].eq_ignore_ascii_case(b"XINFO") } -/// XINFO accepts a subcommand with exactly one trailing NUL, as Redis does. -fn xinfo_sub(sub: &[u8], name: &[u8]) -> bool { - let sub = sub.strip_suffix(b"\0").unwrap_or(sub); - sub.eq_ignore_ascii_case(name) -} - fn sub_token(command: &[Vec]) -> Option<&[u8]> { command .get(1) @@ -214,7 +217,9 @@ fn known_subcommand(command: &[Vec]) -> bool { Some(b"CREATE" | b"SETID" | b"DESTROY" | b"CREATECONSUMER" | b"DELCONSUMER") ), b"XINFO" => command.get(1).is_some_and(|sub| { - xinfo_sub(sub, b"STREAM") || xinfo_sub(sub, b"GROUPS") || xinfo_sub(sub, b"CONSUMERS") + sub.eq_ignore_ascii_case(b"STREAM") + || sub.eq_ignore_ascii_case(b"GROUPS") + || sub.eq_ignore_ascii_case(b"CONSUMERS") }), _ => true, } @@ -309,10 +314,15 @@ fn group_command(c: &[Vec], store: &Store) -> Result> { } else { let _ = stream_mut(&mut entries, key)?; } - // Parse the ID only after the key and type are resolved. - let parsed_id = match raw_id { - Some(raw) => Some(id(raw)?), - None => None, + // CREATE validates the ID before creating an empty stream; SETID resolves + // the group before parsing it. + let parsed_id = if create { + match raw_id { + Some(raw) => Some(id(raw)?), + None => None, + } + } else { + None }; if missing { entries.insert( @@ -325,10 +335,20 @@ fn group_command(c: &[Vec], store: &Store) -> Result> { } let stream = stream_mut(&mut entries, key)?; let reply = if create || setid { + if setid && !stream.groups.contains_key(name) { + return Err(missing_group(key, name)); + } + let last = if create { + parsed_id.unwrap_or(stream.last_generated_id) + } else { + match raw_id { + Some(raw) => id(raw)?, + None => stream.last_generated_id, + } + }; if create && stream.groups.contains_key(name) { return Err(b"-BUSYGROUP Consumer Group name already exists\r\n".to_vec()); } - let last = parsed_id.unwrap_or(stream.last_generated_id); if create { stream.groups.insert( name.clone(), @@ -764,12 +784,12 @@ fn estimate_read( fn info(c: &[Vec], store: &Store) -> Result> { let sub = &c[1]; - if xinfo_sub(sub, b"STREAM") { + if sub.eq_ignore_ascii_case(b"STREAM") { return stream_info(c, store); } let mut entries = store.entries.lock().unwrap(); let stream = stream_mut(&mut entries, &c[2])?; - if xinfo_sub(sub, b"GROUPS") { + if sub.eq_ignore_ascii_case(b"GROUPS") { Ok(array( stream .groups @@ -855,6 +875,8 @@ fn group_lag(stream: &Stream, group: &Group) -> Option { } fn stream_info(c: &[Vec], store: &Store) -> Result> { + let mut entries = store.entries.lock().unwrap(); + let stream = stream_mut(&mut entries, &c[2])?; let full = c.get(3).is_some_and(|v| v.eq_ignore_ascii_case(b"FULL")); let count = match c.len() { 3 => 10, @@ -863,13 +885,11 @@ fn stream_info(c: &[Vec], store: &Store) -> Result> { _ => { return Err(protocol::error(&format!( "unknown subcommand or wrong number of arguments for '{}'. Try XINFO HELP.", - String::from_utf8_lossy(&c[1]) + String::from_utf8_lossy(sub_token(c).unwrap_or(b"")) ))) } }; let limit = if count == 0 { usize::MAX } else { count }; - let mut entries = store.entries.lock().unwrap(); - let stream = stream_mut(&mut entries, &c[2])?; let mut fields = vec![ protocol::bulk(b"length"), integer(stream.entries.len() as u64), diff --git a/tests/stream_groups.rs b/tests/stream_groups.rs index d88a6f1..02917c9 100644 --- a/tests/stream_groups.rs +++ b/tests/stream_groups.rs @@ -1650,6 +1650,7 @@ fn stream_group_texts_name_the_group_and_xinfo_stream_shapes() { c.call(&[b"XADD", b"s", b"1-0", b"f", b"v"]), Reply::Bulk(Some(_)) )); + assert_eq!(c.call(&[b"XGROUP", b"CREATE", b"s", b"g", b"0"]), ok()); let missing_group = || Reply::Error(b"NOGROUP No such consumer group 'nogrp' for key name 's'".to_vec()); @@ -1702,4 +1703,54 @@ fn stream_group_texts_name_the_group_and_xinfo_stream_shapes() { c.call(&[b"XINFO", b"STREAM", b"s", b"COUNT"]), shape("STREAM") ); + + // The key and type are checked before the shape. + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"missing", b"COUNT", b"1"]), + Reply::Error(b"ERR no such key".to_vec()) + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"missing", b"FULL", b"COUNT", b"abc"]), + Reply::Error(b"ERR no such key".to_vec()) + ); + assert_eq!(c.call(&[b"SET", b"str", b"x"]), ok()); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"str", b"FULL", b"BAD"]), + Reply::Error(b"WRONGTYPE Operation against a key holding the wrong kind of value".to_vec()) + ); + + // SETID resolves the group before parsing the ID; CREATE parses first. + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"nogrp", b"badid"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"g", b"badid"]), + Reply::Error(b"ERR Invalid stream ID specified as stream command argument".to_vec()) + ); + assert_eq!( + c.call(&[b"XGROUP", b"CREATE", b"s", b"g", b"badid"]), + Reply::Error(b"ERR Invalid stream ID specified as stream command argument".to_vec()) + ); + assert_eq!( + c.call(&[ + b"XGROUP", + b"CREATE", + b"absent", + b"g2", + b"badid", + b"MKSTREAM" + ]), + Reply::Error(b"ERR Invalid stream ID specified as stream command argument".to_vec()) + ); + + // Non-UTF-8 names are echoed as raw bytes, and NUL-suffixed subcommands are unknown. + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"no\xffgrp", b"0"]), + Reply::Error(b"NOGROUP No such consumer group 'no\xffgrp' for key name 's'".to_vec()) + ); + assert_eq!( + c.call(&[b"XINFO", b"stream\x00", b"s", b"COUNT", b"1"]), + Reply::Error(b"ERR unknown subcommand 'stream'. Try XINFO HELP.".to_vec()) + ); } From 0b9badc1f5a1e7f198e0ef0ec4812944adad9a5e Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 22:33:07 +0530 Subject: [PATCH 3/5] ci: retrigger From e9472d517e53e62a7059d63384169cd9b964982e Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 22:40:09 +0530 Subject: [PATCH 4/5] fix(streams): truncate NUL in quoted stream names --- docs/COMPATIBILITY.md | 3 ++- src/stream_groups.rs | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index 1fa2e00..74d2cff 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -389,7 +389,8 @@ Stream IDs are parsed as C strings: leading whitespace, a leading `+`, leading zeros, and a NUL terminator are accepted, while trailing bytes are not. Stream subcommand arity errors name the command or `parent|subcommand`, and unknown subcommands are reported. A few stream keyword comparisons remain exact rather -than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` options with a NUL suffix), +than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` and `XINFO` options with a +NUL suffix), `XAUTOCLAIM`'s start ID, `XGROUP SETID`, and `XREAD` IDs do not accept `+`/`-`, `XGROUP DESTROY` on a missing key replies 0, `ENTRIESREAD` uses the generic integer error and its reporting differs from Redis, and some `XTRIM`/`XREAD` diff --git a/src/stream_groups.rs b/src/stream_groups.rs index 383cd19..f91cdb2 100644 --- a/src/stream_groups.rs +++ b/src/stream_groups.rs @@ -105,10 +105,16 @@ fn wrongtype() -> Vec { b"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".to_vec() } fn push_quoted(out: &mut Vec, bytes: &[u8]) { - out.extend(bytes.iter().map(|byte| match byte { - b'\r' | b'\n' => b' ', - _ => *byte, - })); + // Redis formats these names with %s: stop at NUL, keep other bytes raw. + out.extend( + bytes + .iter() + .take_while(|byte| **byte != 0) + .map(|byte| match byte { + b'\r' | b'\n' => b' ', + _ => *byte, + }), + ); } fn nogroup(key: &[u8], group: &[u8], read: bool) -> Vec { From 4766a772e7800e76b45c088ab694694758ea66b7 Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 22:40:12 +0530 Subject: [PATCH 5/5] ci: retrigger