Alex Kahn created KAFKA-21676:
---------------------------------
Summary: Broker never responds to Produce request with null
records, stalling the connection (regression in 3.8.0)
Key: KAFKA-21676
URL: https://issues.apache.org/jira/browse/KAFKA-21676
Project: Kafka
Issue Type: Bug
Components: core
Affects Versions: 3.8.0
Environment: apache/kafka Docker images, single-node KRaft
Reporter: Alex Kahn
*Reproduction repository:* [https://github.com/akahn/kafka-null-records-repro]
h2. Summary
The {{records}} field of {{PartitionProduceData}} in the Produce request schema
is nullable. When a client sends a Produce request in which a partition has
{{records=null}}, brokers 3.8.0 and later throw a {{NullPointerException}} and
*never send a response*. The broker handles one request at a time per
connection, so every later request on the same connection also stays
unanswered. The connection stalls until the client's request timeout.
Brokers 3.7.x return {{UNKNOWN_SERVER_ERROR}} for the partition and the
connection stays usable.
Real clients send this. For example, franz-go sends {{records=null}} for a
partition whose batch failed after the request was built
([twmb/franz-go#1599|https://github.com/twmb/franz-go/issues/1599]).
h2. Affected versions
Tested with {{apache/kafka}} images, single-node KRaft, with the highest
Produce version each broker supports (v10 to v13). Produce v3 and v9 against
4.4.0 give the same result.
||Version||Result||
|3.7.0, 3.7.2|Partition error {{UNKNOWN_SERVER_ERROR}}, connection usable|
|3.8.0, 3.9.0, 4.0.0, 4.1.0, 4.2.0, 4.3.0, 4.4.0|*No response, connection
stalled*|
h2. Reproduce
Requires Docker.
{noformat}
git clone https://github.com/akahn/kafka-null-records-repro
cd kafka-null-records-repro
./repro.sh 3.7.2 3.8.0 # default; pass any apache/kafka tags (3.7.0+)
{noformat}
For each version, {{repro.sh}} starts a fresh broker ({{docker-compose.yml}},
{{KAFKA_VERSION}} selects the image tag), creates a topic, and runs
{{main.go}}. {{main.go}} uses a raw TCP connection and uses franz-go's {{kmsg}}
package only to encode and decode messages. On one connection it sends:
# {{ApiVersions}} and {{Metadata}}.
# Control: {{Produce}} with a valid one-record batch, {{acks=1}}. The broker
answers.
# {{Produce}} with partition 0 {{records=null}}, then {{ApiVersions}}
(pipelined).
The exit code is 1 if the broker does not answer step 3 within 10 seconds.
You can also run the client against an existing broker: {{go run . -broker
host:9092 -topic <topic>}}.
h3. 3.7.2 output
{noformat}
-> Produce v10 partition 0: records=null
-> ApiVersions v0
<- Produce v10 partition 0: error=UNKNOWN_SERVER_ERROR: The server experienced
an unexpected error when processing the request.
<- ApiVersions v0 error=NONE
RESULT: NOT AFFECTED. The broker answered the records=null Produce request.
{noformat}
h3. 3.8.0 output
{noformat}
-> Produce v10 partition 0: records=null
-> ApiVersions v0
<- Produce v10: NO RESPONSE after 10s
RESULT: AFFECTED. The broker did not answer the records=null Produce request,
and the connection is stalled.
{noformat}
Broker log (3.8.0):
{noformat}
ERROR [Kafka Request Handler 3 on Broker 1], Exception when handling request
(kafka.server.KafkaRequestHandler)
java.lang.NullPointerException: Cannot invoke
"org.apache.kafka.common.record.BaseRecords.sizeInBytes()" because the return
value of
"org.apache.kafka.common.message.ProduceRequestData$PartitionProduceData.records()"
is null
at
org.apache.kafka.common.requests.ProduceRequest.lambda$null$0(ProduceRequest.java:141)
at java.base/java.util.HashMap.compute(Unknown Source)
at
org.apache.kafka.common.requests.ProduceRequest.lambda$null$1(ProduceRequest.java:139)
at java.base/java.util.ArrayList.forEach(Unknown Source)
at
org.apache.kafka.common.requests.ProduceRequest.lambda$partitionSizes$2(ProduceRequest.java:138)
at java.base/java.lang.Iterable.forEach(Unknown Source)
at
org.apache.kafka.common.requests.ProduceRequest.partitionSizes(ProduceRequest.java:137)
at
org.apache.kafka.common.requests.ProduceRequest.toString(ProduceRequest.java:171)
at
kafka.network.RequestChannel$Request.requestDesc(RequestChannel.scala:170)
at kafka.server.KafkaApis.$anonfun$handle$1(KafkaApis.scala:173)
at kafka.utils.Logging.error(Logging.scala:76)
at kafka.utils.Logging.error$(Logging.scala:76)
at kafka.server.KafkaApis.error(KafkaApis.scala:94)
at kafka.server.KafkaApis.handleError$1(KafkaApis.scala:174)
at kafka.server.KafkaApis.handle(KafkaApis.scala:263)
at kafka.server.KafkaRequestHandler.run(KafkaRequestHandler.scala:160)
at java.base/java.lang.Thread.run(Unknown Source)
{noformat}
h2. Cause
Two NPEs on null {{records}}, one after the other:
# {{ReplicaManager.handleProduceAppend}} calls {{records.batches}} on every
partition to find transactional batches. Commit
[5eb82010ef|https://github.com/apache/kafka/commit/5eb82010ef] (KAFKA-15987,
"Refactor ReplicaManager code for transaction verification", first released in
3.8.0) removed the per-partition {{try}}/{{catch}} that turned this exception
into a partition error. Now the NPE goes up to {{KafkaApis.handle}}.
# The catch block in {{KafkaApis.handle}} calls {{handleError}}, which logs
{{request.requestDesc(true)}}. For a Produce request that calls
{{ProduceRequest.toString}} → {{partitionSizes}}, which calls
{{records().sizeInBytes()}} and throws a second NPE. That exception escapes
{{handleError}} before {{requestHelper.handleError}} sends an error response.
{{KafkaRequestHandler}} only logs it. No response is sent, so the connection
stays muted.
The first NPE is never logged, because the second one replaces it.
In 3.7.x the first dereference of null {{records}} was in
{{UnifiedLog.analyzeAndValidateRecords}}, inside the per-partition error
handling of {{ReplicaManager.appendToLocalLog}}, so the broker answered
{{UNKNOWN_SERVER_ERROR}}. 3.7.x also logs the {{ProduceRequest.toString}} NPE
from {{KafkaApis.handleError}}, but only after the partition error response was
sent. The {{toString}} NPE is therefore older than 3.8.0. 3.8.0 added a path
that reaches it before any response is sent.
h2. Expected behavior
The broker answers with a per-partition error for the partition with null
{{records}} (for example {{CORRUPT_MESSAGE}} or {{INVALID_RECORD}}; 3.7.x
returned {{UNKNOWN_SERVER_ERROR}}). The connection stays usable.
Possible fixes, which do not exclude each other:
* Reject null {{records}} per partition, before {{handleProduceAppend}} or
inside it.
* Make {{ProduceRequest.partitionSizes}} handle null {{records}}, so error
logging cannot throw.
* Make {{KafkaApis.handle}}'s {{handleError}} send the error response even if
building the log message throws.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)