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@7f2b3d0

File tree

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

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

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty5/channel/AbstractCoalescingBufferQueue.java
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ public final Buffer remove(BufferAllocator alloc, int bytes, Promise<Void> aggre
148148
Buffer toReturn = null;
149149
Buffer entryBuffer = null;
150150
int originalBytes = bytes;
151+
Object entry = null;
151152
try {
152153
for (;;) {
153-
Object entry = bufAndListenerPairs.poll();
154+
entry = bufAndListenerPairs.poll();
154155
if (entry == null) {
155156
break;
156157
}
@@ -188,6 +189,18 @@ public final Buffer remove(BufferAllocator alloc, int bytes, Promise<Void> aggre
188189
entryBuffer = null;
189190
}
190191
} catch (Throwable cause) {
192+
// Always decrement to keep things consistent. We decrement directly here and not in a finally-block
193+
// to ensure that the state is consistent even if it would be accessed via a listener that is
194+
// attached to the promise that we fail below.
195+
decrementReadableBytes(originalBytes - bytes);
196+
197+
// Poll the next element if it's a listener that belongs to the ByteBuf.
198+
entry = bufAndListenerPairs.peek();
199+
if (entry instanceof FutureListener) {
200+
aggregatePromise.asFuture().addListener((FutureListener<Void>) entry);
201+
bufAndListenerPairs.poll();
202+
}
203+
191204
SilentDispose.dispose(entryBuffer, logger);
192205
SilentDispose.dispose(toReturn, logger);
193206
aggregatePromise.setFailure(cause);
Collapse file

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

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty5/channel/AbstractCoalescingBufferQueueTest.java
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717

1818
import io.netty5.buffer.Buffer;
1919
import io.netty5.buffer.BufferAllocator;
20+
import io.netty5.util.IllegalReferenceCountException;
2021
import io.netty5.util.Resource;
2122
import io.netty5.channel.embedded.EmbeddedChannel;
2223
import io.netty5.util.concurrent.Future;
24+
25+
import io.netty5.util.concurrent.Promise;
2326
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.function.Executable;
2428

2529
import java.nio.channels.ClosedChannelException;
2630

2731
import static org.junit.jupiter.api.Assertions.assertEquals;
2832
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertSame;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
2935
import static org.junit.jupiter.api.Assertions.assertTrue;
3036

3137
public class AbstractCoalescingBufferQueueTest {
@@ -85,4 +91,66 @@ protected Buffer removeEmptyValue() {
8591

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

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.