From 72a4427b79e553767b931cc1fa821e76f60ce1c1 Mon Sep 17 00:00:00 2001 From: yangpei <19790187+Amorth@users.noreply.github.com> Date: Fri, 27 Nov 2026 21:43:45 +0800 Subject: [PATCH] Keep need_rejoin() true while a group join is in progress When a leader rejoins because of a metadata change, performing the assignment refreshes its assignment snapshot and clears the trigger. If SyncGroup was still pending when poll timed out, the next poll saw need_rejoin() == False and returned without awaiting the in-flight join, leaving the consumer in REBALANCING on its old assignment. --- kafka/coordinator/base.py | 4 ++- test/consumer/test_coordinator.py | 51 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/kafka/coordinator/base.py b/kafka/coordinator/base.py index 622323a42..4348d9fd5 100644 --- a/kafka/coordinator/base.py +++ b/kafka/coordinator/base.py @@ -407,7 +407,9 @@ def need_rejoin(self): Returns: bool: True if it should, False otherwise """ - return self.rejoin_needed + # A rebalance that has started stays pending until it completes, + # even if the trigger (e.g. a metadata change) has been handled. + return self.rejoin_needed or self.rejoining def poll_heartbeat(self): """ diff --git a/test/consumer/test_coordinator.py b/test/consumer/test_coordinator.py index 25c3ee4c3..4116daf10 100644 --- a/test/consumer/test_coordinator.py +++ b/test/consumer/test_coordinator.py @@ -1441,6 +1441,57 @@ async def slow_join_handler(api_key, api_version, correlation_id, request_bytes) assert seeded_coord.state == MemberState.STABLE +def test_join_group_async_collects_pending_sync_after_leader_assignment( + request, broker, net, seeded_coord): + """A leader rejoining because of a metadata change refreshes its + assignment snapshot while performing the assignment, so need_rejoin() + no longer reports the metadata change. If SyncGroup is still pending + when the poll times out, the next poll must still await the in-flight + join and complete it instead of returning with the member stuck in + REBALANCING. + """ + request.addfinalizer(lambda: setattr(seeded_coord, 'state', MemberState.UNJOINED)) + broker.set_metadata(topics=[_metadata_topic('foobar', 2)]) + + # Stable leader that assigned 'foobar' while it had a single partition. + _apply_topic_metadata(seeded_coord._cluster, 'foobar', num_partitions=1) + seeded_coord._perform_assignment('member-1', 'range', _single_member('foobar')) + seeded_coord._joined_subscription = {'foobar'} + seeded_coord._generation = Generation(1, 'member-1', 'range') + assert not seeded_coord.need_rejoin() + + # The topic grows: the leader must rejoin although rejoin_needed is False. + _apply_topic_metadata(seeded_coord._cluster, 'foobar', num_partitions=2) + assert seeded_coord.rejoin_needed is False + assert seeded_coord.need_rejoin() + + broker.respond(JoinGroupRequest, _join_response_object( + generation_id=2, leader='member-1', member_id='member-1', + members=_single_member('foobar'))) + sync_response_pending = net.create_future() + + async def slow_sync_handler(api_key, api_version, correlation_id, request_bytes): + await sync_response_pending + return _sync_response_object(assignment=ConsumerProtocolAssignment( + 0, [('foobar', [0, 1])], b'').encode()) + + broker.respond_fn(SyncGroupRequest, slow_sync_handler) + + # First poll: JoinGroup completes and the leader assigns, SyncGroup hangs. + result = seeded_coord._net.run(seeded_coord.join_group_async, 50) + assert result is False + assert seeded_coord.state == MemberState.REBALANCING + + # Second poll after the broker answers SyncGroup: the join completes. + sync_response_pending.success(None) + result = seeded_coord._net.run(seeded_coord.join_group_async, 5000) + assert result is True + assert seeded_coord.state == MemberState.STABLE + assert seeded_coord._generation.generation_id == 2 + assert seeded_coord._subscription.assigned_partitions() == { + TopicPartition('foobar', 0), TopicPartition('foobar', 1)} + + @pytest.mark.parametrize("broker", [(0, 8, 0)], indirect=True) def test_join_group_async_unsupported_version(broker, coordinator): with pytest.raises(Errors.UnsupportedVersionError):