Skip to content

Fix coordinator join_future race condition #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions kafka/coordinator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,23 @@ def ensure_active_group(self):
# before the pending rebalance has completed.
if self.join_future is None:
self.state = MemberState.REBALANCING
self.join_future = self._send_join_group_request()
future = self._send_join_group_request()

self.join_future = future # this should happen before adding callbacks

# handle join completion in the callback so that the
# callback will be invoked even if the consumer is woken up
# before finishing the rebalance
self.join_future.add_callback(self._handle_join_success)
future.add_callback(self._handle_join_success)

# we handle failures below after the request finishes.
# If the join completes after having been woken up, the
# exception is ignored and we will rejoin
self.join_future.add_errback(self._handle_join_failure)
future.add_errback(self._handle_join_failure)

else:
future = self.join_future

future = self.join_future
self._client.poll(future=future)

if future.failed():
Expand Down
13 changes: 13 additions & 0 deletions test/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,16 @@ def test_lookup_coordinator_failure(mocker, coordinator):
return_value=Future().failure(Exception('foobar')))
future = coordinator.lookup_coordinator()
assert future.failed()


def test_ensure_active_group(mocker, coordinator):
coordinator._subscription.subscribe(topics=['foobar'])
mocker.patch.object(coordinator, 'coordinator_unknown', return_value=False)
mocker.patch.object(coordinator, '_send_join_group_request', return_value=Future().success(True))
mocker.patch.object(coordinator, 'need_rejoin', side_effect=[True, True, False])
mocker.patch.object(coordinator, '_on_join_complete')
mocker.patch.object(coordinator, '_heartbeat_thread')

coordinator.ensure_active_group()

coordinator._send_join_group_request.assert_called_once_with()