Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

Description

Intermittent ByteBuf leaks detected in OrderByDocumentQueryTest trace through ByteToMessageDecoder.channelRead() in RNTBD protocol handlers. Two decoders override channelRead() to conditionally bypass ByteToMessageDecoder's lifecycle management:

  • RntbdRequestDecoder bypasses when resourceOperationType == 0
  • RntbdContextRequestDecoder bypasses when resourceOperationType != 0

When bypassing via context.fireChannelRead(message), buffer ownership transfers to downstream handlers without ByteToMessageDecoder's safety net. If downstream fails to release (exceptions, channel closure, timing), buffers leak.

Changes

Added diagnostic instrumentation to confirm root cause before implementing fixes:

Buffer Touch Points - Netty's native leak detection labels showing execution path in leak reports:

  • Before/after resourceOperationType read
  • Normal vs bypass path decision points
  • Buffer forwarding to downstream handlers

Trace Logging - Reference counts, readable bytes, code paths taken (conditional on logger.isTraceEnabled())

Exception Handlers - Added exceptionCaught() overrides to capture interrupting exceptions

Modified Files

  • RntbdRequestDecoder.java - channelRead() bypass path
  • RntbdContextRequestDecoder.java - channelRead() bypass path
  • RntbdResponseDecoder.java - decode() completeness
  • RntbdContextDecoder.java - decode() with exception handling

Usage

Enable with -Dorg.slf4j.simpleLogger.log.com.azure.cosmos.implementation.directconnectivity.rntbd=TRACE

Leak reports will show:

Recent access records:
#1: ByteBuf.touch("RntbdRequestDecoder.channelRead: bypassing decoder (resourceOperationType == 0)")
#2: ByteBuf.touch("RntbdRequestDecoder.channelRead: forwarding to next handler")

Zero production impact - breadcrumbs only activate with TRACE logging.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG]Investigate possible Netty buffer leak</issue_title>
<issue_description>Describe the bug
Tests in OrderByDocumentQueryTest.java show logs intermittently like below - this is purely based on client implementations - there are two implementations in the sdk/cosmos/azure-cosmos/implementation/directconnectivity and sdk/cosmos/azure-cosmos/implementation/http folders - one using ReactorNettyClient for HTTP and the other using Netty directly with a custom TCP protocol rntbd. Please review the implementations for ByteToMessageDecoder and ChannelHandlers in sdk/cosmos/azure-cosmos for bugs that could explain these intermittent leaks. Most likely due to missing release on discard?

2025-11-13T22:06:20.6753978Z 2025-11-13 22:06:20,657 [reactor-http-epoll-2] ERROR CosmosNettyLeakDetectorFactory - NETTY LEAK (traced) type=ByteBuf records=
2025-11-13T22:06:20.6754181Z
2025-11-13T22:06:20.6754777Z Recent access records:
2025-11-13T22:06:20.6755277Z #1:
2025-11-13T22:06:20.6755856Z io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:300)
2025-11-13T22:06:20.6756462Z io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
2025-11-13T22:06:20.6757072Z io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
2025-11-13T22:06:20.6757662Z io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
2025-11-13T22:06:20.6758257Z io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
2025-11-13T22:06:20.6758815Z io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1519)
2025-11-13T22:06:20.6759392Z io.netty.handler.ssl.SslHandler.decodeNonJdkCompatible(SslHandler.java:1390)
2025-11-13T22:06:20.6760098Z io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1430)
2025-11-13T22:06:20.6760714Z io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530)
2025-11-13T22:06:20.6761301Z io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469)
2025-11-13T22:06:20.6761875Z io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
2025-11-13T22:06:20.6762467Z io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
2025-11-13T22:06:20.6763066Z io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
2025-11-13T22:06:20.6763651Z io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
2025-11-13T22:06:20.6764353Z io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1357)
2025-11-13T22:06:20.6764986Z io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
2025-11-13T22:06:20.6765581Z io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
2025-11-13T22:06:20.6766166Z io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:868)
2025-11-13T22:06:20.6766877Z io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:799)
2025-11-13T22:06:20.6767463Z io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:501)
2025-11-13T22:06:20.6768038Z io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:399)
2025-11-13T22:06:20.6768631Z io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:998)
2025-11-13T22:06:20.6769240Z io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
2025-11-13T22:06:20.6769984Z io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
2025-11-13T22:06:20.6770555Z java.base/java.lang.Thread.run(Thread.java:1474)
2025-11-13T22:06:20.6771061Z #2:
2025-11-13T22:06:20.6771648Z io.netty.buffer.AdvancedLeakAwareByteBuf.skipBytes(AdvancedLeakAwareByteBuf.java:533)
2025-11-13T22:06:20.6772229Z io.netty.handler.codec.http.HttpObjectDecoder.decode(HttpObjectDecoder.java:518)
2025-11-13T22:06:20.6772836Z io.netty.handler.codec.http.HttpClientCodec$Decoder.decode(HttpClientCodec.java:320)
2025-11-13T22:06:20.6773459Z io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530)
2025-11-13T22:06:20.6774045Z io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469)
2025-11-13T22:06:20.6774613Z io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
2025-11-13T22:06:20.6775351Z io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
2025-11-13T22:06:20.677...


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Investigate possible Netty buffer leak in client implementations Add diagnostic breadcrumbs to investigate intermittent Netty ByteBuf leaks in RNTBD decoders Nov 13, 2025
Copilot finished work on behalf of FabianMeiswinkel November 13, 2025 23:16
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