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 — [improve][misc][WIP] Detect "double release" and "use after release" bugs with recycled objects by lhotari · Pull Request #22110 · apache/pulsar

Conversation

@lhotari

@lhotari lhotari commented Feb 23, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Motivation

In Pulsar, users have reported issues that could be caused by "double release" or "use after release" bugs with recycled objects.

Here's are some issues that could potentially be caused by "double release" or "use after release" bugs:
#22035
#21892

Other example of such potential issue: #21421/#21933. It is possible that these issues are fixed by apache/bookkeeper#4196. The other root cause could be a "double release" or "use after release" bug which is corrupting the buffer and causing checksum calculation to fail.

Outside of Java, there's a known bug pattern called "double-free" or "Doubly freeing memory" (with malloc).
The "double release" bug pattern is a bit similar, but happens with the recycled object pattern using Netty's Recycler that Pulsar uses because of performance reasons.

There is also a "use after free" bug pattern. Something similar could be happen with Netty recycled objects that the object instance gets used after releasing.

The solution in this PR attempts to help detect "double release" and "use after release" bugs.

Modifications

  • get rid of any .setRefCnt(1) calls in production code since that could hide real issues
  • add additional detection feature that can be disabled by setting the system property -Dpulsar.refcount.check.on_access=false.
    • the refcount check will add a volatile read. This isn't a performance problem in most use cases. For production usage we could add -Dpulsar.refcount.check.on_access=false into the bin/pulsar script by default if we are afraid of the performance overhead. For all tests, we should be running with checks enabled so that we could find the source of problems.

Documentation

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

Sorry, something went wrong.

Uh oh!

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

All reactions
@lhotari
WIP Add checks for preventing hard-to-debug double release bugs with …
397b6d4
…recycled objects
@lhotari lhotari added the ready-to-test label Feb 23, 2024
@lhotari lhotari added this to the 3.3.0 milestone Feb 23, 2024
@lhotari
lhotari requested review from BewareMyPower, RobertIndie, codelipenghui, liudezhi2098, merlimat and onobc February 23, 2024 16:01
@lhotari lhotari self-assigned this Feb 23, 2024
@lhotari lhotari mentioned this pull request Feb 23, 2024
[Bug] [Broker] Entry digest does not match #22103
Open
1 of 2 tasks
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Feb 23, 2024
@lhotari lhotari changed the title WIP Detect double release bugs with recycled objects [improve][misc] WIP Detect double release bugs with recycled objects Feb 23, 2024
@lhotari
lhotari marked this pull request as ready for review February 23, 2024 16:03
@lhotari lhotari changed the title [improve][misc] WIP Detect double release bugs with recycled objects [improve][misc] WIP Detect "double release" and "use after release" bugs with recycled objects Feb 23, 2024
@lhotari lhotari changed the title [improve][misc] WIP Detect "double release" and "use after release" bugs with recycled objects [improve][misc][WIP] Detect "double release" and "use after release" bugs with recycled objects Feb 23, 2024
@lhotari

lhotari commented Feb 23, 2024

Copy link
Copy Markdown
Member Author

After making the changes, there are a lot of unit test failures. I haven't had a chance to look into the details. This PR is still a very early proposal about how to start detecting "double release" and "use after release" bugs.

All reactions

Sorry, something went wrong.

Uh oh!

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

lhotari added 2 commits February 23, 2024 22:47
@lhotari
refCnt starts at 1
373e343
@lhotari
Fix retain logic for ChunkedMessageCtx to match setRefCnt(totalChunks)
dc3e1a9
@lhotari lhotari mentioned this pull request Feb 23, 2024
[fix][Offload] fix indexEntries NullPointerException error #22035
Closed
2 of 15 tasks
BewareMyPower
BewareMyPower reviewed Feb 26, 2024
View reviewed changes

@BewareMyPower BewareMyPower left a comment •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The recycled objects are widely used in Pulsar, not only for EntryImpl. The 1st concern is that should we apply checks to all these places? For example, the client side could also use recycled objects.

The 2nd concern is, I'm afraid currently Pulsar allows a recycled object is accessed with a "null check". We need to investigate such cases.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryImpl.java Resolved

Uh oh!

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

@lhotari
Remove refCnt==0 check since object might be new or reused when it ge…
d59f9ab
…ts here
@lhotari

lhotari commented Feb 26, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

The recycled objects are widely used in Pulsar, not only for EntryImpl. The 1st concern is that should we apply checks to all these places? For example, the client side could also use recycled objects.

@BewareMyPower Yes that's a good point. It looks like the AbstractCASReferenceCounted/AbstractReferenceCounted base class prevents a lot of the problems since there would be exceptions at some point if there would be execution paths where "double release" or "use after release" bugs existed.

The 2nd concern is, I'm afraid currently Pulsar allows a recycled object is accessed with a "null check". We need to investigate such cases.

Yes, those are cases where the problem is getting hidden.

It would be great to find a better way to track down the issues. That's why I started this PR which is more like an experiment to find some way that would work for detecting "double release" and "use after release" bugs and also raise awareness of such bug patterns with the recycled objects. These are bug patterns that most Java developers have never had to deal with because of Java's garbage collection. With recycled objects and Netty ByteBufs, that all changes.

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari

lhotari commented Feb 26, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

I guess there's also the possibility of "double release" and "use after release" bugs with Netty ByteBufs.

In Netty, there's the leak detector for detecting when you don't release buffers, but there seems to be nothing for detecting the "use after release" bugs. I guess "double release" would be detected with the io.netty.buffer.AbstractReferenceCountedByteBuf base class which will throw an exception on double release.
It leaves the "use after release" bugs undetected.

It seems that the solution might be a Java Agent written with Byte Buddy etc. which would add additional checks with byte code instrumentation when the agent is activated. Thinking something like https://github.com/reactor/BlockHound but for a completely different purpose, to help detect "use after release" bugs.

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari
lhotari marked this pull request as draft February 26, 2024 06:03
@lhotari

lhotari commented Feb 26, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

The 2nd concern is, I'm afraid currently Pulsar allows a recycled object is accessed with a "null check". We need to investigate such cases.

Getting back to this one more time. Netty protects against most "use after release" ByteBuf bugs by setting the fields to null and the NPEs would be popping up as a sign of issues. Therefore it's extremely important that NPEs aren't suppressed with null checks.

All reactions

Sorry, something went wrong.

Uh oh!

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

onobc
onobc requested changes Mar 10, 2024
View reviewed changes

@onobc onobc left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Nice work @lhotari

My comments are mostly questions - 1 minor suggestion on naming. Other than that LGTM.

Also, because it is coupled w/ the "emergency brake" property , it seems harmless to add in .

The remaining piece will be to find out where we are currently doing the null checks and remove them.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/EntryImpl.java
return entry;
}

private static EntryImpl getEntryFromRecycler() {

@onobc onobc Mar 10, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

👍🏻 factoring this out makes it easier to follow (and add other common behaviors in one place later if needed - basic DRY stuff).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread ...r-common/src/main/java/org/apache/pulsar/common/util/AbstractValidatingReferenceCounted.java
setRefCnt(1);
}

public static <T extends AbstractValidatingReferenceCounted> T getAndCheck(Recycler<T> recycler) {

@onobc onobc Mar 10, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

[NIT] It is not really doing a "check". In the ACRC it was called getEntryFromRecycler. I think it would be helpful to name them the same thing (sans "entry"), maybe getInstanceFromRecycler.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread ...r-common/src/main/java/org/apache/pulsar/common/util/AbstractValidatingReferenceCounted.java
}

public final void resetRefCnt() {
setRefCnt(1);

@onobc onobc Mar 10, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It is unfortunate that setRefCnt is protected. Otherwise, this could have been done in a single util rather than inserted into the hierarchy (composition over inheritance). Also, the code in both hierarchies is almost identical.

  • The checkOnAccess var is just a static lookup.
  • The checkRefCount and getFromRecycler could be passed in the counter or recycler, respectively

Not the end of the world but my brain is having trouble leaving it alone.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
public static ChunkedMessageCtx get(int totalChunks) {
ChunkedMessageCtx chunkedMessageCtx = RECYCLER.get();
chunkedMessageCtx.setRefCnt(totalChunks);
ChunkedMessageCtx chunkedMessageCtx = getAndCheck(RECYCLER);

@onobc onobc Mar 10, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is setting ref count to 1 and then retaining N-1 less performant than setting ref count to N?

OR

Is setting ref count to 1 and then retaining N-1 more functionally correct than setting ref count to N?

Sorry, something went wrong.

Uh oh!

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

All reactions
@coderzc coderzc modified the milestones: 3.3.0, 3.4.0 May 8, 2024
@lhotari lhotari mentioned this pull request May 17, 2024
[Bug] parseMessageMetadata error when broker entry metadata enable with high loading #22601
Closed
2 of 3 tasks
@coderzc coderzc added this to the 4.2.0 milestone Sep 1, 2025
@lhotari lhotari added release/4.0.8 and removed release/4.0.7 labels Sep 27, 2025
@lhotari lhotari added release/4.0.9 and removed release/4.0.8 labels Nov 11, 2025
@lhotari lhotari added release/4.0.10 and removed release/4.0.9 labels Feb 9, 2026
@lhotari lhotari added release/4.0.11 and removed release/4.0.10 labels Apr 27, 2026
@lhotari lhotari modified the milestones: 4.2.0, 4.3.0 Apr 27, 2026
@lhotari lhotari added the release/4.2.2 label Apr 27, 2026
@lhotari lhotari added release/4.2.3 release/4.0.12 and removed release/4.2.2 release/4.0.11 labels Jun 8, 2026
@lhotari lhotari added release/4.2.4 release/4.0.13 and removed release/4.2.3 release/4.0.12 labels Jun 30, 2026
@lhotari lhotari added release/4.2.5 release/4.0.14 and removed release/4.2.4 release/4.0.13 labels Jul 27, 2026
@lhotari lhotari added release/4.0.15 release/4.2.6 and removed release/4.0.14 release/4.2.5 labels Oct 14, 2026
@merlimat

merlimat commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

@lhotari thanks for working on this. It was left as an unfinished draft, with the unit test failures from enabling the checks still to be sorted out, and the managed-ledger and client code it touches has changed quite a bit since. We're going through the backlog of open PRs (https://lists.apache.org/thread/cgy7w0qgnq8s7j0ykyzgrn3hj4nb67wf), so I'm closing this one. If you'd like to continue, feel free to reopen it rebased on current master.

All reactions

Sorry, something went wrong.

Uh oh!

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

@merlimat merlimat closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@BewareMyPower BewareMyPower BewareMyPower left review comments
@onobc onobc onobc requested changes
@merlimat merlimat Awaiting requested review from merlimat
@codelipenghui codelipenghui Awaiting requested review from codelipenghui
@RobertIndie RobertIndie Awaiting requested review from RobertIndie
@liudezhi2098 liudezhi2098 Awaiting requested review from liudezhi2098

Uh oh!

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

Assignees

@lhotari lhotari

Labels

doc-not-needed Your PR changes do not impact docs ready-to-test release/4.0.15 release/4.2.6

Projects

None yet

Milestone

5.0.0-M1

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

5 participants

@lhotari @merlimat @BewareMyPower @onobc @coderzc
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.