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

  1. Retry the request on a fresh connection (sendAndReceive is not auto-retried).
  2. Investigate why the connection dropped: check broker uptime, network stability, and connection idle/timeout settings.
  3. Increase connection keepalive or reduce idle timeout if a load balancer is prematurely closing connections.
  4. 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

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


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)