Skip to content

Skip empty bootstrap_servers entries in collect_hosts - #3215

Open
secretJod wants to merge 1 commit into
dpkp:masterfrom
secretJod:fix/collect-hosts-skip-empty
Open

secretJod wants to merge 1 commit into
dpkp:masterfrom
secretJod:fix/collect-hosts-skip-empty

Conversation

@secretJod

Copy link
Copy Markdown

collect_hosts doesn't skip empty entries, so a trailing comma or a blank item in bootstrap_servers adds a hidden bootstrap broker at localhost:9092:

>>> collect_hosts("b1:9092,b2:9092,", randomize=False)
[('b1', 9092, AF_UNSPEC), ('b2', 9092, AF_UNSPEC), ('', 9092, AF_UNSPEC)]
>>> socket.getaddrinfo('', 9092, 0, socket.SOCK_STREAM)  # -> ::1 / 127.0.0.1

This is easy to hit when the value is built from env vars or templates. Depending on the setup, you get connection errors to localhost in the logs, or the client bootstraps against a local broker nobody configured. The Java client trims entries and ignores empty ones (ClientUtils.parseAndValidateAddresses).

The fix strips each entry and skips it if it's empty after removing the PROTOCOL:// prefix, so a bare PLAINTEXT:// is skipped too.

Tests: added test_collect_hosts__ignores_empty_entries with trailing comma, double comma, whitespace-only, bare protocol and list inputs. It fails before the change and passes after. The unit suite passes: pytest test --ignore=test/integration gives 2764 passed, 1 skipped.

I left the all-empty case (bootstrap_servers=",") alone. It now returns no hosts instead of [('', 9092)]. Raising there like Java does seemed like a separate decision; happy to add it if you want.

I used Claude Code to help find and test this, and I reviewed the change myself.

A trailing comma or blank entry (e.g. "b1:9092,b2:9092,") produced a
('', 9092) host, which getaddrinfo resolves to localhost. The java client
trims entries and ignores empty ones; do the same.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant