{"record":{"id":"2cf65f9274160b51","repo":"apache/kafka","slug":"networkclient-is-no-longer-active-state-is","errorCode":null,"errorMessage":"NetworkClient is no longer active, state is {}","messagePattern":"NetworkClient is no longer active, state is (.+?)","errorType":"exception","errorClass":"DisconnectException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/NetworkClient.java","lineNumber":795,"sourceCode":"    public void wakeup() {\n        this.selector.wakeup();\n    }\n\n    @Override\n    public void initiateClose() {\n        if (state.compareAndSet(State.ACTIVE, State.CLOSING)) {\n            wakeup();\n        }\n    }\n\n    @Override\n    public boolean active() {\n        return state.get() == State.ACTIVE;\n    }\n\n    private void ensureActive() {\n        if (!active())\n            throw new DisconnectException(\"NetworkClient is no longer active, state is \" + state);\n    }\n\n    /**\n     * Close the network client\n     */\n    @Override\n    public void close() {\n        state.compareAndSet(State.ACTIVE, State.CLOSING);\n        if (state.compareAndSet(State.CLOSING, State.CLOSED)) {\n            cancelBootstrapResolution();\n            ThreadUtils.shutdownExecutorServiceQuietly(bootstrapExecutor, 1, TimeUnit.SECONDS);\n            this.selector.close();\n            this.metadataUpdater.close();\n            if (telemetrySender != null)\n                telemetrySender.close();\n        } else {\n            log.warn(\"Attempting to close NetworkClient that has already been closed.\");\n        }","sourceCodeStart":777,"sourceCodeEnd":813,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java#L777-L813","documentation":"Thrown by NetworkClient.ensureActive() when the client's atomic state is not State.ACTIVE — meaning it is CLOSING or CLOSED. This is a DisconnectException indicating the NetworkClient can no longer send or receive because close() has been called or is in progress.","triggerScenarios":"Calling any NetworkClient operation (send, poll, leastLoadedNode) after close() was invoked, or concurrently with a shutdown. The state transitions ACTIVE -> CLOSING -> CLOSED; once it leaves ACTIVE, ensureActive throws.","commonSituations":"Producer/Consumer/Admin client used after close(), sharing a client instance across threads where one thread closes it, or a shutdown hook racing with in-flight requests.","solutions":["Ensure no calls to the client (send, poll, etc.) happen after close() — track lifecycle in the calling code.","If shared across threads, coordinate shutdown with a flag or latch so workers stop before close().","Catch DisconnectException and treat it as a terminal signal to stop the send loop."],"exampleFix":"// before\nclient.send(request, now); // may throw if already closed\n\n// after\nif (!client.active()) {\n    throw new IllegalStateException(\"Client closed; cannot send\");\n}\nclient.send(request, now);","handlingStrategy":"validation","validationCode":"if (!networkClient.active()) {\n    throw new IllegalStateException(\"NetworkClient is not active; cannot proceed\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    networkClient.send(request, now);\n} catch (DisconnectException e) {\n    if (!networkClient.active()) {\n        // terminal: client closed, stop processing\n    }\n}","preventionTips":["Never use a client after close(); track its lifecycle in calling code.","Coordinate multi-threaded access with a shutdown flag/latch.","Treat DisconnectException during shutdown as terminal, not retriable."],"tags":["lifecycle","network","threading"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}