GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Ensure AbstractCoalescingBufferQueue does not end up in inconsistent … · netty/netty@eaf788e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport/src
    • main/java/io/netty/channel
      • AbstractCoalescingBufferQueue.java
    • test/java/io/netty/channel
      • AbstractCoalescingBufferQueueTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/AbstractCoalescingBufferQueue.java
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ public final ByteBuf remove(ByteBufAllocator alloc, int bytes, ChannelPromise ag
154154
ByteBuf toReturn = null;
155155
ByteBuf entryBuffer = null;
156156
int originalBytes = bytes;
157+
Object entry = null;
157158
try {
158159
for (;;) {
159-
Object entry = bufAndListenerPairs.poll();
160+
entry = bufAndListenerPairs.poll();
160161
if (entry == null) {
161162
break;
162163
}
@@ -196,6 +197,18 @@ public final ByteBuf remove(ByteBufAllocator alloc, int bytes, ChannelPromise ag
196197
}
197198
}
198199
} catch (Throwable cause) {
200+
// Always decrement to keep things consistent. We decrement directly here and not in a finally-block
201+
// to ensure that the state is consistent even if it would be accessed via a listener that is
202+
// attached to the promise that we fail below.
203+
decrementReadableBytes(originalBytes - bytes);
204+
205+
// Poll the next element if it's a listener that belongs to the ByteBuf.
206+
entry = bufAndListenerPairs.peek();
207+
if (entry instanceof ChannelFutureListener) {
208+
aggregatePromise.addListener((ChannelFutureListener) entry);
209+
bufAndListenerPairs.poll();
210+
}
211+
199212
safeRelease(entryBuffer);
200213
safeRelease(toReturn);
201214
aggregatePromise.setFailure(cause);
Collapse file

‎transport/src/test/java/io/netty/channel/AbstractCoalescingBufferQueueTest.java‎

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/AbstractCoalescingBufferQueueTest.java
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
import io.netty.buffer.ByteBufAllocator;
2020
import io.netty.buffer.Unpooled;
2121
import io.netty.channel.embedded.EmbeddedChannel;
22+
import io.netty.util.IllegalReferenceCountException;
2223
import io.netty.util.ReferenceCountUtil;
2324
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.function.Executable;
2426

2527
import java.nio.channels.ClosedChannelException;
2628

2729
import static org.junit.jupiter.api.Assertions.assertEquals;
2830
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.junit.jupiter.api.Assertions.assertSame;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
2933
import static org.junit.jupiter.api.Assertions.assertTrue;
3034

3135
public class AbstractCoalescingBufferQueueTest {
@@ -88,4 +92,66 @@ public void operationComplete(ChannelFuture future) {
8892

8993
assertFalse(channel.finish());
9094
}
95+
96+
@Test
97+
public void testKeepStateConsistentOnError() {
98+
final IllegalReferenceCountException exception = new IllegalReferenceCountException();
99+
final EmbeddedChannel channel = new EmbeddedChannel();
100+
final AbstractCoalescingBufferQueue queue = new AbstractCoalescingBufferQueue(channel, 128) {
101+
@Override
102+
protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
103+
// Simulate throwing an IllegalReferenceCountException.
104+
throw exception;
105+
}
106+
107+
@Override
108+
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) {
109+
// Simulate throwing an IllegalReferenceCountException.
110+
throw exception;
111+
}
112+
113+
@Override
114+
protected ByteBuf removeEmptyValue() {
115+
return Unpooled.EMPTY_BUFFER;
116+
}
117+
};
118+
119+
ChannelPromise promise = channel.newPromise();
120+
ByteBuf buffer = Unpooled.buffer().writeLong(0);
121+
queue.add(buffer, promise);
122+
123+
ChannelPromise promise2 = channel.newPromise();
124+
ByteBuf buffer2 = Unpooled.buffer().writeLong(0);
125+
queue.add(buffer2, promise2);
126+
127+
// Size should be 4 as its 2 buffers and 2 listeners.
128+
assertEquals(4, queue.size());
129+
assertEquals(16, queue.readableBytes());
130+
131+
assertThrows(IllegalStateException.class, new Executable() {
132+
@Override
133+
public void execute() throws Throwable {
134+
queue.remove(channel.alloc(), 128, channel.newPromise());
135+
}
136+
});
137+
138+
// This should result in have the first buf and first listener removed from the queue.
139+
assertEquals(2, queue.size());
140+
assertEquals(8, queue.readableBytes());
141+
assertSame(exception, promise.cause());
142+
assertEquals(0, buffer.refCnt());
143+
144+
assertFalse(promise2.isDone());
145+
assertEquals(1, buffer2.refCnt());
146+
ByteBuf removed = queue.remove(channel.alloc(), 128, channel.newPromise());
147+
assertEquals(8, removed.readableBytes());
148+
removed.release();
149+
150+
assertTrue(queue.isEmpty());
151+
assertEquals(0, queue.size());
152+
assertEquals(0, queue.readableBytes());
153+
ByteBuf removed2 = queue.remove(channel.alloc(), 128, channel.newPromise());
154+
assertEquals(0, removed2.readableBytes());
155+
removed2.release();
156+
}
91157
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.