Skip to content

Improve connection error handling when try_api_versions_check fails all attempts #2548

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 4 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion kafka/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ def _try_api_versions_check(self):
if self._api_versions_future is None:
if self.config['api_version'] is not None:
self._api_version = self.config['api_version']
# api_version will be normalized by KafkaClient, so this should not happen
if self._api_version not in BROKER_API_VERSIONS:
raise Errors.UnrecognizedBrokerVersion('api_version %s not found in kafka.protocol.broker_api_versions' % (self._api_version,))
self._api_versions = BROKER_API_VERSIONS[self._api_version]
log.debug('%s: Using pre-configured api_version %s for ApiVersions', self, self._api_version)
return True
Expand All @@ -553,7 +556,8 @@ def _try_api_versions_check(self):
self.state = ConnectionStates.API_VERSIONS_RECV
self.config['state_change_callback'](self.node_id, self._sock, self)
else:
raise 'Unable to determine broker version.'
self.close(Errors.KafkaConnectionError('Unable to determine broker version.'))
return False

for r, f in self.recv():
f.success(r)
Expand Down
30 changes: 30 additions & 0 deletions test/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ def test_connect(_socket, conn, states):
assert conn.state is state


def test_api_versions_check(_socket):
conn = BrokerConnection('localhost', 9092, socket.AF_INET)
assert conn._api_versions_future is None
conn.connect()
assert conn._api_versions_future is not None
assert conn.connecting() is True
assert conn.state is ConnectionStates.API_VERSIONS_RECV

assert conn._try_api_versions_check() is False
assert conn.connecting() is True
assert conn.state is ConnectionStates.API_VERSIONS_RECV

conn._api_versions_future = None
conn._check_version_idx = 0
assert conn._try_api_versions_check() is False
assert conn.connecting() is True

conn._check_version_idx = len(conn.VERSION_CHECKS)
conn._api_versions_future = None
assert conn._try_api_versions_check() is False
assert conn.connecting() is False
assert conn.disconnected() is True


def test_api_versions_check_unrecognized(_socket):
conn = BrokerConnection('localhost', 9092, socket.AF_INET, api_version=(0, 0))
with pytest.raises(Errors.UnrecognizedBrokerVersion):
conn.connect()


def test_connect_timeout(_socket, conn):
assert conn.state is ConnectionStates.DISCONNECTED

Expand Down
Loading