Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NettyCodecAdapter decoder memory leak #14538

Merged
merged 3 commits into from
Aug 28, 2024
Merged

fix NettyCodecAdapter decoder memory leak #14538

merged 3 commits into from
Aug 28, 2024

Conversation

JoeCqupt
Copy link
Contributor

@JoeCqupt JoeCqupt commented Aug 14, 2024

What is the purpose of the change

reproduce code

    @Test
    void testDecodeException() throws IOException {
        Codec2 codec2 = Mockito.mock(Codec2.class);
        doThrow(new IOException("testDecodeIllegalPacket")).when(codec2).decode(any(), any());

        URL url = Mockito.mock(URL.class);
        doReturn("default").when(url).getParameter(eq(Constants.CODEC_KEY));

        ChannelHandler handler = Mockito.mock(ChannelHandler.class);
        NettyCodecAdapter nettyCodecAdapter = new NettyCodecAdapter(codec2, url, handler);
        io.netty.channel.ChannelHandler decoder = nettyCodecAdapter.getDecoder();
        EmbeddedChannel embeddedChannel = new EmbeddedChannel();
        embeddedChannel.pipeline().addLast(decoder);

        // simulate illegal data packet
        ByteBuf input = AbstractByteBufAllocator.DEFAULT.buffer();
        input.writeBytes("testDecodeIllegalPacket".getBytes(StandardCharsets.UTF_8));

        DecoderException decoderException = Assertions.assertThrows(DecoderException.class, () -> {
            embeddedChannel.writeInbound(input);
        });
        Assertions.assertTrue( decoderException.getCause() instanceof IOException);

        Assertions.assertEquals(0, input.refCnt());
    }

when NettyCodecAdapter decoder decode exception param ByteBuf not released

expect refcnt 0 , actual 1

Solution
when decode excpetion . skip all readable bytes let ByteToMessageDecoder release ByteBuf

Brief changelog

Verifying this change

Checklist

  • Make sure there is a GitHub_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GitHub issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Check if is necessary to patch to Dubbo 3 if you are work on Dubbo 2.7
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Add some description to dubbo-website project if you are requesting to add a feature.
  • GitHub Actions works fine on your own branch.
  • If this contribution is large, please follow the Software Donation Guide.

@JoeCqupt
Copy link
Contributor Author

@AlbumenJ PTAL

Copy link

sonarcloud bot commented Aug 21, 2024

Copy link
Member

@AlbumenJ AlbumenJ left a comment

Choose a reason for hiding this comment

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

@oxsean PTAL

@oxsean oxsean self-requested a review August 23, 2024 10:32
Copy link
Collaborator

@oxsean oxsean left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@EarthChen EarthChen left a comment

Choose a reason for hiding this comment

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

LGTM

@EarthChen EarthChen merged commit 1190033 into apache:3.2 Aug 28, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants