richdougherty opened a new pull request, #24297: URL: https://github.com/apache/kafka/pull/24297
`KafkaConsumer.close()` can block indefinitely with the classic group protocol. The hang seems to occur under some circumstances when an application calls `close()` on a consumer during a network partition. Specifically, `close()` blocks indefinitely when all of these hold: 1. The application calls `close()` while the consumer's JoinGroup request is in flight. 2. The JoinGroup response elects this consumer group leader. 3. Its metadata needs an update (e.g. another member subscribes to a topic this consumer doesn't). 4. No broker can answer the Metadata request (a network partition, say). The mechanism is that the `close()` call initiates a `poll()` and close's poll then reads the JoinGroup response, handling it there on the closing thread (`onLeaderElected` → `updateGroupSubscription`). Then we can wait indefinitely here if metadata is due for an update, without a deadline: `client.ensureFreshMetadata(time.timer(Long.MAX_VALUE))`. With a network partition, this won't arrive, and we block the close operation indefinitely. There seem to be two polls inside close that can read the JoinGroup response. With the default assignors (eager), it's the poll in `maybeLeaveGroup()`. With a cooperative assignor and auto-commit on, it's one poll earlier, in the auto-commit's `commitOffsetsSync()`. The fix given in this PR is for the close operation to store a timer which can then be used if waiting for metadata, preventing an unbounded wait. So if the close timeout happens, `updateGroupSubscription` would throw `TimeoutException`, the rejoin would fail, and close would continue. This seems OK because the member is leaving the group anyway. Other than when closing, things stay the same. Tested via reproducing on a network-partitioned cluster before and after fix. For unit testing, added `testCloseDoesNotBlockOnMetadataWhenElectedLeaderWithAutoCommit` to `ConsumerCoordinatorTest`. This test fails before the fix and passes after. I am happy to license this work to the project under the project's open source license. Co-Authored-By: Claude Opus 5.5 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
