Skip to content

Fixed ByteBuf releasing after encoding values#298

Closed
Tomasz-Marciniak wants to merge 1 commit intor2dbc:mainfrom
intosoft-tm:feature/byte-buf-leak-fix
Closed

Fixed ByteBuf releasing after encoding values#298
Tomasz-Marciniak wants to merge 1 commit intor2dbc:mainfrom
intosoft-tm:feature/byte-buf-leak-fix

Conversation

@Tomasz-Marciniak
Copy link
Copy Markdown
Contributor

@Tomasz-Marciniak Tomasz-Marciniak commented Aug 26, 2025

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:

image

New Public APIs

No new public APIs introduced or modified.

Additional context

Running all test with:

-ea
-Dio.netty.leakDetection.level=PARANOID
-Dio.netty.recycler.maxCapacityPerThread=0

@Tomasz-Marciniak Tomasz-Marciniak force-pushed the feature/byte-buf-leak-fix branch from dd9119e to f5e2351 Compare August 26, 2025 16:42
@Tomasz-Marciniak Tomasz-Marciniak changed the title Fixed ByteBuf releasing after encoding values (ByteBuf.release() was not called). Fixed ByteBuf releasing after encoding values (Warning: ByteBuf.release() was not called). Aug 26, 2025
Signed-off-by: Tomasz Marciniak <[email protected]>
@Tomasz-Marciniak Tomasz-Marciniak force-pushed the feature/byte-buf-leak-fix branch from f5e2351 to d2f3f63 Compare August 27, 2025 19:20
return ByteBufUtil.getBytes(buffer);
} finally {
encoded.dispose();
if (buffer != null && buffer.refCnt() > 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

@Tomasz-Marciniak Tomasz-Marciniak Sep 8, 2025

Choose a reason for hiding this comment

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

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));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@mp911de
Copy link
Copy Markdown
Member

mp911de commented Sep 8, 2025 via email

@mp911de mp911de changed the title Fixed ByteBuf releasing after encoding values (Warning: ByteBuf.release() was not called). Fixed ByteBuf releasing after encoding values Sep 9, 2025
@mp911de mp911de linked an issue Sep 9, 2025 that may be closed by this pull request
@mp911de mp911de added the type: bug Something isn't working label Sep 9, 2025
@mp911de mp911de added this to the 1.0.3.RELEASE milestone Sep 9, 2025
mp911de pushed a commit that referenced this pull request Sep 9, 2025
Signed-off-by: Tomasz Marciniak <[email protected]>
[resolves #279][#298]
mp911de added a commit that referenced this pull request Sep 9, 2025
Introduce ReferenceCountUtil for safe and conditional release of ReferenceCounted objects.

Add missing author tags.

[#279][resolves #298]

Signed-off-by: Mark Paluch <[email protected]>
mp911de pushed a commit that referenced this pull request Sep 9, 2025
Signed-off-by: Tomasz Marciniak <[email protected]>
[resolves #279][#298]
@mp911de mp911de closed this in 47290fb Sep 9, 2025
@mp911de
Copy link
Copy Markdown
Member

mp911de commented Sep 9, 2025

Thank you for your contribution. That's merged, polished, and backported now.

@Tomasz-Marciniak
Copy link
Copy Markdown
Contributor Author

Tomasz-Marciniak commented Sep 9, 2025

Thank you for your contribution. That's merged, polished, and backported now.

Thank you for your review and adjustments! Glad we got it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak detected while using r2dbc-mssql 1.0.2

2 participants