Skip to content

[fix][broker] Fix direct memory leak in RawReaderImpl - #18928

Merged
lhotari merged 1 commit into
apache:masterfrom
lhotari:lh-fix-leak-in-rawreader
Dec 20, 2022
Merged

[fix][broker] Fix direct memory leak in RawReaderImpl#18928
lhotari merged 1 commit into
apache:masterfrom
lhotari:lh-fix-leak-in-rawreader

Conversation

@lhotari

@lhotari lhotari commented Dec 14, 2022

Copy link
Copy Markdown
Member

Fixes #18927

Motivation

There's a direct memory leak in RawReaderImpl, please see #18927 for details.

Modifications

Override methods org.apache.pulsar.client.impl.ConsumerBase#failPendingReceive and org.apache.pulsar.client.impl.ConsumerBase#clearIncomingMessages in org.apache.pulsar.client.impl.RawReaderImpl.RawConsumerImpl so that cleanup happens properly. Don't create RawMessageImpl instances in messageReceived method if consumer is already closing or closed.
The changes should prevent future direct memory leaks caused by seeks or when the consumer is already closed.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository: lhotari#113

@lhotari lhotari added this to the 2.12.0 milestone Dec 14, 2022
@lhotari lhotari self-assigned this Dec 14, 2022
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Dec 14, 2022
Comment on lines +235 to +237
CompletableFuture<Void> closeFuture = super.closeAsync();
reset();
return super.closeAsync();
return closeFuture;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

closeAsync and messageReceived don't in the same thread run and have no locks. so the message also can be added in incomingRawMessages after it has been closed

step :

  1. thread-1 messageReceived check the state is ready.
    https://github.com/apache/pulsar/pull/18928/files#diff-2d41ea4027219e13a0a942edc54352bb0a997060112705796d1d811213d585f3R245
  2. thread-2 closeAsync() the RawReaderImpl state change to close and reset the incomingRawMessages
    https://github.com/apache/pulsar/pull/18928/files#diff-2d41ea4027219e13a0a942edc54352bb0a997060112705796d1d811213d585f3R245
  3. thread-1 messageReceived continue to run the add operation
    https://github.com/apache/pulsar/pull/18928/files#diff-2d41ea4027219e13a0a942edc54352bb0a997060112705796d1d811213d585f3R252

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that's true. Pulsar contains a lot of such race conditions. I wonder if it's worth fixing that one, since it would add complexity to the code. There are multiple of such race conditions in ConsumerBase's closeAsync method. The fix would have to be done there. The problem is more severe in ConsumerBase since it can impact the memory limit controller. I'll create a separate issue to track the issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I filed #18938

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I also added #18939

void messageReceived(CommandMessage commandMessage, ByteBuf headersAndPayload, ClientCnx cnx) {
State state = getState();
if (state == State.Closing || state == State.Closed) {
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we release headersAndPayload here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Perhaps. I think this is a problem that should be covered while fixing #18938 . It's not specific to RawReaderImpl's consumer.

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

I think that the best approach is to fix all the findings in separate patches.

I would commit this patch and then let @lhotari move forward with further fixes

@lhotari lhotari closed this Dec 20, 2022
@lhotari lhotari reopened this Dec 20, 2022
@codecov-commenter

codecov-commenter commented Dec 20, 2022

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.75862% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.79%. Comparing base (050b310) to head (5d35ed3).
⚠️ Report is 3124 commits behind head on master.

Files with missing lines Patch % Lines
...a/org/apache/pulsar/client/impl/RawReaderImpl.java 82.75% 3 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #18928      +/-   ##
============================================
- Coverage     47.35%   46.79%   -0.56%     
- Complexity     9384    10533    +1149     
============================================
  Files           623      706      +83     
  Lines         59104    69021    +9917     
  Branches       6146     7394    +1248     
============================================
+ Hits          27987    32300    +4313     
- Misses        28100    33118    +5018     
- Partials       3017     3603     +586     
Flag Coverage Δ
unittests 46.79% <82.75%> (-0.56%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...a/org/apache/pulsar/client/impl/RawReaderImpl.java 81.73% <82.75%> (-2.18%) ⬇️

... and 146 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lhotari
lhotari merged commit 46cacff into apache:master Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/broker doc-not-needed Your PR changes do not impact docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RawReaderImpl used by TwoPhaseCompactor leaks direct memory

6 participants