Make NettyByteBuf share parent reference count.#1891
Make NettyByteBuf share parent reference count.#1891vbabanin wants to merge 5 commits intomongodb:mainfrom
Conversation
|
LGTM |
rozza
left a comment
There was a problem hiding this comment.
Recommended a minor tweak - but its optional as the new code is essentially the same.
Recommend reenabling the test in CommandHelperSpecification.groovy which was the source of the original ticket.
Other than that LGTM
driver-core/src/main/com/mongodb/internal/connection/netty/NettyStream.java
Outdated
Show resolved
Hide resolved
…tyStream.java Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
|
Duplication from Slack (I should have really posted that comment here, to avoid trying to find it later in Slack if it is needed): Let's create a ticket for this PR. There we may say that when fixing https://jira.mongodb.org/browse/JAVA-5901, we replaced a pair of invocations |
NettyButeBufcurrently usesretainedDuplicate()when duplicating Netty buffers. WhileretainedDuplicate()is documented as similar toduplicate().retain(), the two can differ underpooled Netty buffers, which can surface asIllegalReferenceCountExceptionin the command logging path.What’s happening (lifetime difference in this flow)
duplicate().retain() (shared refCnt):
duplicate() returns a view that shares the parent’s refCnt; retain() increments that shared counter. Behavior in the relevant block:
Detailed diagnostic
retainedDuplicate() (may be an independent wrapper lifecycle):
With pooled buffers, retainedDuplicate() can return a derived buffer object
(P)with its own lifecycle. ReleasingPmakesPunusable even if the underlying memory is still retained by the parent. Behavior withretainedDuplicate():Why the behavior depends on pooling (method name is not the contract)
Pooled buffers use recycled derived objects (“less garbage”), and recycled objects need their own ref counter so they can be returned to the recycler when their counter hits 0. Under
PooledByteBufAllocator,retainedDuplicate()can therefore change the ref-counting model.If the allocator is
Unpooled,retainedDuplicate()is identical toduplicate().retain()and this issue does not surface.Change in this PR
Revert this specific duplication call back to
duplicate().retain()to restore the shared-refCnt lifetime behavior, while keeping the rest of the close/release changes intact.JAVA-6107