Skip to content

Commit f2d10f0

Browse files
committed
Fix concurrency bug in RecordAccumulator.ready()
1 parent 2dd216b commit f2d10f0

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

kafka/producer/record_accumulator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,11 @@ def ready(self, cluster):
320320
now = time.time()
321321

322322
exhausted = bool(self._free.queued() > 0)
323-
for tp, dq in six.iteritems(self._batches):
324-
323+
# several threads are accessing self._batches -- to simplify
324+
# concurrent access, we iterate over a snapshot of partitions
325+
# and lock each partition separately as needed
326+
partitions = list(self._batches.keys())
327+
for tp in partitions:
325328
leader = cluster.leader_for_partition(tp)
326329
if leader is None or leader == -1:
327330
unknown_leaders_exist = True
@@ -330,6 +333,7 @@ def ready(self, cluster):
330333
continue
331334

332335
with self._tp_locks[tp]:
336+
dq = self._batches[tp]
333337
if not dq:
334338
continue
335339
batch = dq[0]

0 commit comments

Comments
 (0)