Skip to content

Manual Offset Commit/Heartbeat Deadlock #2413

Closed
@mro-rhansen2

Description

@mro-rhansen2

I believe that I have found the reason for the deadlock that has been alluded to in a few other issues on the board.

#2373
#2099
#2042
#1989

The offset commit appears to be blocked by-design with the assumption that the operation should resume without issue once the underlying network problem has been resolved. The issue appears to be that the consumer is not holding onto an exclusive client lock while it is waiting. This leads to a race condition between the main thread and the heartbeat thread due to a failure to maintain lock ordering.

The order of operations is as follows:

  1. Consumer handles a message.
  2. Network error occurs.
  3. Consumer tries to commit offset. Commit blocks in an infinite loop and releases the KafkaClient lock on each attempt:
    self.ensure_coordinator_ready()
  4. While trying to identify a new coordinator, BaseCoordinator takes the KafkaClient lock and then the internal coordinator lock:
    with self._client._lock, self._lock:
  5. HeartbeatThread thread runs and only takes the coordinator lock:
    with self.coordinator._lock:
  6. HeartbeatThread detects consumer timeout and tries to shutdown the coordinator, taking the client lock only after having already taken the coordinator lock in the step above (the inverse order in how the locks are taken by the main thread during the block commit operation):
    self.coordinator.maybe_leave_group()
    with self._client._lock, self._lock:

It is admittedly a very tight window for a race condition but it does exist based on my own experience as well as that of others in the community. The problem can be avoided by allowing the consumer exclusive access to the KafkaClient while trying to commit the offset, or by ensuring that the heartbeat thread has exclusivity to the client while it is checking things out.

It should also be noted that, while I have only spelled out the race condition as it exists between the commit and heartbeat operations, I wouldn't be surprised if the heartbeat was also interfering with other operations because of this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions