Skip to content

KAFKA-20398: Fix memory leak by removing StreamsThreadMetricsDelegatingReporter from metrics registry on thread shutdown - #21973

Merged
bbejeck merged 1 commit into
apache:trunkfrom
sstremler:KAFKA-20398-remove-metrics-reporter-from-registry-on-shutdown
Apr 7, 2026
Merged

bbejeck merged 1 commit into
apache:trunkfrom
sstremler:KAFKA-20398-remove-metrics-reporter-from-registry-on-shutdown

Conversation

@sstremler

@sstremler sstremler commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

When a stream thread in Kafka Streams crashes,
StreamsUncaughtExceptionHandler can be configured to replace the
crashed thread with a new one. Each time a new stream thread is created,
it adds a new StreamsThreadMetricsDelegatingReporter to it. However,
when the old thread shuts down, its reporter is never removed from the
registry implying "unbounded" memory growth.

This bug was introduced via #17021

This PR ensures that the existing
StreamsThreadMetricsDelegatingReporter is removed when a thread is
shutting down.

Reviewers: Matthias J. Sax matthias@confluent.io, Bill Bejeck
bbejeck@apache.org

…ngReporter from metrics registry on thread shutdown

Signed-off-by: Szabolcs Stremler <stremler.sz@gmail.com>
@github-actions github-actions Bot added triage PRs from the community streams small Small PRs labels Apr 6, 2026
@mjsax mjsax added ci-approved and removed triage PRs from the community labels Apr 7, 2026

@mjsax mjsax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix. LGTM.

@bbejeck bbejeck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, LGTM

@bbejeck
bbejeck merged commit 71c0882 into apache:trunk Apr 7, 2026
21 checks passed
@bbejeck

bbejeck commented Apr 7, 2026

Copy link
Copy Markdown
Member

Merged #21971 into trunk

bbejeck pushed a commit that referenced this pull request Apr 7, 2026
…ngReporter from metrics registry on thread shutdown (#21973)

When a stream thread in Kafka Streams crashes,
`StreamsUncaughtExceptionHandler` can be configured to replace the
crashed thread with a new one. Each time a new stream thread is created,
it adds a new `StreamsThreadMetricsDelegatingReporter` to it. However,
when the old thread shuts down, its reporter is never removed from the
registry implying "unbounded" memory growth.

This bug was introduced via #17021

This PR ensures that the existing
`StreamsThreadMetricsDelegatingReporter` is removed when a thread is
shutting down.

Reviewers: Matthias J. Sax <matthias@confluent.io>, Bill Bejeck
 <bbejeck@apache.org>

Signed-off-by: Szabolcs Stremler <stremler.sz@gmail.com>
@bbejeck

bbejeck commented Apr 7, 2026

Copy link
Copy Markdown
Member

cherry-picked to 4.3

@bbejeck

bbejeck commented Apr 7, 2026

Copy link
Copy Markdown
Member

@sstremler I tried cherry-picking to 4.2 but there are some conflcts, can you do a separate PR for 4.2?

@sstremler

Copy link
Copy Markdown
Contributor Author

@sstremler I tried cherry-picking to 4.2 but there are some conflcts, can you do a separate PR for 4.2?

@bbejeck Sure: #21989

nileshkumar3 pushed a commit to nileshkumar3/kafka that referenced this pull request Apr 15, 2026
…ngReporter from metrics registry on thread shutdown (apache#21973)

When a stream thread in Kafka Streams crashes,
`StreamsUncaughtExceptionHandler` can be configured to replace the
crashed thread with a new one. Each time a new stream thread is created,
it adds a new `StreamsThreadMetricsDelegatingReporter` to it. However,
when the old thread shuts down, its reporter is never removed from the
registry implying "unbounded" memory growth.

This bug was introduced via apache#17021

This PR ensures that the existing
`StreamsThreadMetricsDelegatingReporter` is removed when a thread is
shutting down.

Reviewers: Matthias J. Sax <matthias@confluent.io>, Bill Bejeck
 <bbejeck@apache.org>

Signed-off-by: Szabolcs Stremler <stremler.sz@gmail.com>
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.

3 participants