Skip to content

GH-3332: Always release consumerFactoryLock in KafkaBinderMetrics.close() - #3353

Closed
caiocv18 wants to merge 1 commit into
spring-cloud:mainfrom
caiocv18:gh-3332-metrics-close-unlock
Closed

caiocv18 wants to merge 1 commit into
spring-cloud:mainfrom
caiocv18:gh-3332-metrics-close-unlock

Conversation

@caiocv18

Copy link
Copy Markdown
Contributor

Follow-up to #3332, noticed while working on that fix.

close() closes the metadata consumers in its finally block before consumerFactoryLock.unlock(). If a Consumer.close() throws, the unlock is skipped: the lock stays held by the closing thread, the remaining consumers are left open and metadataConsumers is not cleared. KafkaConsumer.close() is documented to throw InterruptException and KafkaException, and close() reaches that block with the interrupt flag set when awaitTermination() is interrupted.

  • Close each metadata consumer on its own and log a failure instead of propagating it, so all of them are closed and the map is cleared.
  • Release consumerFactoryLock in a nested finally.

The new test creates two metadata consumers, one of which throws from close(), and then checks from another thread that consumerFactoryLock can be acquired:

  • main: fails with consumerFactoryLock is still held after close()
  • this PR: passes, along with the other 14 tests in KafkaBinderMetricsTest

The same block is on 5.0.x.

…rMetrics.close()

close() closed the metadata consumers in its finally block before
unlocking consumerFactoryLock. A Consumer.close() that threw skipped the
unlock, so the lock stayed held by the closing thread, the remaining
consumers were left open and the map was not cleared.
KafkaConsumer.close() is documented to throw InterruptException and
KafkaException, and close() reaches that block with the interrupt flag
set when awaitTermination() is interrupted.

* Close each metadata consumer on its own and log a failure instead of
  propagating it, so all of them are closed and the map is cleared
* Release consumerFactoryLock in a nested finally

Follow-up to spring-cloud#3332

Signed-off-by: Caio Vinicius <caioviniciuscv18@gmail.com>
olegz pushed a commit that referenced this pull request Nov 29, 2026
…se()

close() closed the metadata consumers in its finally block before
unlocking consumerFactoryLock. A Consumer.close() that threw skipped the
unlock, so the lock stayed held by the closing thread, the remaining
consumers were left open and the map was not cleared.
KafkaConsumer.close() is documented to throw InterruptException and
KafkaException, and close() reaches that block with the interrupt flag
set when awaitTermination() is interrupted.

* Close each metadata consumer on its own and log a failure instead of
  propagating it, so all of them are closed and the map is cleared
* Release consumerFactoryLock in a nested finally

Follow-up to #3332

Signed-off-by: Caio Vinicius <caioviniciuscv18@gmail.com>

Resolves #3353

Signed-off-by: Oleg Zhurakousky <oleg.zhurakousky@broadcom.com>
@olegz olegz closed this in 61f3f79 Nov 29, 2026
@olegz

olegz commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Thank you for the follow-up. Merged to main as 61f3f79 and backported to 5.0.x as cf35b26 (the main commit was amended only to add the Resolves line and a sign-off).

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.

2 participants