apache/kafka · error · DisconnectException

NetworkClient is no longer active, state is {}

Error message

NetworkClient is no longer active, state is {}

What it means

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.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/NetworkClient.java:795

    public void wakeup() {
        this.selector.wakeup();
    }

    @Override
    public void initiateClose() {
        if (state.compareAndSet(State.ACTIVE, State.CLOSING)) {
            wakeup();
        }
    }

    @Override
    public boolean active() {
        return state.get() == State.ACTIVE;
    }

    private void ensureActive() {
        if (!active())
            throw new DisconnectException("NetworkClient is no longer active, state is " + state);
    }

    /**
     * Close the network client
     */
    @Override
    public void close() {
        state.compareAndSet(State.ACTIVE, State.CLOSING);
        if (state.compareAndSet(State.CLOSING, State.CLOSED)) {
            cancelBootstrapResolution();
            ThreadUtils.shutdownExecutorServiceQuietly(bootstrapExecutor, 1, TimeUnit.SECONDS);
            this.selector.close();
            this.metadataUpdater.close();
            if (telemetrySender != null)
                telemetrySender.close();
        } else {
            log.warn("Attempting to close NetworkClient that has already been closed.");
        }

View on GitHub (pinned to 996fb4585a)

Solutions

  1. Ensure no calls to the client (send, poll, etc.) happen after close() — track lifecycle in the calling code.
  2. If shared across threads, coordinate shutdown with a flag or latch so workers stop before close().
  3. Catch DisconnectException and treat it as a terminal signal to stop the send loop.

Example fix

// before
client.send(request, now); // may throw if already closed

// after
if (!client.active()) {
    throw new IllegalStateException("Client closed; cannot send");
}
client.send(request, now);
Defensive patterns

Strategy: validation

Validate before calling

if (!networkClient.active()) {
    throw new IllegalStateException("NetworkClient is not active; cannot proceed");
}

Try / catch

try {
    networkClient.send(request, now);
} catch (DisconnectException e) {
    if (!networkClient.active()) {
        // terminal: client closed, stop processing
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/kafka@996fb4585a (2026-08-11). Data as JSON: /api/errors/2cf65f9274160b51. Report an issue: GitHub.