apache/kafka · error · IOException
Connection to was disconnected before the response was read
Error message
Connection to {destination} was disconnected before the response was read What it means
Thrown by NetworkClientUtils.sendAndReceive() when the matching ClientResponse (by correlation ID) has wasDisconnected() == true. This means the connection to the response's destination node was dropped after the request was sent but before the full response was read, so the result is unreliable.
Solutions
- Retry the request on a fresh connection (sendAndReceive is not auto-retried).
- Investigate why the connection dropped: check broker uptime, network stability, and connection idle/timeout settings.
- Increase connection keepalive or reduce idle timeout if a load balancer is prematurely closing connections.
- Use the higher-level KafkaProducer/KafkaConsumer which handle reconnection and retry automatically.
Defensive patterns
Strategy: retry
Try / catch
int attempts = 0;
while (attempts++ < MAX_RETRIES) {
try {
return NetworkClientUtils.sendAndReceive(client, request, time);
} catch (IOException e) {
// disconnected before response: back off and retry on new connection
}
} Prevention
- Prefer high-level Producer/Consumer/Admin that retry automatically.
- If using NetworkClientUtils directly, wrap sendAndReceive with bounded retries.
- Investigate recurring mid-response disconnects as a network/infra issue.
When it happens
Trigger: A request is sent via client.send(); during the subsequent poll loop, the response with the matching correlation ID arrives but is flagged as disconnected — the transport closed mid-response.
Common situations: Broker restarts or crashes mid-request, network interruptions, idle connection timeout closing the socket, load balancer killing idle connections, or the broker closing the connection due to an error it could not encode.
Related errors
- Connection to failed.
- Buffer underflow while parsing response for request with…
- Client was shutdown before response was read
- Failed to create new NetworkClient
- Invalid port in bootstrap.servers
AI-assisted analysis of apache/kafka@996fb4585a (2026-08-11).
Data as JSON: /api/errors/dc78aa239f424f08.
Report an issue: GitHub.
Appendix: source
Thrown at clients/src/main/java/org/apache/kafka/clients/NetworkClientUtils.java:111
/**
* Invokes `client.send` followed by 1 or more `client.poll` invocations until a response is received or a
* disconnection happens (which can happen for a number of reasons including a request timeout).
*
* In case of a disconnection, an `IOException` is thrown.
* If shutdown is initiated on the client during this method, an IOException is thrown.
*
* This method is useful for implementing blocking behaviour on top of the non-blocking `NetworkClient`, use it with
* care.
*/
public static ClientResponse sendAndReceive(KafkaClient client, ClientRequest request, Time time) throws IOException {
try {
client.send(request, time.milliseconds());
while (client.active()) {
List<ClientResponse> responses = client.poll(Long.MAX_VALUE, time.milliseconds());
for (ClientResponse response : responses) {
if (response.requestHeader().correlationId() == request.correlationId()) {
if (response.wasDisconnected()) {
throw new IOException("Connection to " + response.destination() + " was disconnected before the response was read");
}
if (response.versionMismatch() != null) {
throw response.versionMismatch();
}
return response;
}
}
}
throw new IOException("Client was shutdown before response was read");
} catch (DisconnectException e) {
if (client.active())
throw e;
else
throw new IOException("Client was shutdown before response was read");
}
}
View on GitHub (pinned to 996fb4585a)