GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||
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
|
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
@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.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
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 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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
| return entry; | ||
| } | ||
|
|
||
| private static EntryImpl getEntryFromRecycler() { |
There was a problem 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.
There was an error while loading. Please reload this page.
| setRefCnt(1); | ||
| } | ||
|
|
||
| public static <T extends AbstractValidatingReferenceCounted> T getAndCheck(Recycler<T> recycler) { |
There was a problem 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.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| public final void resetRefCnt() { | ||
| setRefCnt(1); |
There was a problem 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.
Not the end of the world but my brain is having trouble leaving it alone.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| public static ChunkedMessageCtx get(int totalChunks) { | ||
| ChunkedMessageCtx chunkedMessageCtx = RECYCLER.get(); | ||
| chunkedMessageCtx.setRefCnt(totalChunks); | ||
| ChunkedMessageCtx chunkedMessageCtx = getAndCheck(RECYCLER); |
There was a problem 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.
There was an error while loading. Please reload this page.
|
@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.