[HTTP2] Fix memory leak while writing empty data frame with padding (#11633)

Motivation:

There is a memory leak while writing empty data frame with padding.

The empty data frame was occurred because we are running a proxy server
built by netty, and found that google services always sent data frames
followed by an empty data frame.

Modifications:

Calls the ctx.write even the payload is empty.

Result:

Fix memory leak.

Co-authored-by: Norman Maurer <norman_maurer@apple.com>
This commit is contained in:
Chun-Han, Hsiao 2021-08-31 18:41:49 +08:00 committed by Norman Maurer
parent 14102eb98c
commit 8cc34e5c4c
2 changed files with 21 additions and 1 deletions

View File

@ -217,7 +217,7 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize
ctx.write(frameHeader2).cascadeTo(promiseAggregator.newPromise()); ctx.write(frameHeader2).cascadeTo(promiseAggregator.newPromise());
// Write the payload. // Write the payload.
if (frameDataBytes != 0 && data != null) { // Make sure Data is not null if (data != null) { // Make sure Data is not null
if (remainingData == 0) { if (remainingData == 0) {
ByteBuf lastFrame = data.readSlice(frameDataBytes); ByteBuf lastFrame = data.readSlice(frameDataBytes);
data = null; data = null;

View File

@ -151,6 +151,26 @@ public class DefaultHttp2FrameWriterTest {
assertEquals(expectedOutbound, outbound); assertEquals(expectedOutbound, outbound);
} }
@Test
public void writeEmptyDataWithPadding() {
int streamId = 1;
ByteBuf payloadByteBuf = Unpooled.buffer();
frameWriter.writeData(ctx, streamId, payloadByteBuf, 2, true);
assertEquals(0, payloadByteBuf.refCnt());
byte[] expectedFrameBytes = {
(byte) 0x00, (byte) 0x00, (byte) 0x02, // payload length
(byte) 0x00, // payload type
(byte) 0x09, // flags
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, // stream id
(byte) 0x01, (byte) 0x00, // padding
};
expectedOutbound = Unpooled.copiedBuffer(expectedFrameBytes);
assertEquals(expectedOutbound, outbound);
}
/** /**
* Test large headers that exceed {@link DefaultHttp2FrameWriter#maxFrameSize()} * Test large headers that exceed {@link DefaultHttp2FrameWriter#maxFrameSize()}
* the remaining headers will be sent in a CONTINUATION frame * the remaining headers will be sent in a CONTINUATION frame