kafka-python icon indicating copy to clipboard operation
kafka-python copied to clipboard

`bootstrap_connected` is returning false even though connection is still there

Open prdpx7 opened this issue 3 years ago • 1 comments

I was trying to write a module to reconnect to Kafka after connection failure and i was using this kafka_producer.bootstrap_connected() to check if the connection is still there or not. It is returning faulty value.

Libraries used Python: 3.9.15 kafka-python: 2.0.2 Kafka: confluentinc/cp-kafka:7.0.1 Zookeeper: confluentinc/cp-zookeeper:7.0.1

Sample code

from kafka import KafkaProducer
import json
producer =  KafkaProducer(bootstrap_servers=["localhost:9092"], value_serializer=lambda payload: payload.encode("utf-8"), key_serializer=lambda key: key.encode("utf-8"))
producer.bootstrap_connected() # returns True
producer.send(topic="test-topic", value='{"x": 1, "y": 2}',key="") # working
producer.bootstrap_connected() # returns False??
producer.flush()
producer.bootstrap_connected() # returns False
producer.send(topic="test-topic", value='{"key":"value"}',key="") # this is working although it just said bootstrap is not connected
producer.bootstrap_connected()
producer.flush()

Sample Screenshot

  • IPython Screenshot 2022-11-25 at 4 39 11 PM Screenshot 2022-11-25 at 4 39 42 PM

  • Broker Screenshot 2022-11-25 at 4 34 30 PM

prdpx7 avatar Nov 25 '22 11:11 prdpx7

Have you found any solution?, I have a same problem with kafka-consumer module

devxxxparmar avatar Sep 05 '24 07:09 devxxxparmar

Can confirm this is still happening.

Python: 3.10.9 kafka-python: 2.0.2 Kafka: Docker apache/kafka:3.7.2 (using KRaft)

brandondotcomputer avatar Feb 09 '25 06:02 brandondotcomputer

This is expected. The "bootstrap" connections are exactly that: bootstraps. They are disconnected once the normal cluster connections are established. You can see this in the logs you posted. node_id=1 connects, node_id=bootstrap-0 disconnects...

dpkp avatar Feb 09 '25 18:02 dpkp