{"record":{"id":"0ddc90b085b2b1bd","repo":"apache/kafka","slug":"client-was-shutdown-before-response-was-read","errorCode":null,"errorMessage":"Client was shutdown before response was read","messagePattern":"Client was shutdown before response was read","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"critical","filePath":"clients/src/main/java/org/apache/kafka/clients/NetworkClientUtils.java","lineNumber":120,"sourceCode":"     */\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\n    /**\n     * Check if the code is disconnected and unavailable for immediate reconnection (i.e. if it is in\n     * reconnect backoff window following the disconnect).\n     */\n    public static boolean isUnavailable(KafkaClient client, Node node, Time time) {\n        return client.connectionFailed(node) && client.connectionDelay(node, time.milliseconds()) > 0;\n    }\n\n    /**","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/NetworkClientUtils.java#L102-L138","documentation":"Thrown by NetworkClientUtils.sendAndReceive() when the poll loop exits because client.active() returns false — the NetworkClient was shut down (state left ACTIVE) before the response for the request could be read. This is a terminal failure: the client is no longer usable.","triggerScenarios":"After sending a request, the while(client.active()) loop terminates because the client was closed (or is closing). The response never arrived before shutdown completed.","commonSituations":"Calling close() on the client from another thread while sendAndReceive is blocking, a JVM shutdown hook closing clients during in-flight requests, or application logic that closes the client before draining pending operations.","solutions":["Ensure the client is not closed while sendAndReceive is in progress — coordinate lifecycle with a flag or latch.","Register a shutdown hook that waits for in-flight operations to complete before closing.","Avoid sharing a single NetworkClient across components with independent close behavior."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (!client.active()) {\n    throw new IllegalStateException(\"Client inactive; cannot sendAndReceive\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    NetworkClientUtils.sendAndReceive(client, request, time);\n} catch (IOException e) {\n    if (!client.active()) {\n        // shutdown race: do not retry, propagate\n    }\n}","preventionTips":["Do not close the client while sendAndReceive is blocking.","Use a shutdown coordinator that drains in-flight calls first.","Register shutdown hooks that await outstanding operations."],"tags":["lifecycle","network","threading"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}