Skip to content

Fix #1985: fix consumer deadlock when heartbeat thread request timeout #2064

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 1 commit into from
Sep 7, 2020
Merged
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
20 changes: 12 additions & 8 deletions kafka/coordinator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def ensure_coordinator_ready(self):
"""Block until the coordinator for this group is known
(and we have an active connection -- java client uses unsent queue).
"""
with self._lock:
with self._client._lock, self._lock:
while self.coordinator_unknown():

# Prior to 0.8.2 there was no group coordinator
Expand Down Expand Up @@ -346,7 +346,7 @@ def _handle_join_failure(self, _):

def ensure_active_group(self):
"""Ensure that the group is active (i.e. joined and synced)"""
with self._lock:
with self._client._lock, self._lock:
if self._heartbeat_thread is None:
self._start_heartbeat_thread()

Expand Down Expand Up @@ -764,7 +764,7 @@ def close(self):

def maybe_leave_group(self):
"""Leave the current group and reset local generation/memberId."""
with self._lock:
with self._client._lock, self._lock:
if (not self.coordinator_unknown()
and self.state is not MemberState.UNJOINED
and self._generation is not Generation.NO_GENERATION):
Expand Down Expand Up @@ -947,6 +947,15 @@ def run(self):
log.debug('Heartbeat thread closed')

def _run_once(self):
with self.coordinator._client._lock, self.coordinator._lock:
if self.enabled and self.coordinator.state is MemberState.STABLE:
# TODO: When consumer.wakeup() is implemented, we need to
# disable here to prevent propagating an exception to this
# heartbeat thread
# must get client._lock, or maybe deadlock at heartbeat
# failure callbak in consumer poll
self.coordinator._client.poll(timeout_ms=0)

with self.coordinator._lock:
if not self.enabled:
log.debug('Heartbeat disabled. Waiting')
Expand All @@ -962,11 +971,6 @@ def _run_once(self):
self.disable()
return

# TODO: When consumer.wakeup() is implemented, we need to
# disable here to prevent propagating an exception to this
# heartbeat thread
self.coordinator._client.poll(timeout_ms=0)

if self.coordinator.coordinator_unknown():
future = self.coordinator.lookup_coordinator()
if not future.is_done or future.failed():
Expand Down