[fix][broker] Fix direct memory leak in RawReaderImpl - #18928
Conversation
3198712 to
5d35ed3
Compare
| CompletableFuture<Void> closeFuture = super.closeAsync(); | ||
| reset(); | ||
| return super.closeAsync(); | ||
| return closeFuture; |
There was a problem hiding this comment.
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 :
- thread-1
messageReceivedcheck the state is ready.
https://github.com/apache/pulsar/pull/18928/files#diff-2d41ea4027219e13a0a942edc54352bb0a997060112705796d1d811213d585f3R245 - thread-2
closeAsync()the RawReaderImpl state change to close and reset the incomingRawMessages
https://github.com/apache/pulsar/pull/18928/files#diff-2d41ea4027219e13a0a942edc54352bb0a997060112705796d1d811213d585f3R245 - thread-1
messageReceivedcontinue to run the add operation
https://github.com/apache/pulsar/pull/18928/files#diff-2d41ea4027219e13a0a942edc54352bb0a997060112705796d1d811213d585f3R252
There was a problem hiding this comment.
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.
| void messageReceived(CommandMessage commandMessage, ByteBuf headersAndPayload, ClientCnx cnx) { | ||
| State state = getState(); | ||
| if (state == State.Closing || state == State.Closed) { | ||
| return; |
There was a problem hiding this comment.
Should we release headersAndPayload here?
There was a problem hiding this comment.
Perhaps. I think this is a problem that should be covered while fixing #18938 . It's not specific to RawReaderImpl's consumer.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: lhotari#113