Ensure timeout is checked after each fetch position update#2668
Merged
Conversation
bpedersen2
reviewed
Jul 31, 2025
| if self._update_fetch_positions(timeout_ms=timer.timeout_ms): | ||
| position = self._subscription.assignment[partition].position | ||
| elif timer.expired: | ||
| if timer.expired: |
There was a problem hiding this comment.
if position is None and time.expired ?
Owner
Do you have any more details on this case? The internal contract is/should be that |
Contributor
Author
|
Yes, |
Contributor
Author
|
Here is a minimal example. FROM apache/kafka:4.0.0
COPY server.properties /kafka/config/kraft/
EXPOSE 9092with build and run it: docker build -t kafka .
docker run --rm --name kafka -p 9092:9092 -it kafkaAnd python code import time
from kafka import KafkaConsumer, KafkaProducer, TopicPartition
prod = KafkaProducer(bootstrap_servers='localhost:9092')
cons = KafkaConsumer(
bootstrap_servers='localhost:9092',
auto_offset_reset='earliest',
session_timeout_ms=1000,
)
partitions = cons.partitions_for_topic('test')
cons.assign([TopicPartition('test', p) for p in partitions])
end = cons.end_offsets(list(cons.assignment()))
for partition in cons.assignment():
print(1)
while (pos := cons.position(partition, timeout_ms=1000)) \
is not None and pos < end[partition]:
print(2)
prod.send(
topic='test',
value=b'0000',
key=b'1111',
timestamp_ms=int(time.time() * 1000))
prod.flush()First run finishes normally, on 2nd run it hangs on |
bpedersen2
pushed a commit
to mlz-ictrl/nicos
that referenced
this pull request
Aug 1, 2025
Since kafka-python 2.2.0 the api has changed. In KafkaConsumer.position method a while loop was added, so to avoid eternal loop a `timeout_ms` parameter can be passed. I pass 1000 ms timeout, for it is long enough for our back-end to respond and smaller than our 10 s cache response timeout. The timeout parameter was not supported in versions before 2.1.0. However, this fix works until kafka-python 2.2.9, where I believe there is a bug, which will be hopefully resolved soon dpkp/kafka-python#2668 Until then, kafka-python version should be fixed. Change-Id: I45d95e03107904e60412996f64253429dcf7fde4 Reviewed-on: https://forge.frm2.tum.de/review/c/frm2/nicos/nicos/+/36950 Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de> Reviewed-by: Bjoern Pedersen <bjoern.pedersen@frm2.tum.de>
Owner
|
Ok, thanks! |
dpkp
pushed a commit
that referenced
this pull request
Nov 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For
Consumer.position()I had a case whenself._update_fetch_positionsalways resulted inTrueandpositionwas alwaysNone. This hangs the loop for ~305 seconds, and it appears to me that the lineis never reached.
The root cause is probably how
timeout_msvalue is propagated and checked in the following methods, however I believe my change is still valid as the timeout check should work disregarding ofself._update_fetch_positionsvalue.