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 — Merge branch '4.2' into ocsp_test · netty/netty@31458a9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/main/java/io/netty/buffer
    • AbstractAllocatorEvent.java
    • AdaptivePoolingAllocator.java
    • JfrEventRegistration.java
    • PooledByteBufAllocator.java
  • codec-base/src
    • main/java/io/netty/handler/codec
      • DelimiterBasedFrameDecoder.java
    • test/java/io/netty/handler/codec
      • DelimiterBasedFrameDecoderTest.java
  • codec-http3/src
    • main/java/io/netty/handler/codec/http3
      • QpackDecoder.java
    • test/java/io/netty/handler/codec/http3
      • QpackDecoderTest.java
      • QpackEncoderDecoderTest.java
  • common/src/main/java/io/netty/util/internal
    • Hidden.java
    • PlatformDependent.java
  • handler/src
    • main/java/io/netty/handler/ssl
      • GroupsConverter.java
    • test/java/io/netty/handler/ssl
      • GroupsConverterTest.java
  • jfr-stub/src/main/java/jdk/jfr
    • FlightRecorder.java
    • FlightRecorderListener.java
    • Registered.java
  • transport-blockhound-tests/src/test/java/io/netty/buffer
    • AdaptiveAllocatorBlockHoundTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/AbstractAllocatorEvent.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AbstractAllocatorEvent.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
import jdk.jfr.Enabled;
2121
import jdk.jfr.Event;
2222
import jdk.jfr.Label;
23+
import jdk.jfr.Registered;
2324

2425
/**
2526
* An abstract memory allocator event.
27+
* <p>
28+
* The events are registered by {@link JfrEventRegistration} once a Flight Recorder exists.
2629
*/
2730
@Enabled(false)
31+
@Registered(false)
2832
@Category("Netty")
2933
@SuppressWarnings("Since15")
3034
abstract class AbstractAllocatorEvent extends Event {
Collapse file

‎buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ final class AdaptivePoolingAllocator {
221221
private final FastThreadLocal<MagazineGroup[]> threadLocalGroup;
222222

223223
AdaptivePoolingAllocator(ChunkAllocator chunkAllocator, boolean useCacheForNonEventLoopThreads) {
224+
if (PlatformDependent.isJfrEnabled()) {
225+
JfrEventRegistration.init();
226+
}
224227
this.chunkAllocator = ObjectUtil.checkNotNull(chunkAllocator, "chunkAllocator");
225228
chunkRegistry = new ChunkRegistry();
226229
sizeClassedMagazineGroups = createMagazineGroupSizeClasses(this, false);
Collapse file

‎buffer/src/main/java/io/netty/buffer/JfrEventRegistration.java‎

Copy file name to clipboard
+98Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.buffer;
17+
18+
import io.netty.util.internal.PlatformDependent;
19+
import io.netty.util.internal.logging.InternalLogger;
20+
import io.netty.util.internal.logging.InternalLoggerFactory;
21+
import jdk.jfr.Event;
22+
import jdk.jfr.FlightRecorder;
23+
import jdk.jfr.FlightRecorderListener;
24+
25+
/**
26+
* Registers the allocator events with JFR once a Flight Recorder has been initialized.
27+
* <p>
28+
* The events are {@link jdk.jfr.Registered @Registered(false)}, because registering the first event class sets up
29+
* JFR's metadata repository, which is expensive and pointless in processes that never record. Until an event class
30+
* is registered, JFR does not instrument it, so {@link Event#isEnabled()} is a constant {@code false} and the JIT
31+
* removes the allocation path guards entirely. Registering it later retransforms the class, which deoptimizes the
32+
* guards, exactly like starting a recording in a running process.
33+
* <p>
34+
* JFR notifies listeners while it holds the {@code PlatformRecorder} class lock. It registers its own JDK events
35+
* under the same lock, and the event classes are loaded by this class's initializer and have trivial initializers,
36+
* so registering from the callback does not wait for anything that may in turn wait for that lock.
37+
* <p>
38+
* A Flight Recorder is initialized at most once per JVM, so the listener removes itself after registering the events.
39+
* Otherwise JFR's static listener list would keep this class, and with it the class loader that loaded Netty,
40+
* reachable for the lifetime of the JVM. JFR iterates over a copy of the list, so this is safe from the callback.
41+
* <p>
42+
* If no Flight Recorder is ever initialized, the listener stays in that list, so the class loader that loaded Netty
43+
* can't be unloaded, for example on a webapp redeploy. Holding the listener weakly doesn't help: before JDK 25,
44+
* {@code addListener} stores the caller's {@code AccessControlContext} with it, which references Netty's protection
45+
* domain and so its class loader. Set {@code -Dio.netty.jfr.enabled=false} to avoid this.
46+
* <p>
47+
* This must not be initialized from an event class initializer: registration initializes the event classes.
48+
*/
49+
@SuppressWarnings("Since15")
50+
final class JfrEventRegistration implements FlightRecorderListener {
51+
private static final InternalLogger logger = InternalLoggerFactory.getInstance(JfrEventRegistration.class);
52+
53+
private static final Class<?>[] EVENT_CLASSES = {
54+
AllocateChunkEvent.class,
55+
FreeChunkEvent.class,
56+
AllocateBufferEvent.class,
57+
FreeBufferEvent.class,
58+
ReallocateBufferEvent.class,
59+
};
60+
61+
static {
62+
try {
63+
// Notifies the listener immediately if a recorder already exists, e.g. with -XX:StartFlightRecording.
64+
FlightRecorder.addListener(new JfrEventRegistration());
65+
} catch (Throwable t) {
66+
logger.debug("Failed to register the JFR recorder listener, allocator events are disabled", t);
67+
}
68+
}
69+
70+
private JfrEventRegistration() {
71+
}
72+
73+
/**
74+
* Make sure the allocator events are registered once a Flight Recorder exists. Allocators call this, guarded by
75+
* {@link PlatformDependent#isJfrEnabled()}, before they can emit events. The class initializer does the work.
76+
*/
77+
static void init() {
78+
}
79+
80+
@SuppressWarnings("unchecked")
81+
@Override
82+
public void recorderInitialized(FlightRecorder recorder) {
83+
try {
84+
for (Class<?> eventClass : EVENT_CLASSES) {
85+
FlightRecorder.register((Class<? extends Event>) eventClass);
86+
}
87+
} catch (Throwable t) {
88+
// Don't break the recording that is being started.
89+
logger.debug("Failed to register the allocator JFR events", t);
90+
} finally {
91+
try {
92+
FlightRecorder.removeListener(this);
93+
} catch (Throwable t) {
94+
logger.debug("Failed to remove the JFR recorder listener", t);
95+
}
96+
}
97+
}
98+
}
Collapse file

‎buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectA
272272
int smallCacheSize, int normalCacheSize,
273273
boolean useCacheForAllThreads, int directMemoryCacheAlignment) {
274274
super(preferDirect);
275+
if (PlatformDependent.isJfrEnabled()) {
276+
JfrEventRegistration.init();
277+
}
275278
threadCache = new PoolThreadLocalCache(useCacheForAllThreads);
276279
this.smallCacheSize = smallCacheSize;
277280
this.normalCacheSize = normalCacheSize;
Collapse file

‎codec-base/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java‎

Copy file name to clipboardExpand all lines: codec-base/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java
+25-5Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,14 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Except
314314

315315
return frame;
316316
} else {
317+
// The buffer may end with the first bytes of a delimiter whose remaining bytes were not received yet.
318+
// They are neither counted as part of the frame nor discarded.
319+
int frameBytes = readableBytes - partialDelimiterLength(buffer);
317320
if (!discardingTooLongFrame) {
318-
if (readableBytes > maxFrameLength) {
321+
if (frameBytes > maxFrameLength) {
319322
// Discard the content of the buffer until a delimiter is found.
320-
tooLongFrameLength = readableBytes;
321-
buffer.skipBytes(readableBytes);
323+
tooLongFrameLength = frameBytes;
324+
buffer.skipBytes(frameBytes);
322325
discardingTooLongFrame = true;
323326
scanOffset = 0;
324327
if (failFast) {
@@ -331,14 +334,31 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Except
331334
}
332335
} else {
333336
// Still discarding the buffer since a delimiter is not found.
334-
tooLongFrameLength += buffer.readableBytes();
335-
buffer.skipBytes(buffer.readableBytes());
337+
tooLongFrameLength += frameBytes;
338+
buffer.skipBytes(frameBytes);
336339
scanOffset = 0;
337340
}
338341
return null;
339342
}
340343
}
341344

345+
/**
346+
* Returns the length of the longest suffix of the readable bytes that is the beginning of a delimiter.
347+
*/
348+
private int partialDelimiterLength(ByteBuf buffer) {
349+
int readableBytes = buffer.readableBytes();
350+
int partialLength = 0;
351+
for (ByteBuf delim: delimiters) {
352+
for (int length = Math.min(delim.capacity() - 1, readableBytes); length > partialLength; length--) {
353+
if (ByteBufUtil.equals(buffer, buffer.writerIndex() - length, delim, 0, length)) {
354+
partialLength = length;
355+
break;
356+
}
357+
}
358+
}
359+
return partialLength;
360+
}
361+
342362
private void fail(long frameLength) {
343363
if (frameLength > 0) {
344364
throw new TooLongFrameException(
Collapse file

‎codec-base/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-base/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.junit.jupiter.api.Assertions.assertEquals;
3131
import static org.junit.jupiter.api.Assertions.assertFalse;
3232
import static org.junit.jupiter.api.Assertions.assertNull;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
3334
import static org.junit.jupiter.api.Assertions.assertTrue;
3435

3536
public class DelimiterBasedFrameDecoderTest {
@@ -131,6 +132,65 @@ public void testDecode() throws Exception {
131132
buf2.release();
132133
}
133134

135+
@Test
136+
public void testMaxLengthFrameWithDelimiterSplitAcrossReads() {
137+
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(4, ascii("##")));
138+
// The frame has maxFrameLength bytes, the first delimiter byte is in the same read.
139+
ch.writeInbound(ascii("ABCD#"));
140+
ch.writeInbound(ascii("#ok##"));
141+
assertFrame(ch, "ABCD");
142+
assertFrame(ch, "ok");
143+
assertNull(ch.readInbound());
144+
assertFalse(ch.finish());
145+
}
146+
147+
@Test
148+
public void testDelimiterSplitAcrossReadsWhileDiscarding() {
149+
final EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(4, ascii("##")));
150+
// failFast: the exception is raised as soon as the frame is known to be too long.
151+
assertThrows(TooLongFrameException.class, () -> ch.writeInbound(ascii("AAAAAAAAAA#")));
152+
// The rest of the delimiter ends the discarded frame, the next frame must not be lost.
153+
ch.writeInbound(ascii("#ok##"));
154+
assertFrame(ch, "ok");
155+
assertNull(ch.readInbound());
156+
assertFalse(ch.finish());
157+
}
158+
159+
@Test
160+
public void testDelimiterSplitAcrossReadsWhileDiscardingNoFailFast() {
161+
final EmbeddedChannel ch = new EmbeddedChannel(
162+
new DelimiterBasedFrameDecoder(4, true, false, ascii("##")));
163+
ch.writeInbound(ascii("AAAAAAAAAA#"));
164+
// The exception is raised once the delimiter that ends the too long frame was read.
165+
assertThrows(TooLongFrameException.class, () -> ch.writeInbound(ascii("#ok##")));
166+
ch.writeInbound(ascii("next##"));
167+
assertFrame(ch, "ok");
168+
assertFrame(ch, "next");
169+
assertNull(ch.readInbound());
170+
assertFalse(ch.finish());
171+
}
172+
173+
@Test
174+
public void testMaxFrameLengthEnforcedWithDelimiterSplitAcrossReads() {
175+
final EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(4, ascii("##")));
176+
// One byte more than maxFrameLength.
177+
assertThrows(TooLongFrameException.class, () -> ch.writeInbound(ascii("ABCDE#")));
178+
ch.writeInbound(ascii("#ok##"));
179+
assertFrame(ch, "ok");
180+
assertNull(ch.readInbound());
181+
assertFalse(ch.finish());
182+
}
183+
184+
private static ByteBuf ascii(String s) {
185+
return Unpooled.copiedBuffer(s, CharsetUtil.US_ASCII);
186+
}
187+
188+
private static void assertFrame(EmbeddedChannel ch, String expected) {
189+
ByteBuf frame = ch.readInbound();
190+
assertEquals(expected, frame.toString(CharsetUtil.US_ASCII));
191+
frame.release();
192+
}
193+
134194
/**
135195
* A multi-byte, non-line delimiter that straddles the resumed-scan boundary must still be
136196
* found. This guards the scan-offset optimization added to fix the quadratic rescanning of
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/QpackDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/QpackDecoder.java
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ public boolean decode(QpackAttributes qpackAttributes, long streamId, ByteBuf in
150150
assert qpackAttributes.decoderStreamAvailable();
151151

152152
stateSyncStrategy.sectionAcknowledged(requiredInsertCount);
153+
// A Section Acknowledgment also acknowledges all insertions up to the Required Insert Count, so the next
154+
// Insert Count Increment must not include them again.
155+
// See https://www.rfc-editor.org/rfc/rfc9204.html#section-4.4.1
156+
if (requiredInsertCount > lastAckInsertCount) {
157+
lastAckInsertCount = requiredInsertCount;
158+
}
153159
final ByteBuf sectionAck = qpackAttributes.decoderStream().alloc().buffer(8);
154160
encodePrefixedInteger(sectionAck, (byte) 0b1000_0000, 7, streamId);
155161
closeOnFailure(qpackAttributes.decoderStream().writeAndFlush(sectionAck));
@@ -535,7 +541,9 @@ private void sendInsertCountIncrementIfRequired(QuicStreamChannel qpackDecoderSt
535541
throw BLOCKED_STREAM_RESUMPTION_FAILED;
536542
}
537543
}
538-
if (stateSyncStrategy.entryAdded(insertCount)) {
544+
if (stateSyncStrategy.entryAdded(insertCount) &&
545+
// The insertions might have been acknowledged by a Section Acknowledgment already.
546+
insertCount > lastAckInsertCount) {
539547
// https://www.rfc-editor.org/rfc/rfc9204.html#name-insert-count-increment
540548
// 0 1 2 3 4 5 6 7
541549
// +---+---+---+---+---+---+---+---+

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.