Petras created KAFKA-21595:
------------------------------
Summary: AsyncKafkaConsumer.close() hangs indefinitely in
leaveGroupOnClose when the background thread dies (e.g. OutOfMemoryError)
without completing cleanup()
Key: KAFKA-21595
URL: https://issues.apache.org/jira/browse/KAFKA-21595
Project: Kafka
Issue Type: Bug
Components: cilents, consumer
Affects Versions: 4.4.0, 4.3.1
Environment: OpenJDK 25.0.4.1, async KafkaConsumer (new consumer
threading model)
Reporter: Petras
{{AsyncKafkaConsumer.close()}} can block forever if the background
{{consumer_background_thread}} ({{ConsumerNetworkThread}}) terminates
abnormally.
Here are detailed analysis with the help of Kiro.
Observed sequence:
1. Application calls {{consumer.close(...)}}.
2. {{close()}} → {{leaveGroupOnClose()}} enqueues a
{{LeaveGroupOnCloseEvent}} via {{ApplicationEventHandler.addAndGet(...)}}.
3. {{addAndGet()}} blocks in {{ConsumerUtils.getResult(Future)}}, which is a
bare future.get() with no timeout.
4. The {{ConsumerNetworkThread}} dies from an {{OutOfMemoryError}} that
reaches its {{UncaughtExceptionHandler}}. Because of the OOM,
{{ConsumerNetworkThread.cleanup()}} does not complete, so
{{CompletableEventReaper.reap(applicationEventQueue)}} — the close-time safety
net that completes all pending event futures exceptionally — never runs.
5. The {{LeaveGroupOnCloseEvent}} future is never completed (neither by
success nor by the deadline-based {{TimeoutException}}), so the application
thread parks in {{future.get()}} indefinitely.
The event does carry a deadline, but deadline enforcement lives entirely on
the background thread ({{CompletableEventReaper}}). When that thread is gone,
nothing enforces it, and the no-timeout {{getResult(Future)}} overload used by
{{addAndGet()}} has no independent bound.
Relevant code (4.3.1, 4.4.0):
- {{ConsumerUtils.getResult(Future)}} — return {{future.get()}}; (no timeout).
- {{ApplicationEventHandler.addAndGet()}} — calls the no-timeout {{getResult.
The {{ensureNetworkThreadAlive()}} check in {{add()}} is pre-enqueue only and
documented as racy; it does not help a caller already parked in
{{future.get()}}.
- {{ConsumerNetworkThread.run()}} — relies on finally {{ { cleanup(); } →
applicationEventReaper.reap(...)}} to fail pending events, which does not
survive an {{OutOfMemoryError}}.
- {{AsyncKafkaConsumer.leaveGroupOnClose() / close()}}.
Thread dump (blocked application thread):
{code}
java.lang.Thread.State: WAITING (parking)
at
java.util.concurrent.CompletableFuture.waitingGet(CompletableFuture.java:1919)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2093)
at
org.apache.kafka.clients.consumer.internals.ConsumerUtils.getResult(ConsumerUtils.java:240)
at
org.apache.kafka.clients.consumer.internals.events.ApplicationEventHandler.addAndGet(ApplicationEventHandler.java:144)
at
org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer.leaveGroupOnClose(AsyncKafkaConsumer.java:1739)
at
org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer.lambda$close$2(AsyncKafkaConsumer.java:1648)
at org.apache.kafka.common.utils.Utils.swallow(Utils.java:1046)
at
org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer.close(AsyncKafkaConsumer.java:1647)
at
org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer.close(AsyncKafkaConsumer.java:1558)
at
org.apache.kafka.clients.consumer.KafkaConsumer.close(KafkaConsumer.java:1867)
{code}
(The {{consumer_background_thread}} is absent from the dump — it was
terminated by the OOM.)
*Expected behavior*: {{close()}} honors the close timeout even if the
background thread has died. {{addAndGet()}} for a
{{CompletableApplicationEvent}} should bound its wait by the event's deadline
(and/or fail fast if the background thread is no longer alive) and throw
{{TimeoutException/KafkaException}}
rather than block forever.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)