Skip to content

Fix protocol error not reported when a command is split across reads - #15795

Merged
sundb merged 3 commits into
redis:unstablefrom
pirate329:fix-protocol-error-after-partial-read
Sep 24, 2026
Merged

sundb merged 3 commits into
redis:unstablefrom
pirate329:fix-protocol-error-after-partial-read

Conversation

@pirate329

@pirate329 pirate329 commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

A command that is parsed across several reads keeps its pendingCommand alive between calls, with PENDING_CMD_FLAG_INCOMPLETE set. When a protocol error is found on one of the later reads, processMultibulkBuffer() records it in pcmd->read_error and returns early, before the tail of the function that clears the flag. addPendingCommand() only counts a command as ready when that flag is clear, so ready_len stays zero and processInputBuffer() breaks out of the loop before copying read_error to the client. The error is recorded and then dropped: the client never gets a reply, and its query buffer keeps growing until it reaches client-query-buffer-limit.

The same bytes are reported correctly when they arrive in a single read, because the command is then still freshly acquired and not yet flagged as incomplete.

This affects every error raised inside the multibulk argument loop: too big bulk count string, expected '$', invalid bulk length and unauthenticated bulk length. Of these, too big bulk count string was unreachable in practice, because a bulk count string above the 64k limit always spans more than one read.

Clear the flag when a read error is set, so that the command is queued as ready and the error reaches the client.

Tests are added for the two split-read cases and for the three size limits, none of which were covered before.

Runnable reproduction

Save as repro.py and run it against a server with python3 repro.py [port].

The time.sleep() between the two writes is the essential part: it forces the server into a separate read(). Without it both halves usually arrive in a single read and the error is reported correctly, which hides the problem.

import socket, sys, time

HOST, PORT = "127.0.0.1", int(sys.argv[1]) if len(sys.argv) > 1 else 6379

def send(label, parts, gap=0.3):
    s = socket.create_connection((HOST, PORT), timeout=5)
    s.settimeout(5)
    try:
        for i, p in enumerate(parts):
            if i:
                time.sleep(gap)   # force the server into a separate read()
            s.sendall(p)
        reply = s.recv(200).decode(errors="replace").strip()
    except socket.timeout:
        reply = "<no reply: client left hanging>"
    finally:
        s.close()
    print("%-22s -> %s" % (label, reply))

send("expected-$ 1 write",  [b"*3\r\n$3\r\nSET\r\n$1\r\nx\r\nfooz\r\n"])
send("expected-$ 2 writes", [b"*3\r\n$3\r\nSET\r\n$1\r\nx\r\n", b"fooz\r\n"])
send("bad buflen 1 write",  [b"*3\r\n$3\r\nSET\r\n$1\r\nx\r\n$blabla\r\n"])
send("bad buflen 2 writes", [b"*3\r\n$3\r\nSET\r\n$1\r\nx\r\n", b"$blabla\r\n"])
send("too big bulk count",  [b"*1\r\n$", b"1" * (64 * 1024 + 16)])

Before this patch:

expected-$ 1 write     -> -ERR Protocol error: expected '$', got 'f'
expected-$ 2 writes    -> <no reply: client left hanging>
bad buflen 1 write     -> -ERR Protocol error: invalid bulk length
bad buflen 2 writes    -> <no reply: client left hanging>
too big bulk count     -> <no reply: client left hanging>

After:

expected-$ 1 write     -> -ERR Protocol error: expected '$', got 'f'
expected-$ 2 writes    -> -ERR Protocol error: expected '$', got 'f'
bad buflen 1 write     -> -ERR Protocol error: invalid bulk length
bad buflen 2 writes    -> -ERR Protocol error: invalid bulk length
too big bulk count     -> -ERR Protocol error: too big bulk count string

The hanging connections stay open and keep buffering. On an unpatched build they are only closed once they reach client-query-buffer-limit, which defaults to 1GB.


Note

Medium Risk
Changes core client input parsing in networking.c; behavior is narrower (fail fast with an error reply) but affects every multibulk parse error path.

Overview
Fixes a bug where RESP multibulk protocol errors detected on a later read were stored on the pending command but never sent to the client, leaving connections hanging and the query buffer growing until client-query-buffer-limit.

In processMultibulkBuffer(), error paths that set pcmd->read_error now jump to a shared readerr label instead of returning immediately. That path clears PENDING_CMD_FLAG_INCOMPLETE so the command is treated as ready and processInputBuffer() can surface the error—matching behavior when the bad bytes arrive in one read.

tests/unit/protocol.tcl adds coverage for split-write protocol failures (wrong bulk prefix, invalid bulk length) and for oversized inline/multibulk/bulk count strings that previously could not be exercised when the header spanned reads.

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

@CLAassistant

CLAassistant commented Sep 15, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread src/networking.c Resolved
A command that is parsed across several reads keeps its pendingCommand
alive between calls, with PENDING_CMD_FLAG_INCOMPLETE set. When a
protocol error is found on one of the later reads, processMultibulkBuffer()
records it in pcmd->read_error and returns early, before the tail of the
function that clears the flag. addPendingCommand() only counts a command
as ready when that flag is clear, so ready_len stays zero and
processInputBuffer() breaks out of the loop before copying read_error to
the client. The error is recorded and then dropped: the client never gets
a reply, and its query buffer keeps growing until it reaches
client-query-buffer-limit.

The same bytes are reported correctly when they arrive in a single read,
because the command is then still freshly acquired and not yet flagged as
incomplete.

This affects every error raised inside the multibulk argument loop: too
big bulk count string, expected '$', invalid bulk length and
unauthenticated bulk length. Of these, too big bulk count string was
unreachable in practice, because a bulk count string above the 64k limit
always spans more than one read.

Route those four error exits through a single err label that clears the
flag on the way out, so the command is queued as ready and the error
reaches the client. The three errors raised while multibulklen is still
zero do not need it: the command is freshly acquired there and the flag
is never set.

Tests are added for the two split-read cases and for the three size
limits, none of which were covered before.
@pirate329
pirate329 force-pushed the fix-protocol-error-after-partial-read branch from 7068517 to 40c2a3d Compare September 23, 2026 06:56
Comment thread src/networking.c Resolved
Comment thread src/networking.c Resolved
The three errors raised while multibulklen is still zero could not hit
the bug on their own, since the pending command is always freshly
acquired there and the flag is never set. Going through err keeps all
the read_error exits consistent and means a new error added there
cannot reintroduce it.

The two returns that only mean "not enough data yet" are left as they
are, since they are not errors.
Comment thread src/networking.c Resolved
@sundb sundb added the release-notes indication that this issue needs to be mentioned in the release notes label Sep 23, 2026
The label only handles read errors, and readerr matches the existing one
in aof.c as well as the other qualified error labels in the tree.
@pirate329

Copy link
Copy Markdown
Contributor Author

@sundb could you approve the workflow run when you get a chance? CI went green on the previous commit and the label rename is the only change since.

@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.60%. Comparing base (669b2a1) to head (35cd707).
⚠️ Report is 15 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable   #15795      +/-   ##
============================================
+ Coverage     76.94%   77.60%   +0.66%     
============================================
  Files           145      146       +1     
  Lines         87695    87999     +304     
============================================
+ Hits          67477    68294     +817     
+ Misses        20218    19705     -513     
Files with missing lines Coverage Δ
src/networking.c 92.59% <100.00%> (+0.21%) ⬆️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sundb sundb changed the title Report protocol errors detected after a partial read Fix protocol error not reported when a command is split across reads Sep 24, 2026
@sundb
sundb merged commit 5418bb1 into redis:unstable Sep 24, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Redis 8.12 Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes indication that this issue needs to be mentioned in the release notes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants