Skip to content

Conversation

@akenra
Copy link
Contributor

@akenra akenra commented Mar 7, 2025

fix(kafka-topic-provisioner): Prevent resource leak on binding producer to KafkaMessageChannelBinder

  • Producer that fetches partition info now initializes within a try-with-resources block
  • If exceptions occur on calling producer.partitionsFor(topicName), it's now properly closed and resources are released

@akenra
Copy link
Contributor Author

akenra commented Mar 7, 2025

Hey folks, during the production use of the library I've noticed that whenever an error occurs (due to improper binder configuration, such as invalid topic name, or an authorization error on a broker, for example) when trying to fetch partition information for the topic on binding a Kafka Producer to KafkaMessageChannelBinder, it leads to a resource leak due to continous attempts to retry the initialization and subsequent recreation of Producer instances.

Which basically means the Producer instances (and whatever they reference), Producer IO thread count, open file descriptors, etc. keep growing indefinitely until resource exhaustion occurs - whether it's platform threads, or heap space, or a file descriptor limit. It can also cause a DDOS on the broker side due to leaking producers taking up TCP connections and never releasing them - we've actually experienced this first hand when 18 kubernetes pods suddenly began spamming our kafka broker with a large count of TCP connections that were kept alive forever.

After some time spent debugging the issue, I found out the root cause and felt that the solution is so glaringly obvious that I didn't bother creating a POC project and went straight for PR with a fix.

Please note that the issue isn't reproduced when fetching partition info for consumers in KafkaTopicProvisioner#getPartitionInfoForConsumer due to a proper use of try-with-resources.

Thanks!

@sobychacko
Copy link
Contributor

@akenra Thanks for the PR! Could you add your name as an author to the provisioner class you modified?

…er to KafkaMessageChannelBinder

- Producer that fetches partition info now initializes within a try-with-resources block
- If exceptions occur on calling producer.partitionsFor(topicName), it's now properly closed and resources are released

Signed-off-by: akenra <37288280+akenra@users.noreply.github.com>
@akenra
Copy link
Contributor Author

akenra commented Mar 7, 2025

@akenra Thanks for the PR! Could you add your name as an author to the provisioner class you modified?

Of course! Done now.

@sobychacko sobychacko merged commit e0fdfde into spring-cloud:main Mar 7, 2025
1 check passed
@sobychacko
Copy link
Contributor

@akenra Merged the PR and back-ported to 4.2.x and 4.1.x branches. Thanks again!

@sobychacko
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants