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 — Auto-port 4.1: Reconcile `AbstractCoalescingBufferQueue` readableByte… · netty/netty@26bb273

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • DefaultHttp2ConnectionEncoder.java
    • test/java/io/netty/handler/codec/http2
      • DefaultHttp2ConnectionEncoderTest.java
  • transport/src
    • main/java/io/netty/channel
      • AbstractCoalescingBufferQueue.java
    • test/java/io/netty/channel
      • CoalescingBufferQueueTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.netty.channel.CoalescingBufferQueue;
2323
import io.netty.handler.codec.http.HttpStatusClass;
2424
import io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator;
25+
import io.netty.util.ReferenceCountUtil;
2526

2627
import java.util.ArrayDeque;
2728
import java.util.Queue;
@@ -30,6 +31,7 @@
3031
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
3132
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
3233
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
34+
import static io.netty.handler.codec.http2.Http2Exception.streamError;
3335
import static io.netty.util.internal.ObjectUtil.checkNotNull;
3436
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
3537
import static java.lang.Integer.MAX_VALUE;
@@ -501,6 +503,20 @@ public void write(ChannelHandlerContext ctx, int allowedBytes) {
501503
ByteBuf toWrite = queue.remove(writableData, writePromise);
502504
dataSize = queue.readableBytes();
503505

506+
// The queue reported writableData readable bytes but produced fewer: a queued buffer was released or
507+
// consumed while still referenced by the queue, so the stream's data is corrupted. Fail the stream.
508+
int producedBytes = toWrite.readableBytes();
509+
if (producedBytes < writableData) {
510+
ReferenceCountUtil.safeRelease(toWrite);
511+
// Set dataSize and padding to 0 to signal that the whole frame was consumed, so it is removed and
512+
// its bytes are returned to flow control (matching the error path above).
513+
padding = dataSize = 0;
514+
writePromise.tryFailure(streamError(stream.id(), INTERNAL_ERROR,
515+
"Stream %d flow-controlled queue produced %d bytes but reported %d",
516+
stream.id(), producedBytes, writableData));
517+
return;
518+
}
519+
504520
// Determine how much padding to write.
505521
int writablePadding = min(allowedBytes - writableData, padding);
506522
padding -= writablePadding;
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoderTest.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,31 @@ private void assertSplitPaddingOnEmptyBuffer(ByteBuf data) throws Exception {
388388
assertTrue(p.isSuccess());
389389
}
390390

391+
@Test
392+
public void writeDataFailsStreamWhenFlowControlledQueueUnderDelivers() throws Exception {
393+
createStream(STREAM_ID, false);
394+
ByteBuf data = wrappedBuffer(new byte[10]);
395+
ChannelPromise promise = newPromise();
396+
// Include padding so we also cover that dataSize and padding are reset on failure.
397+
encoder.writeData(ctx, STREAM_ID, data, 10, false, promise);
398+
FlowControlled fc = payloadCaptor.getValue();
399+
assertEquals(20, fc.size());
400+
401+
// Simulate a queued buffer being consumed out from under the queue
402+
data.skipBytes(data.readableBytes());
403+
404+
fc.write(ctx, 20);
405+
406+
// Expect the stream to fail rather than emitting any frames
407+
assertFalse(promise.isSuccess());
408+
assertInstanceOf(Http2Exception.class, promise.cause());
409+
assertEquals(Http2Error.INTERNAL_ERROR, ((Http2Exception) promise.cause()).error());
410+
assertTrue(writtenData.isEmpty());
411+
// The frame reports as fully consumed so it is removed and its bytes return to flow control.
412+
assertEquals(0, fc.size());
413+
assertEquals(0, data.refCnt());
414+
}
415+
391416
@Test
392417
public void headersWriteForUnknownStreamShouldCreateStream() throws Exception {
393418
writeAllFlowControlledFrames();
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
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public final ByteBuf removeFirst(ChannelPromise aggregatePromise) {
126126
aggregatePromise.addListener((ChannelFutureListener) entry);
127127
bufAndListenerPairs.poll();
128128
}
129+
reconcileReadableBytes();
129130
return result;
130131
}
131132

@@ -146,7 +147,7 @@ public final ByteBuf remove(ByteBufAllocator alloc, int bytes, ChannelPromise ag
146147

147148
// Use isEmpty rather than readableBytes==0 as we may have a promise associated with an empty buffer.
148149
if (bufAndListenerPairs.isEmpty()) {
149-
assert readableBytes == 0;
150+
reconcileReadableBytes();
150151
return removeEmptyValue();
151152
}
152153
bytes = Math.min(bytes, readableBytes);
@@ -215,6 +216,7 @@ public final ByteBuf remove(ByteBufAllocator alloc, int bytes, ChannelPromise ag
215216
throwException(cause);
216217
}
217218
decrementReadableBytes(originalBytes - bytes);
219+
reconcileReadableBytes();
218220
return toReturn;
219221
}
220222

@@ -289,6 +291,7 @@ public final void writeAndRemoveAll(ChannelHandlerContext ctx) {
289291
}
290292
}
291293
}
294+
reconcileReadableBytes();
292295
if (pending != null) {
293296
throw new IllegalStateException(pending);
294297
}
@@ -402,6 +405,7 @@ private void releaseAndCompleteAll(ChannelFuture future) {
402405
}
403406
}
404407
}
408+
reconcileReadableBytes();
405409
if (pending != null) {
406410
throw new IllegalStateException(pending);
407411
}
@@ -426,6 +430,22 @@ private void decrementReadableBytes(int decrement) {
426430
}
427431
}
428432

433+
/**
434+
* Resets readableBytes to 0 when the queue is empty. They can only diverge if a queued buffer was released
435+
* or consumed while still referenced by the queue (similar to a reference-counting bug) after it was added,
436+
* which would otherwise make remove(...) return empty buffers forever. Logged at error level because it
437+
* always indicates a bug that needs to be found.
438+
* See https://github.com/netty/netty/issues/16946
439+
*/
440+
private void reconcileReadableBytes() {
441+
if (readableBytes != 0 && bufAndListenerPairs.isEmpty()) {
442+
logger.error("readableBytes is {} but the queue is empty: a queued buffer was released or consumed " +
443+
"while still referenced by the queue. This indicates a bug in the code that produced the " +
444+
"buffer. Resetting readableBytes to 0.", readableBytes);
445+
decrementReadableBytes(readableBytes);
446+
}
447+
}
448+
429449
private static ChannelFutureListener toChannelFutureListener(ChannelPromise promise) {
430450
return promise.isVoid() ? null : new DelegatingChannelPromiseNotifier(promise);
431451
}
Collapse file

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

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/CoalescingBufferQueueTest.java
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,93 @@ public void testEmptyBuffersAreCoalesced() {
236236
assertEquals(0, empty.refCnt());
237237
}
238238

239+
// If a queued buffer's readable bytes shrink after enqueue (released or consumed out from under the queue),
240+
// the queue must still report 0 readable bytes once drained.
241+
// See https://github.com/netty/netty/issues/16946
242+
@Test
243+
public void testReadableBytesResetWhenQueuedBufferConsumedExternally() {
244+
// Not used in this test.
245+
cat.release();
246+
mouse.release();
247+
248+
ByteBuf buffer = Unpooled.buffer().writeZero(292);
249+
writeQueue.add(buffer);
250+
assertEquals(292, writeQueue.readableBytes());
251+
252+
// Consume the buffer out from under the queue (e.g. a pooled buffer released and reused).
253+
buffer.skipBytes(buffer.readableBytes());
254+
255+
ByteBuf removed = writeQueue.remove(Integer.MAX_VALUE, newPromise());
256+
assertFalse(removed.isReadable());
257+
removed.release();
258+
assertQueueSize(0, true);
259+
260+
// A subsequent remove must not resurrect phantom readable bytes.
261+
ByteBuf removedAgain = writeQueue.remove(Integer.MAX_VALUE, newPromise());
262+
assertFalse(removedAgain.isReadable());
263+
removedAgain.release();
264+
assertQueueSize(0, true);
265+
}
266+
267+
@Test
268+
public void testReadableBytesResetWhenReleaseAndFailAllSeesConsumedBuffer() {
269+
cat.release();
270+
mouse.release();
271+
272+
ByteBuf buffer = Unpooled.buffer().writeZero(292);
273+
writeQueue.add(buffer);
274+
assertEquals(292, writeQueue.readableBytes());
275+
276+
buffer.skipBytes(buffer.readableBytes());
277+
writeQueue.releaseAndFailAll(new RuntimeException());
278+
279+
assertQueueSize(0, true);
280+
assertEquals(0, buffer.refCnt());
281+
}
282+
283+
@Test
284+
public void testReadableBytesResetWhenWriteAndRemoveAllSeesConsumedBuffer() {
285+
cat.release();
286+
mouse.release();
287+
288+
EmbeddedChannel ch = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
289+
@Override
290+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
291+
ReferenceCountUtil.release(msg);
292+
promise.setSuccess();
293+
}
294+
}, new ChannelHandlerAdapter() { });
295+
CoalescingBufferQueue queue = new CoalescingBufferQueue(ch);
296+
297+
ByteBuf buffer = Unpooled.buffer().writeZero(292);
298+
queue.add(buffer);
299+
assertEquals(292, queue.readableBytes());
300+
301+
buffer.skipBytes(buffer.readableBytes());
302+
queue.writeAndRemoveAll(ch.pipeline().lastContext());
303+
304+
assertTrue(queue.isEmpty());
305+
assertEquals(0, queue.readableBytes());
306+
assertFalse(ch.finish());
307+
}
308+
309+
@Test
310+
public void testReadableBytesResetWhenRemoveFirstSeesConsumedBuffer() {
311+
cat.release();
312+
mouse.release();
313+
314+
ByteBuf buffer = Unpooled.buffer().writeZero(292);
315+
writeQueue.add(buffer);
316+
assertEquals(292, writeQueue.readableBytes());
317+
318+
buffer.skipBytes(buffer.readableBytes());
319+
ByteBuf removed = writeQueue.removeFirst(newPromise());
320+
assertSame(buffer, removed);
321+
removed.release();
322+
323+
assertQueueSize(0, true);
324+
}
325+
239326
@Test
240327
public void testMerge() {
241328
writeQueue.add(cat, catPromise);

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.