Skip to content

Commit 8d7c113

Browse files
authored
Add ReferenceCountUtil.touch(...) calls before we store messages into… (#13838)
… a queue to make debugging leaks easier Motivation: We should touch our messages when we take over ownership, this allows easier debugging of buffer leaks. Modifications: Let's add touch call before enqueue into internal datastructure Result: Easier to debug buffer leaks
1 parent f254226 commit 8d7c113

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

transport/src/main/java/io/netty/channel/AbstractCoalescingBufferQueue.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public final void addFirst(ByteBuf buf, ChannelPromise promise) {
5858
}
5959

6060
private void addFirst(ByteBuf buf, ChannelFutureListener listener) {
61+
// Touch the message to make it easier to debug buffer leaks.
62+
buf.touch();
63+
6164
if (listener != null) {
6265
bufAndListenerPairs.addFirst(listener);
6366
}
@@ -91,6 +94,9 @@ public final void add(ByteBuf buf, ChannelPromise promise) {
9194
* @param listener to notify when all the bytes have been consumed and written, can be {@code null}.
9295
*/
9396
public final void add(ByteBuf buf, ChannelFutureListener listener) {
97+
// Touch the message to make it easier to debug buffer leaks.
98+
buf.touch();
99+
94100
// buffers are added before promises so that we naturally 'consume' the entire buffer during removal
95101
// before we complete it's promise.
96102
bufAndListenerPairs.add(buf);

transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ public void addMessage(Object msg, int size, ChannelPromise promise) {
125125
unflushedEntry = entry;
126126
}
127127

128+
// Touch the message to make it easier to debug buffer leaks.
129+
130+
// this save both checking against the ReferenceCounted interface
131+
// and makes better use of virtual calls vs interface ones
132+
if (msg instanceof AbstractReferenceCountedByteBuf) {
133+
((AbstractReferenceCountedByteBuf) msg).touch();
134+
} else {
135+
ReferenceCountUtil.touch(msg);
136+
}
137+
128138
// increment pending bytes after adding message to the unflushed arrays.
129139
// See https://github.com/netty/netty/issues/1619
130140
incrementPendingOutboundBytes(entry.pendingSize, false);

transport/src/main/java/io/netty/channel/PendingWriteQueue.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.channel;
1717

18+
import io.netty.buffer.AbstractReferenceCountedByteBuf;
1819
import io.netty.util.ReferenceCountUtil;
1920
import io.netty.util.concurrent.EventExecutor;
2021
import io.netty.util.concurrent.PromiseCombiner;
@@ -119,6 +120,15 @@ public void add(Object msg, ChannelPromise promise) {
119120
size ++;
120121
bytes += messageSize;
121122
tracker.incrementPendingOutboundBytes(write.size);
123+
// Touch the message to make it easier to debug buffer leaks.
124+
125+
// this save both checking against the ReferenceCounted interface
126+
// and makes better use of virtual calls vs interface ones
127+
if (msg instanceof AbstractReferenceCountedByteBuf) {
128+
((AbstractReferenceCountedByteBuf) msg).touch();
129+
} else {
130+
ReferenceCountUtil.touch(msg);
131+
}
122132
}
123133

124134
/**

0 commit comments

Comments
 (0)