Fixed ByteBuf releasing after encoding values#298
Fixed ByteBuf releasing after encoding values#298Tomasz-Marciniak wants to merge 1 commit intor2dbc:mainfrom
Conversation
dd9119e to
f5e2351
Compare
Signed-off-by: Tomasz Marciniak <[email protected]>
f5e2351 to
d2f3f63
Compare
| return ByteBufUtil.getBytes(buffer); | ||
| } finally { | ||
| encoded.dispose(); | ||
| if (buffer != null && buffer.refCnt() > 0) { |
There was a problem hiding this comment.
if (buffer.refCnt() > 0) buffer.release() is a recurring call across the code base. Care to add a ReferenceCountedUtil to the io.r2dbc.mssql.util package that would cover instance-of check (passing in an Object), catering for nullability and having the refCnt() check?
There was a problem hiding this comment.
Sure I can move it, the it would look like:
public class ReferenceCountedUtil {
public static boolean safeRelease(ByteBuf buffer) {
if(buffer.refCnt() > 0){
return buffer.release();
}
return false;
}
}
I don't understand why we to pass it as Object since all these checks are done for ByteBuf type.
Additionally there is io.netty.util.ReferenceCountUtil#safeRelease(java.lang.Object) which seems to do some magic around releasing the buffer in safe mode. However I don't want to decrease performance by making so many count check:
public final boolean release(T instance) {
int rawCnt = nonVolatileRawCnt(instance);
return rawCnt == 2 ? tryFinalRelease0(instance, 2) || retryRelease0(instance, 1)
: nonFinalRelease0(instance, 1, rawCnt, toLiveRealRefCnt(rawCnt, 1));
}
There was a problem hiding this comment.
I don't understand why we to pass it as Object since all these checks are done for ByteBuf type.
This is the design choice that netty made and there's no imminent reason to deviate or invent our own design that would introduce accidental complexity.
However I don't want to decrease performance by making so many count check:
io.netty.util.ReferenceCountUtil#safeRelease(java.lang.Object) contains flow control by exceptions and logging. I don't see the need to have multiple count checks and instanceof checks (as in ReferenceCountUtil) have a lower CPU penalty than exceptions.
|
Thanks for mentioning the utility. I was already looking for it but I was only able to find variants that catch exceptions and log these. Let me have another look.
Am 8. Sept. 2025, 13:00 +0200 schrieb Tomasz Marciniak ***@***.***>:
… @Tomasz-Marciniak commented on this pull request.
In src/main/java/io/r2dbc/mssql/codec/ByteArray.java:
> } finally {
- encoded.dispose();
+ if (buffer != null && buffer.refCnt() > 0) {
This is the only place with null check since I moved buffer assignment outside try/catch.
All other places does not make a null check since they are invoked in loops.
I don't understand why we to pass it as Object since all these checks are done for ByteBuf type.
Additionally there is io.netty.util.ReferenceCountUtil#safeRelease(java.lang.Object) which seems to do some magic around releasing the buffer in safe mode. However I don't want to decrease performance by making so many count check:
public final boolean release(T instance) {
int rawCnt = nonVolatileRawCnt(instance);
return rawCnt == 2 ? tryFinalRelease0(instance, 2) || retryRelease0(instance, 1)
: nonFinalRelease0(instance, 1, rawCnt, toLiveRealRefCnt(rawCnt, 1));
}
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Signed-off-by: Tomasz Marciniak <[email protected]> [resolves #279][#298]
Introduce ReferenceCountUtil for safe and conditional release of ReferenceCounted objects. Add missing author tags. [#279][resolves #298] Signed-off-by: Mark Paluch <[email protected]>
Signed-off-by: Tomasz Marciniak <[email protected]> [resolves #279][#298]
|
Thank you for your contribution. That's merged, polished, and backported now. |
Thank you for your review and adjustments! Glad we got it merged. |
Make sure that:
You have read the contribution guidelines.
You have created a feature request first to discuss your contribution intent. Please reference the feature request ticket number in the pull request.
This is bug fix, no feature request created.
You use the code formatters provided here and have them applied to your changes. Don't submit any formatting related changes.
You submit test cases (unit or integration tests) that back your changes.
Reused existing test cases
Issue description
This PR fixes all ByteBuf leak warnings that were visible both during test execution and in application runtime. Around 41 individual leaks were identified and corrected, ensuring proper reference counting and buffer release.
Fixed ~41 ByteBuf leaks discovered during tests.
Corrected improper buffer handling in application paths where leaks were visible at runtime.
All these warning have been fixed:
New Public APIs
No new public APIs introduced or modified.
Additional context
Running all test with: