Skip to content

Keep need_rejoin() true while a group join is in progress - #3210

Open
Amorth wants to merge 1 commit into
dpkp:masterfrom
Amorth:fix-pending-sync-after-leader-assignment
Open

Amorth wants to merge 1 commit into
dpkp:masterfrom
Amorth:fix-pending-sync-after-leader-assignment

Conversation

@Amorth

@Amorth Amorth commented Nov 27, 2026

Copy link
Copy Markdown

What goes wrong

A group leader that rejoins because topic metadata changed (e.g. a subscribed topic gained partitions) can get stuck in REBALANCING on its old assignment when the rejoin spans more than one poll():

  1. Group is stable, rejoin_needed is False. The partition count changes, so ConsumerCoordinator.need_rejoin() returns True through _assignment_snapshot != _metadata_snapshot.
  2. poll(timeout_ms=small) starts the join. JoinGroup returns, the leader runs _perform_assignment(), which sets _assignment_snapshot = _metadata_snapshot. SyncGroup is still pending when the poll timer expires, so join_group_async() returns False and leaves _join_task in flight (as intended).
  3. Next poll: none of the triggers hold any more (snapshots equal, subscription unchanged, rejoin_needed still False), so need_rejoin() is False. ConsumerCoordinator.poll() skips ensure_active_group() and join_group_async() exits its while self.need_rejoin() loop immediately. The finished SyncGroup is never collected: _on_join_complete does not run, the state stays REBALANCING, heartbeats stay disabled and the new partitions are never assigned.

Why

need_rejoin() only reports why a rebalance is needed, not that one is already underway. The Java client avoids this with rejoinNeededOrPending() (rejoinNeeded || joinFuture != null).

Change

BaseCoordinator.need_rejoin() returns self.rejoin_needed or self.rejoining. rejoining is set when a rebalance attempt starts (after _on_join_prepare) and cleared when it completes, so it is exactly the "pending" half of rejoinNeededOrPending().

Test

test_join_group_async_collects_pending_sync_after_leader_assignment in test/consumer/test_coordinator.py uses MockBroker: a stable leader sees its topic grow from 1 to 2 partitions, JoinGroup answers immediately, SyncGroup is held on a future, the first join_group_async(50) returns False, then SyncGroup is released and the second call must complete the join. Without the change the second call returns True with the state still REBALANCING:

E       AssertionError: assert '<rebalancing>' == '<stable>'

With the change it passes (20/20 repeated runs). pytest test --ignore=test/integration: 2730 passed, 1 failed (test/record/test_util.py::test_crc32c[None], optional crc32c package not installed locally, unrelated). pylint --errors-only on the touched files is clean.

A related problem in the same function (a join task that completes between polls is discarded) is addressed separately in another PR.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant