GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@Shawyeok Shawyeok commented Jan 9, 2025 •
edited by merlimat
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

All reactions
@Shawyeok
[fix][client] Client interceptors close method not called after produ…
310095f
…cer/consumer/reader close

Call interceptors close method before close methods return.
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Jan 9, 2025
@Shawyeok

Shawyeok commented Jan 9, 2025

Copy link
Copy Markdown
Contributor Author

@codelipenghui Please feel free take a look, thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Denovo1998
Denovo1998 reviewed Jun 14, 2025
View reviewed changes
Comment thread pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

merlimat added 2 commits October 22, 2026 11:48
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/adopt-23830-…
6f85816
…interceptor-close
@merlimat
[fix][client] Close interceptors exactly once on every close path
2d2e9ae
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.
@merlimat

merlimat commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

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:

  1. A merge of master. closeAsync() in ProducerImpl and ConsumerImpl now returns a composite future, which is where the conflicts came from.
  2. 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

merlimat
merlimat approved these changes Oct 22, 2026
View reviewed changes
@merlimat merlimat added this to the 5.1.0 milestone Oct 22, 2026
@merlimat
merlimat merged commit a883ea4 into apache:master Oct 22, 2026
44 checks passed

Uh oh!

There was an error while loading. Please reload this page.

lhotari pushed a commit that referenced this pull request Oct 23, 2026
@Shawyeok @merlimat @lhotari
[fix][client] Client interceptors close method not called after produ…
2a3665e
…cer/consumer/reader close (#23830)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit a883ea4)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Oct 23, 2026
@sharanggupta sharanggupta mentioned this pull request Oct 28, 2026
[fix][client] Close interceptors once after growth #27163
Merged
11 tasks
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@Shawyeok @merlimat @lhotari
[fix][client] Client interceptors close method not called after produ…
29c5b18
…cer/consumer/reader close (apache#23830)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit a883ea4)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@Shawyeok @merlimat @lhotari
[fix][client] Client interceptors close method not called after produ…
3f75d91
…cer/consumer/reader close (apache#23830)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit a883ea4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Denovo1998 Denovo1998 Denovo1998 left review comments
@merlimat merlimat merlimat approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 doc-not-needed Your PR changes do not impact docs release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Shawyeok @merlimat @Denovo1998 @lhotari
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.