Fix client output buffer memory tracking not accounting for copy-avoided bulk string references by sundb · Pull Request #14934 · redis/redis
Problem
After #14608 (Reply Copy Avoidance), when copy avoidance is used, bulk string replies are sent by reference instead of being copied into the output buffer.
However, the referenced bytes are not counted in reply_bytes, which causes:
getClientOutputBufferMemoryUsage()underestimates the actual memory usage, so output buffer limits may not be triggered in time, allowing clients to consume unbounded memory.- Client eviction does not account for the referenced bytes, making it ineffective when copy avoidance is used.
omemreported inCLIENT LIST/CLIENT INFOdoes not reflect the true output buffer memory footprint.
Solution
Add a new per-client field reply_bytes_ref to track the total bytes of referenced bulk strings in the output buffer.
ormem is now taken into account by client eviction and output buffer limit checks. Even though the string object is not copied into the client output buffer, the referenced memory is still charged to the client.
This ensures consistent behavior with that prior to #14608.
Info field
- Expose the new field
ormem(Output Referenced memory) inCLIENT LIST/CLIENT INFO.
Bug Fix
Fix missing closeClientOnOutputBufferLimitReached() call when adding a referenced robj to the reply
Note
Medium Risk
Touches core client output buffering and limit enforcement; mistakes could cause incorrect client eviction/closure or memory accounting regressions under load, but changes are localized to tracking referenced reply bytes.
Overview
Fixes client output buffer accounting when reply copy-avoidance sends bulk strings by reference by introducing per-client reply_bytes_ref and treating it as logical output buffer usage.
Updates reply building/sending paths to increment/decrement reply_bytes_ref, enforce output buffer limits when adding referenced payloads, and include referenced bytes in pending-reply calculations, eviction/limit checks, and serverAssert invariants. Exposes the new metric via CLIENT LIST/INFO as ormem and extends INFO MEMORY / MEMORY STATS with mem_clients_ref and clients.ref plus an orphan breakdown (mem_clients_orphan_ref / clients.orphan.ref) computed by scanning encoded reply buffers.
Adjusts tests to reflect the new fields and adds coverage for ormem/ref counters, while removing most blanket disabling of reply copy avoidance (kept only where needed to trigger eviction).
Written by Cursor Bugbot for commit 9c91d08. This will update automatically on new commits. Configure here.