GeezerVison is showing github.com — Netty Issue: GZIP trailer corruption with large payloads after upgrading from 4.2.9 to 4.2.11 · Issue #16656 · netty/netty
Netty Issue: GZIP trailer corruption with large payloads after upgrading from 4.2.9 to 4.2.11 #16656DescriptionTitle
ZipException: Corrupt GZIP trailer for large GZIP-compressed HTTP request bodies after upgrading from 4.2.9.Final to 4.2.11.Final
Netty version
- Affected: 4.2.11.Final (also likely 4.2.10.Final)
- Working: 4.2.9.Final
Java version
- OpenJDK 21 (also observed on JDK 17)
Deployment environment
- Kubernetes (containerized microservices)
- Linux containers
Summary
After upgrading Netty from 4.2.9.Final to 4.2.11.Final, we started observing java.util.zip.ZipException: Corrupt GZIP trailer errors when processing large GZIP-compressed HTTP POST request bodies (≥ 3MB uncompressed). Smaller payloads (< 1MB) are not affected. The exact same client payloads work without any issues on 4.2.9.Final.
The issue is intermittent — it does not reproduce on every request, but is consistently observed under production load with larger payloads. Reverting to 4.2.9.Final immediately resolves the issue.
Stack trace
java.io.UncheckedIOException: java.util.zip.ZipException: Corrupt GZIP trailer
at java.base/java.io.BufferedReader$1.hasNext(BufferedReader.java:684)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:132)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1939)
at java.base/java.util.stream.ReferencePipeline$Head.forEachOrdered(ReferencePipeline.java:772)
...
Caused by: java.util.zip.ZipException: Corrupt GZIP trailer
at java.base/java.util.zip.GZIPInputStream.readTrailer(GZIPInputStream.java:257)
at java.base/java.util.zip.GZIPInputStream.read(GZIPInputStream.java:151)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor$FinishableGZIPInputStream.read(GZIPDecodingInterceptor.java:74)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:350)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:393)
at java.base/sun.nio.cs.StreamDecoder.lockedRead(StreamDecoder.java:217)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:171)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:188)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:160)
at java.base/java.io.BufferedReader.implReadLine(BufferedReader.java:370)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:347)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:436)
at java.base/java.io.BufferedReader$1.hasNext(BufferedReader.java:681)
...
The outer exception is dispatched by RESTEasy's Netty4 integration:
org.jboss.resteasy.spi.UnhandledException: java.io.UncheckedIOException: java.util.zip.ZipException: Corrupt GZIP trailer
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:344)
...
at org.jboss.resteasy.plugins.server.netty.RequestHandler.channelRead0(RequestHandler.java:51)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
...
Request processing flow
- Client sends HTTP POST with
Content-Encoding: gzip header and a GZIP-compressed JSON body
- Netty receives the request through its pipeline (
HttpServerCodec → HttpObjectAggregator)
- RESTEasy Netty4 adapter (
resteasy-netty4) reads the request body from the Netty ByteBuf as an InputStream
- A JAX-RS
ContainerRequestFilter wraps the entity stream with GZIPInputStream for decompression
- A downstream filter reads the decompressed body via
BufferedReader.lines()
GZIPInputStream.readTrailer() fails with Corrupt GZIP trailer — the last 8 bytes (CRC32 + ISIZE) are corrupted
Key observations
| Observation |
Detail |
| Affected payload size |
≥ 1MB uncompressed (compressed size ~200KB+) |
| Unaffected payload size |
< 1MB uncompressed |
| Frequency |
Intermittent under production load, not reproducible on localhost |
| 4.2.9.Final |
Works correctly — zero errors |
| 4.2.11.Final |
Consistently produces errors for large payloads |
| Client payload |
Verified correct — same clients, same payloads, same compression logic |
| Increasing decompression buffer size |
No effect (tried up to 12MB) |
| Reverting to 4.2.9.Final |
Immediately resolves the issue |
Suspected root cause
The following PRs merged between 4.2.9 and 4.2.11 changed buffer handling and are the most likely cause:
| PR |
Change |
Milestone |
| #15736 |
AdaptiveByteBuf.setBytes optimization — new absolutePut path |
4.2.10 |
| #16058 |
Allocation-free AdaptiveByteBuf.setBytes(byte[]) using root parent NIO buffer |
4.2.10 |
| #15741 |
Adaptive allocator thread-local performance overhaul |
4.2.11 |
| #16053 |
Buddy allocation for large buffers |
4.2.11 |
These changes affect how Netty copies data internally within AdaptiveByteBuf. If there is an off-by-one error or offset miscalculation in the setBytes/absolutePut fast path, the last bytes of the GZIP stream (the 8-byte trailer containing CRC32 and ISIZE) could be silently corrupted as the request body passes through Netty's HttpObjectAggregator pipeline — before it reaches the application layer.
The fact that:
- Only large payloads are affected (requiring multiple buffer allocations/copies)
- The corruption is in the GZIP trailer (the last 8 bytes of the stream)
- The issue is intermittent (depends on buffer allocation patterns)
- It does not reproduce on localhost (single-buffer pass, no real TCP fragmentation)
...strongly suggests a buffer boundary/offset issue in the adaptive allocator's large buffer handling path.
Minimal reproduction context
The issue occurs when:
- A Netty-based HTTP server receives a GZIP-compressed POST body ≥ 3MB uncompressed
- The body is read from the
ByteBuf as an InputStream (e.g., via ByteBufInputStream)
- The
InputStream is wrapped with java.util.zip.GZIPInputStream for decompression
- The decompressed content is read lazily (e.g., via
BufferedReader.lines())
- Under real network conditions (TCP fragmentation, multiple Netty read cycles)
We were unable to reproduce in a standalone unit test on localhost because the data arrives in a single contiguous buffer, bypassing the multi-chunk aggregation path where the corruption occurs.
Please check and share your comments on this observation Activityfranz1981 commented on Apr 16, 2026 You cannot try reverting singularly the commit you found to be potentially dangerous?
Since you are the only one with the reproducer... nsnmurthyk commented on Apr 16, 2026 As it is impacting the production stack so no other option for holding the application with errors so we promoted our application with 4.2.9 version dreamlike-ocean commented on Apr 26, 2026 I think you can upgrade to 4.2.12 to fix it
In jdk26 Version matrix:
- 4.2.10.Final: PASS
- 4.2.11.Final: FAIL
- 4.2.12.Final: PASS
The regression appears to come from:
In netty-4.2.11.Final, the fast path used by readByte() contains:
private Component findComponentForRead(int offset) {
int cc = componentCount;
for (int next = lastAccessedIndex + 1; next < cc; next++) {
Component c = components[next];
if (c.endOffset > c.offset) {
lastAccessed = c;
lastAccessedIndex = next;
return c;
}
}
return findIt(offset);
}
In 4.2.11, CompositeByteBuf.readByte() can pick the wrong component during sequential reads: instead of reading from the component that contains the current readerIndex, it jumps to a later non-empty component. That means trailer verification reads the wrong bytes, which surfaces as Corrupt GZIP trailer.
CompositeByteBuf
[ header ][ body-1 ][ body-2 ][ trailer ]
0..9 10..109 110..209 210..217
readerIndex = 210
Should read from: trailer component
4.2.11 fast path:
lastAccessed = body-1
|
v
incorrectly jumps to the “next non-empty component”
|
v
body-2
So it effectively does:
use readerIndex=210
but read from the wrong component
=> wrong trailer bytes
=> Corrupt GZIP trailer
Also, 4.2.12.Final no longer reproduces because the problematic optimization was reverted in:
reproducer:
public final class GzipBug {
private static final int GZIP_HEADER_SIZE = 10;
private static final int GZIP_TRAILER_SIZE = 8;
private static final int PAYLOAD_SIZE = 2048;
private static final int SEED = 0;
private static final int BODY_CHUNK_SIZE = 17;
private static final int GZIP_INPUT_BUFFER_SIZE = 8;
public static void main(String[] args) throws Exception {
byte[] payload = new byte[PAYLOAD_SIZE];
new Random(SEED).nextBytes(payload);
byte[] gzipBytes = gzip(payload);
try (InputStream nettyIn = new ByteBufInputStream(compositeOf(gzipBytes), true);
GZIPInputStream gzipIn = new GZIPInputStream(nettyIn, GZIP_INPUT_BUFFER_SIZE)) {
byte[] actual = gzipIn.readAllBytes();
if (!Arrays.equals(payload, actual)) {
throw new AssertionError("payload mismatch at index " + Arrays.mismatch(payload, actual));
}
}
System.out.println("PASS");
}
private static CompositeByteBuf compositeOf(byte[] gzipBytes) {
CompositeByteBuf composite = Unpooled.compositeBuffer();
addComponent(composite, gzipBytes, 0, GZIP_HEADER_SIZE);
int bodyEnd = gzipBytes.length - GZIP_TRAILER_SIZE;
for (int offset = GZIP_HEADER_SIZE; offset < bodyEnd; offset += BODY_CHUNK_SIZE) {
int length = Math.min(BODY_CHUNK_SIZE, bodyEnd - offset);
addComponent(composite, gzipBytes, offset, length);
}
addComponent(composite, gzipBytes, bodyEnd, GZIP_TRAILER_SIZE);
return composite;
}
private static void addComponent(CompositeByteBuf composite, byte[] gzipBytes, int offset, int length) {
// Each component has an independent backing array, but bytes are copied into their absolute positions.
// This keeps the normal composite reads valid while exposing wrong component selection during trailer reads.
byte[] backing = new byte[gzipBytes.length];
System.arraycopy(gzipBytes, offset, backing, offset, length);
composite.addComponent(true, Unpooled.wrappedBuffer(backing, offset, length));
}
private static byte[] gzip(byte[] payload) throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOut = new GZIPOutputStream(output)) {
gzipOut.write(payload);
}
return output.toByteArray();
}
}yawkat commented on Apr 26, 2026 Thanks for tracking it down. This was fixed in #16548 as well. Marking as stale. nsnmurthyk commented on Apr 27, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|