apache/kafka · warning · InterruptException

Interrupted waiting for results from fetching records

Error message

Interrupted waiting for results from fetching records

What it means

InterruptException thrown inside FetchBuffer.awaitWakeup when the consumer thread's interrupted flag is found set at the moment the timer expired (before entering the await). Per the KafkaConsumer.poll(Duration) contract, an interrupt observed even when not blocking counts as an interruption of the fetch wait. No cause exception is attached because Thread.interrupted() only returns a boolean.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchBuffer.java:179

     * </ol>
     *
     * @param timer Timer that provides time to wait
     */
    void awaitWakeup(Timer timer) {
        try {
            lock.lock();

            while (!wokenup.compareAndSet(true, false)) {
                // Update the timer before we head into the loop in case it took a while to get the lock.
                timer.update();

                if (timer.isExpired()) {
                    // If the thread was interrupted before we start waiting, it still counts as
                    // interrupted from the point of view of the KafkaConsumer.poll(Duration) contract.
                    // We only need to check this when we are not going to wait because waiting
                    // already checks whether the thread is interrupted.
                    if (Thread.interrupted())
                        throw new InterruptException("Interrupted waiting for results from fetching records");

                    break;
                }

                if (!blockingCondition.await(timer.remainingMs(), TimeUnit.MILLISECONDS)) {
                    break;
                }
            }
        } catch (InterruptedException e) {
            throw new InterruptException("Interrupted waiting for results from fetching records", e);
        } finally {
            lock.unlock();
            timer.update();
        }
    }

    void wakeup() {
        try {

View on GitHub (pinned to c31c9215e1)

Solutions

  1. Prefer KafkaConsumer.wakeup() for cooperative shutdown; it sets the wokenup flag and avoids the interrupt path.
  2. If interrupts are expected, catch InterruptException around poll() and restore the interrupt flag before exiting.
  3. Avoid very short poll(Duration) values that cause the timer to expire before the await check.

Example fix

// before
consumerThread.interrupt();

// after
try {
  while (running) consumer.poll(Duration.ofMillis(500));
} catch (InterruptException | WakeupException e) {
  Thread.currentThread().interrupt();
} finally { consumer.close(); }
Defensive patterns

Strategy: try-catch

Validate before calling

// Same call path as [171]: cannot pre-validate an interrupt. But you can avoid the
// race where the thread was interrupted BEFORE entering awaitWakeup by clearing the
// flag intentionally only at a known checkpoint:
if (Thread.currentThread().isInterrupted() && shutdownRequested) {
    // exit cleanly instead of letting FetchBuffer.awaitWakeup throw at line 179
    return;
}

Try / catch

try {
    consumer.poll(Duration.ofMillis(Long.MAX_VALUE)); // long poll, hits awaitWakeup
} catch (org.apache.kafka.common.errors.InterruptException e) {
    // Thrown at FetchBuffer.java:179 when Thread.interrupted() is true on entry to
    // the wait path (timer already expired). It is a shutdown signal.
    Thread.currentThread().interrupt();
    log.debug("Consumer poll interrupted on entry; shutting down");
    return;
}

Prevention

When it happens

Trigger: Thread.interrupt() called on the consumer thread and the awaitWakeup timer is already expired when the check at FetchBuffer.java:178 runs (so the code takes the expiry branch rather than the await branch).

Common situations: Application shuts down the consumer thread via interrupt while poll()'s remaining time has already elapsed; executor shutdownNow() racing with a poll nearing its timeout; wakeup() not used and interrupt lands at the boundary of the timer.

Related errors


AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03). Data as JSON: /data/errors/3b052a4b6fa53733.json. Report an issue: GitHub.