GeezerVison is showing github.com — [fix][client] Client interceptors close method not called after producer/consumer/reader close by Shawyeok · Pull Request #23830 · apache/pulsar
Conversation
Shawyeok
commented
Jan 9, 2025
•
edited by merlimat
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation
ProducerInterceptor, ConsumerInterceptor and ReaderInterceptor declare a close() method so an interceptor can release what it holds, but the client never calls it. ProducerInterceptors.close() and ConsumerInterceptors.close() have no callers, so every interceptor that holds resources leaks them when its producer, consumer or reader closes. The built-in OpenTelemetryConsumerInterceptor is one of them: its close() ends the spans that are still open, and it never runs.
Modifications
ProducerBase and ConsumerBase get a closeInterceptors() helper that closes the interceptors at most once per producer or consumer.
ProducerImpl, ConsumerImpl, PartitionedProducerImpl and MultiTopicsConsumerImpl call it when they finish closing, on every close path. That includes closing while not connected and a close the broker rejects. Calling close again does not close the interceptors a second time. Readers are covered through their consumer.
- For a partitioned producer, each partition's producer gets a wrapper whose
close() does nothing, so the parent closes the interceptors once instead of once per partition. A multi-topic consumer already used such a wrapper for its per-topic consumers, but that wrapper's close() forwarded to the parent's interceptors, so it now does nothing too.
- The javadoc of the interceptors'
close() now says when it is called. An interceptor instance attached to several producers or consumers, for example through a reused builder, is closed once for each of them. This applies to interceptor.ProducerInterceptor, the deprecated ProducerInterceptor, ConsumerInterceptor and ReaderInterceptor.
This is a behavior change for interceptors whose close() is not idempotent and that are shared between producers or consumers. Until now, close() was never called.
Verifying this change
InterceptorsTest:
- producers, consumers and readers on non-partitioned and partitioned topics close their interceptor exactly once, even when closed twice;
- an interceptor shared by two producers from one builder is closed once for each.
ConsumerImplTest and ProducerImplTest: closing a producer or consumer that is not connected closes its interceptors once.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…cer/consumer/reader close
Call interceptors close method before close methods return.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Close the interceptors from a guarded helper in ProducerBase and
ConsumerBase, called when the producer or consumer finishes closing on
every path, including closing while not connected and closing twice.
Document in the interceptors' close() javadoc that a shared instance is
closed once for each producer or consumer it is attached to. Rework the
tests for the shared-cluster InterceptorsTest and add unit tests for the
not-connected close.
|
Hi @Shawyeok, I picked this up as part of the open-PR backlog pass (https://lists.apache.org/thread/cgy7w0qgnq8s7j0ykyzgrn3hj4nb67wf) and pushed two commits to your branch:
- A merge of
master. closeAsync() in ProducerImpl and ConsumerImpl now returns a composite future, which is where the conflicts came from.
- A fix on top of your change:
ProducerBase and ConsumerBase now have a closeInterceptors() helper that runs at most once.
- It is called on every close path, including closing while not connected (which
ProducerImpl skipped) and closing twice (which closed the consumer's interceptors twice, as Denovo1998 pointed out).
- Your partitioned-producer and multi-topic-consumer wrappers are kept.
- The interceptors'
close() javadoc now says it is called once per producer or consumer an instance is attached to.
- The tests now use mock interceptors and unique topic names, since
InterceptorsTest runs on the shared cluster.
I also updated the description. Please take a look and let me know if anything doesn't match what you intended.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Reviewers
merlimat
merlimat approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|