Skip to content

[ML] Fix ByteBuf leaks when execution fails - #10755

Closed
lhotari wants to merge 4 commits into
apache:masterfrom
lhotari:lh-fix-bytebuf-leaks
Closed

lhotari wants to merge 4 commits into
apache:masterfrom
lhotari:lh-fix-bytebuf-leaks

Conversation

@lhotari

@lhotari lhotari commented May 31, 2021 •

Copy link
Copy Markdown
Member

Motivation

The repro case in #10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector. There are a few exceptional execution paths where the data ByteBuf of OpAddEntry isn't released.

Modifications

  • release the data ByteBuf in a try-finally block
  • release the data ByteBuf when execution is rejected in the invalid state case where the entry isn't the first in the queue.
  • release extra retain for OpAddEntry.data when entry is copied at ledger rollover

This seems to stop the detected leaks in #10738 case.

@lhotari
lhotari requested a review from codelipenghui May 31, 2021 18:36
@lhotari

lhotari commented May 31, 2021

Copy link
Copy Markdown
Member Author

@merlimat Please review. This seems to fix the memory leak that should up in #10738. The last commit, f176c15, in this PR made the difference.

@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.

great work @lhotari !

I left a few questions

}

synchronized OpAddEntry pollPendingAddEntry() {
return pendingAddEntries.poll();

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 make pendingAddEntries 'private' ?

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 was thinking of doing that but there is some test code that accesses the field directly.

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.

You could mark it with @VisibleForTesting

firstInQueue.failed(managedLedgerException);
}
} finally {
failed(managedLedgerException);

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.

Seems the OpAddEntry get failed but does not remove from the pending entries.

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.

that's true, but I guess that this PR is doing the right thing by calling .failed on the entries to discard them if the execution gets to this point? The previous behavior of using checkArgument(this == firstInQueue) would throw an IllegalArgumentException and leak ByteBufs on the discarded OpAddEntrys. Isn't this PR an improvement to that since it blocks the memory leak?
The repro case in #10738 was able to reproduce these errors in the original code amoung others.

@codelipenghui do you expect that some changes would be made to this PR?

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.

@lhotari The checkArgument(..) call is used in a lot of places in Pulsar. Could it be an issue in other places as well?

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.

@devinbost if the arguments get rejected and one of the argument is a Netty reference counted object, it would cause a memory leak if the called method is responsible for releasing the object.

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.

Regarding the comment from @codelipenghui , I don't remember seeing any logic to handle that case (of removing from pending entries upon failure), so should we just add that logic, or would we want to investigate the cause of why the get failed?

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.

@devinbost there is some logic for that.

pendingAddEntries.add(addOperation);
final State state = STATE_UPDATER.get(this);
if (state == State.Fenced) {
addOperation.failed(new ManagedLedgerFencedException());
return;
} else if (state == State.Terminated) {
addOperation.failed(new ManagedLedgerTerminatedException("Managed ledger was already terminated"));
return;
} else if (state == State.Closed) {
addOperation.failed(new ManagedLedgerAlreadyClosedException("Managed ledger was already closed"));
return;
} else if (state == State.WriteFailed) {
pendingAddEntries.remove(addOperation);
addOperation.failed(new ManagedLedgerAlreadyClosedException("Waiting to recover from failure"));
return;
}

It's a bit hard to understand why the addOperation gets added to the pendingAddEntries initially. Why doesn't this happen after the state is checked? However I consider handling this a separate issue.

In Netty, the contract of reference counted objects is that "The general rule of thumb is that the party that accesses a reference-counted object last is also responsible for the destruction of that reference-counted object.". This PR attempts to follow this rule. Therefore I'd suggest that we first process this PR and file separate issues to fix other problems.

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.

Looks like it's only removed according to the state, but if the sanity check fails, it's not handled right.

@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.

Overall LGTM

I wonder if you could add some unit test cases (even with Mockito is fine)

@lhotari

lhotari commented Jun 1, 2021

Copy link
Copy Markdown
Member Author

I discussed this PR with @merlimat . I'll split the PR into individual changes since this PR now contains multiple independent changes bundled in a single PR. I'll close this PR and open new PRs.

@lhotari lhotari closed this Jun 1, 2021
public void safeRun() {
// Remove this entry from the head of the pending queue
OpAddEntry firstInQueue = ml.pendingAddEntries.poll();
checkArgument(this == firstInQueue);

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.

I'd leave this change out of this PR. This is a "logic" check. If this fails it means we really have a code bug issue and we cannot make many assumptions on how to recover from it.

existsOp.close();
existsOp = OpAddEntry.create(existsOp.ml, existsOp.data, existsOp.getNumberOfMessages(), existsOp.callback, existsOp.ctx);
// release the extra retain
ReferenceCountUtil.release(existsOp.data);

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.

👍 I think this was introduced in #5942.

this.recycle();
} else {
ByteBuf data = this.data;
try {

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.

The change here LGTM, although I'd prefer to get it in a separate PR. Also, do you have a stack trace on why the exception would have been triggered here?

}
ByteBuf data = this.data;
try {
checkArgument(ledger.getId() == lh.getId(), "ledgerId %s doesn't match with acked ledgerId %s",

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.

Same comment for the other assertion above. This is really a code-sanity check. If this fails, it means things are very broken.

codelipenghui pushed a commit that referenced this pull request Jun 2, 2021
Fixes #10738

### Motivation

There's a ByteBuf leak that happens in ledger rollover. The repro case in #10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

### Modifications

Release `OpAddEntry.data` when the original entry is copied and discarded

### Additional Context

This PR was split out of PR #10755 which was closed without merging.
wangjialing218 pushed a commit to wangjialing218/pulsar that referenced this pull request Jun 2, 2021
)

Fixes apache#10738

### Motivation

There's a ByteBuf leak that happens in ledger rollover. The repro case in apache#10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

### Modifications

Release `OpAddEntry.data` when the original entry is copied and discarded

### Additional Context

This PR was split out of PR apache#10755 which was closed without merging.
lhotari added a commit that referenced this pull request Jun 2, 2021
Fixes #10738

There's a ByteBuf leak that happens in ledger rollover. The repro case in #10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

Release `OpAddEntry.data` when the original entry is copied and discarded

This PR was split out of PR #10755 which was closed without merging.
lhotari added a commit that referenced this pull request Jun 2, 2021
Fixes #10738

There's a ByteBuf leak that happens in ledger rollover. The repro case in #10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

Release `OpAddEntry.data` when the original entry is copied and discarded

This PR was split out of PR #10755 which was closed without merging.
eolivelli pushed a commit to datastax/pulsar that referenced this pull request Jun 7, 2021
)

Fixes apache#10738

There's a ByteBuf leak that happens in ledger rollover. The repro case in apache#10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

Release `OpAddEntry.data` when the original entry is copied and discarded

This PR was split out of PR apache#10755 which was closed without merging.

(cherry picked from commit 121305f)
yangl pushed a commit to yangl/pulsar that referenced this pull request Jun 23, 2021
)

Fixes apache#10738

### Motivation

There's a ByteBuf leak that happens in ledger rollover. The repro case in apache#10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

### Modifications

Release `OpAddEntry.data` when the original entry is copied and discarded

### Additional Context

This PR was split out of PR apache#10755 which was closed without merging.
bharanic-dev pushed a commit to bharanic-dev/pulsar that referenced this pull request Mar 18, 2022
)

Fixes apache#10738

### Motivation

There's a ByteBuf leak that happens in ledger rollover. The repro case in apache#10738 reproduces some Netty ByteBuf leaks that are detected by the Netty Leak detector.

### Modifications

Release `OpAddEntry.data` when the original entry is copied and discarded

### Additional Context

This PR was split out of PR apache#10755 which was closed without merging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants