{"record":{"id":"dc78aa239f424f08","repo":"apache/kafka","slug":"connection-to-destination-was-disconnected-befor","errorCode":null,"errorMessage":"Connection to {destination} was disconnected before the response was read","messagePattern":"Connection to (.+?) was disconnected before the response was read","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/NetworkClientUtils.java","lineNumber":111,"sourceCode":"    /**\n     * Invokes `client.send` followed by 1 or more `client.poll` invocations until a response is received or a\n     * disconnection happens (which can happen for a number of reasons including a request timeout).\n     *\n     * In case of a disconnection, an `IOException` is thrown.\n     * If shutdown is initiated on the client during this method, an IOException is thrown.\n     *\n     * This method is useful for implementing blocking behaviour on top of the non-blocking `NetworkClient`, use it with\n     * care.\n     */\n    public static ClientResponse sendAndReceive(KafkaClient client, ClientRequest request, Time time) throws IOException {\n        try {\n            client.send(request, time.milliseconds());\n            while (client.active()) {\n                List<ClientResponse> responses = client.poll(Long.MAX_VALUE, time.milliseconds());\n                for (ClientResponse response : responses) {\n                    if (response.requestHeader().correlationId() == request.correlationId()) {\n                        if (response.wasDisconnected()) {\n                            throw new IOException(\"Connection to \" + response.destination() + \" was disconnected before the response was read\");\n                        }\n                        if (response.versionMismatch() != null) {\n                            throw response.versionMismatch();\n                        }\n                        return response;\n                    }\n                }\n            }\n            throw new IOException(\"Client was shutdown before response was read\");\n        } catch (DisconnectException e) {\n            if (client.active())\n                throw e;\n            else\n                throw new IOException(\"Client was shutdown before response was read\");\n\n        }\n    }\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/NetworkClientUtils.java#L93-L129","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"int attempts = 0;\nwhile (attempts++ < MAX_RETRIES) {\n    try {\n        return NetworkClientUtils.sendAndReceive(client, request, time);\n    } catch (IOException e) {\n        // disconnected before response: back off and retry on new connection\n    }\n}","preventionTips":["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."],"tags":["network","connection","retry"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}