[BUG] A failed XADD ... IDMP leaves an idempotency producer that no cleanup path can ever free
Describe the bug
In xaddCommand() (src/t_stream.c), idmpGetOrCreateProducer() allocates
s->idmp_producers and inserts a new idmpProducer before any of the
error checks, while trackStreamIdmpEntries() — the only place that puts the
key into db->stream_idmp_keys — runs only after streamAppendItem() has
succeeded:
if (parsed_args.idmp_pid != NULL) {
producer = idmpGetOrCreateProducer(s, pid_str, pid_len); /* allocates */
...
}
/* Return ASAP if the stream has reached the last possible ID */
if (s->last_id.ms == UINT64_MAX && s->last_id.seq == UINT64_MAX) {
addReplyError(c,"The stream has exhausted the last possible ID, ...");
idmpEntryFree(entry, &s->alloc_size);
return; /* entry freed; producer kept; key never registered */
}
if (streamAppendItem(...) == C_ERR) {
...
idmpEntryFree(entry, &s->alloc_size);
return; /* same */
}
...
if (parsed_args.idmp_pid != NULL) {
idmpInsertEntry(s, producer, entry, &id);
trackStreamIdmpEntries(c, c->argv[1]); /* success path only */
}handleExpiredIdmpEntries() only ever iterates db->stream_idmp_keys
(if (dictIsEmpty(db->stream_idmp_keys)) continue;), so the empty producers
left behind by a rejected command are never visited and never freed.
stream-idmp-duration, which the docs describe as the bound on this state, is
never applied to them.
The cleanup itself is fine, it is just never scheduled: one successful
XADD ... IDMP on the same key registers it, and the next cron cycle frees
every leaked producer. A stream whose ID space is exhausted can never get that
write, so its leak is permanent and grows with every retry.
To reproduce
stream-idmp-duration 1 (the minimum), 5000 distinct producer ids per stream,
two streams, then wait six seconds:
XADD ok IDMP p<i> i0 * f v # x5000, succeeds
XADD full 18446744073709551615-18446744073709551615 f v
XADD full IDMP p<i> i0 * f v # x5000, every one returns
# "The stream has exhausted the last possible ID" pids before pids after 6s MEMORY USAGE
5000 successful XADD ... IDMP 5000 0 507628 -> 59572
5000 failed XADD ... IDMP (exhausted ID) 5000 5000 248340XLEN full is 1 — the 5000 rejected commands stored nothing, yet the stream
now carries 5000 producers (XINFO STREAM full → idmp-producers) and about
248 KB that will never be released. The identical producer count on the healthy
stream is reclaimed to zero in the same window.
A second trigger through the same path is an entry whose summed element length
exceeds STREAM_LISTPACK_MAX_SIZE (streamAppendItem() → ERANGE, "Elements
are too large to be stored"): 40 rejected XADD … IDMP commands of 1100 ×
1 MiB fields left exactly 40 producers on an otherwise healthy stream.
A self-checking Python reproducer (exit 1 when present, 0 when fixed; starts
its own redis-server) is attached / available on request. It also verifies
that the healthy control stream really is reclaimed, so a non-zero count on
the failed stream can only be about the failure path.
Expected behavior
A command that returns an error and stores nothing should leave no per-stream
IDMP state behind — or, if it does, that state should be subject to
stream-idmp-duration like every other producer. Either create the producer
only once the append has succeeded (resolve it with a non-creating lookup for
idmpLookupAndReply() and create it alongside idmpInsertEntry()), or free a
freshly created, still-empty producer (and the idmp_producers rax if it
becomes empty) on each error return, or call trackStreamIdmpEntries() as soon
as the producer is created.
Additional information
- Reproduced on unstable @ c3215f01 (
REDIS_VERSION 8.9.241, 2026-09-22) and on the released 8.10.2 (498ecd0d); both built from source withmake MALLOC=jemalloc. - Ubuntu 22.04.2, Linux 5.15.0-187-generic, gcc 11.4.0, x86_64.
- On
unstablethe producer is created atsrc/t_stream.c:2581and the error returns that keep it are at lines 2614 (exhausted ID) and 2623–2635 (streamAppendItemfailure);trackStreamIdmpEntries()is only called at line 2643.handleExpiredIdmpEntries()(line 6259) iteratesdb->stream_idmp_keysonly. - Searched the tracker for
idmp producer,IDMP leak,XADD IDMP: the nearest items are #14869 (cron expiration after RDB load) and #14897 (stream_idmp_keysin database lifecycle ops), both about keys that were registered. Nothing about the failure path.
Source: redis/redis