[ 
https://issues.apache.org/jira/browse/KAFKA-21595?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148267#comment-18148267
 ] 

Lianet Magrans commented on KAFKA-21595:
----------------------------------------

Hey [~abyss] , thanks for reporting this. As of 4.4 there are some validations 
to protect against this in the case the event is enqueued when the background 
thread is dead already (so should make this gap even harder to hit, needs not 
only the background to die, but to die after the event has been added to the 
queue).

I linked the Jira that added the guards to events added when the background 
thread is dead fail fast.

All that being said, I agree that we could still hit the hang you describe, if 
an event is added to the queue and then the background thread dies. Some 
thoughts for when looking into fixing here:
 * I expect the hang in that edge case would affect other APIs (not only 
close). All Apis that rely on the background thread reaping/expiring the events
 * we should me mindful about any change we add to fix this edge case (e.g., 
careful not to break the expiration logic driven by the background, or to add 
overhead on the inter-thread communication that we've seen it can easily affect 
CPU)

Happy to help with reviews if you or someone else takes on this. Thanks!

> 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.3.1, 4.4.0
>         Environment: OpenJDK 25.0.4.1, async KafkaConsumer (new consumer 
> threading model)
>            Reporter: Petras
>            Priority: Major
>
> {{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}
>   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)

Reply via email to