Skip to content

Collect a group join that completed between polls - #3211

Open
Amorth wants to merge 2 commits into
dpkp:masterfrom
Amorth:fix-join-completed-between-polls
Open

Amorth wants to merge 2 commits into
dpkp:masterfrom
Amorth:fix-join-completed-between-polls

Conversation

@Amorth

@Amorth Amorth commented Nov 27, 2026

Copy link
Copy Markdown

What goes wrong

join_group_async() caches the in-flight JoinGroup -> SyncGroup task in _join_task so that a short poll(timeout_ms=N) doesn't send a duplicate JoinGroup. But the reuse check is

if self._join_task is None or self._join_task.is_done:
    ...
    self._join_task = self._manager.call_soon(self._do_join_and_sync_async)

If the poll timer expires while SyncGroup is pending and the SyncGroup response arrives before the next poll (the task finishes in the background), the next poll sees is_done and starts a brand-new JoinGroup, discarding the successful result. That costs the whole group an extra rebalance round, which is the duplicate JoinGroup the cache was added to prevent.

Change

Replace the cached task only when it has failed (self._join_task.failed()). A task that succeeded is awaited again, which returns its result immediately. A failed task is still replaced as before. This includes a task that failed with KafkaTimeoutError on a request timeout, which the except Errors.KafkaTimeoutError branch can't tell apart from the poll timer expiring.

Tests (in test/consumer/test_coordinator.py, using MockBroker)

  • test_join_group_async_uses_join_completed_between_polls: SyncGroup is held so the first join_group_async(50) returns False. SyncGroup is then released and the cached task runs to completion before the second call, which must complete the join without another JoinGroup. Without the change a second JoinGroup is sent; nothing is scripted to answer it, so the call fails. With the change the test passes.
  • test_join_group_async_retries_after_join_task_timeout: the first join attempt fails with KafkaTimeoutError, and the next call must start a fresh JoinGroup and succeed. This guards the "reuse only if not failed" part: if the cached task were reused whenever it is set, every later poll would re-await the failed task and return False forever. It passes before and after this PR.

Both pass in 20/20 repeated runs. pytest test --ignore=test/integration: 2732 passed, 1 failed (test/record/test_util.py::test_crc32c[None]; the optional crc32c package isn't installed locally, unrelated). pylint --errors-only on the touched files is clean.

This branch is stacked on #3210 (the need_rejoin() fix), so it also contains that commit until #3210 is merged; only the second commit is new here.

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.
join_group_async() replaced a cached join task with a new JoinGroup
whenever the task was done, discarding a successful SyncGroup result
that arrived after the previous poll timed out. Reuse a task that
succeeded; only replace one that failed.
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