Skip to content

Fix a Bytebuf leak in TcpDnsQueryDecoder (#15033) - #15035

Merged
normanmaurer merged 1 commit into
4.1from
tcp_dns_leak
Apr 15, 2025
Merged

normanmaurer merged 1 commit into
4.1from
tcp_dns_leak

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

Motivation:

A memory leak was detected during the implementation of a DNS over TCP server.

15:49:44.002 [armeria-common-worker-kqueue-2-6] ERROR io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was not called before it's garbage-collected. See https://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records:
  io.netty.buffer.AdvancedLeakAwareByteBuf.readUnsignedShort(AdvancedLeakAwareByteBuf.java:419)
  io.netty.handler.codec.dns.DefaultDnsRecordDecoder.decodeQuestion(DefaultDnsRecordDecoder.java:38)
  io.netty.handler.codec.dns.DnsMessageUtil.decodeQuestions(DnsMessageUtil.java:221)
  io.netty.handler.codec.dns.DnsMessageUtil.decodeDnsQuery(DnsMessageUtil.java:192)
  io.netty.handler.codec.dns.TcpDnsQueryDecoder.decode(TcpDnsQueryDecoder.java:48)
  io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:333)
  io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530)
  io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469)
  io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
  io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)

It seems that the frame generated by
LengthFieldBasedFrameDecoder.decode() TcpDnsQueryDecoder is not released after being decoded by TcpDnsQueryDecoder.

protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf frame = (ByteBuf) super.decode(ctx, in);
if (frame == null) {
return null;
}
return DnsMessageUtil.decodeDnsQuery(decoder, frame.slice(), new DnsMessageUtil.DnsQueryFactory() {
@Override
public DnsQuery newQuery(int id, DnsOpCode dnsOpCode) {
return new DefaultDnsQuery(id, dnsOpCode);
}
});

Motifications:

  • Release frame after use.

Result:

Resolved a memory leak in TcpDnsQueryEncoder

Motivation:

A memory leak was detected during the implementation of a DNS over TCP
server.
```
15:49:44.002 [armeria-common-worker-kqueue-2-6] ERROR io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was not called before it's garbage-collected. See https://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records:
  io.netty.buffer.AdvancedLeakAwareByteBuf.readUnsignedShort(AdvancedLeakAwareByteBuf.java:419)
  io.netty.handler.codec.dns.DefaultDnsRecordDecoder.decodeQuestion(DefaultDnsRecordDecoder.java:38)
  io.netty.handler.codec.dns.DnsMessageUtil.decodeQuestions(DnsMessageUtil.java:221)
  io.netty.handler.codec.dns.DnsMessageUtil.decodeDnsQuery(DnsMessageUtil.java:192)
  io.netty.handler.codec.dns.TcpDnsQueryDecoder.decode(TcpDnsQueryDecoder.java:48)
  io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:333)
  io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530)
  io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469)
  io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
  io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
```

It seems that the `frame` generated by
`LengthFieldBasedFrameDecoder.decode()` `TcpDnsQueryDecoder` is not
released after being decoded by `TcpDnsQueryDecoder`.
https://github.com/netty/netty/blob/d639d876aa577eea21acebaa970c8cc60644a851/codec-dns/src/main/java/io/netty/handler/codec/dns/TcpDnsQueryDecoder.java#L42-L53

Motifications:

- Release `frame` after use.

Result:

Resolved a memory leak in `TcpDnsQueryEncoder`
@normanmaurer normanmaurer added this to the 4.1.120.Final milestone Apr 15, 2025
@normanmaurer
normanmaurer merged commit 4adb523 into 4.1 Apr 15, 2025
@normanmaurer
normanmaurer deleted the tcp_dns_leak branch April 15, 2025 17:40
normanmaurer added a commit that referenced this pull request Apr 15, 2025
Motivation:

A memory leak was detected during the implementation of a DNS over TCP
server.
```
15:49:44.002 [armeria-common-worker-kqueue-2-6] ERROR io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was not called before it's garbage-collected. See https://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records:
  io.netty.buffer.AdvancedLeakAwareByteBuf.readUnsignedShort(AdvancedLeakAwareByteBuf.java:419)
  io.netty.handler.codec.dns.DefaultDnsRecordDecoder.decodeQuestion(DefaultDnsRecordDecoder.java:38)
  io.netty.handler.codec.dns.DnsMessageUtil.decodeQuestions(DnsMessageUtil.java:221)
  io.netty.handler.codec.dns.DnsMessageUtil.decodeDnsQuery(DnsMessageUtil.java:192)
  io.netty.handler.codec.dns.TcpDnsQueryDecoder.decode(TcpDnsQueryDecoder.java:48)
  io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:333)
  io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530)
  io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469)
  io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
  io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
```

It seems that the `frame` generated by
`LengthFieldBasedFrameDecoder.decode()` `TcpDnsQueryDecoder` is not
released after being decoded by `TcpDnsQueryDecoder`.
https://github.com/netty/netty/blob/d639d876aa577eea21acebaa970c8cc60644a851/codec-dns/src/main/java/io/netty/handler/codec/dns/TcpDnsQueryDecoder.java#L42-L53

Motifications:

- Release `frame` after use.

Result:

Resolved a memory leak in `TcpDnsQueryEncoder`

Co-authored-by: Ikhun Um <[email protected]>
yaooqinn pushed a commit to apache/spark that referenced this pull request Apr 29, 2025
### What changes were proposed in this pull request?

This PR aims to Upgrade Netty to 4.1.121.Final.

### Why are the changes needed?

To support Java 24+ more easily and bring the latest bug fixes.
- https://netty.io/news/2025/04/23/4-1-120-Final.html
  - netty/netty#14943
  - netty/netty#15035
- https://netty.io/news/2025/04/24/4-1-121-Final.html

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass the CIs.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #50749 from dongjoon-hyun/SPARK-51944.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Kent Yao <[email protected]>
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 10, 2026
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 11, 2026
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 11, 2026
Ko-dany pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 11, 2026
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.

2 participants