Skip to content

Commit

Permalink
Fix ByteBuf leak in Http2ControlFrameLimitEncoderTest (netty#9466)
Browse files Browse the repository at this point in the history
Motivation:

We recently introduced Http2ControlFrameLimitEncoderTest which did not correctly notify the goAway promises and so leaked buffers.

Modifications:

Correctly notify all promises and so release the debug data.

Result:

Fixes leak in HTTP2 test
  • Loading branch information
normanmaurer authored Aug 14, 2019
1 parent 1fa7a5e commit 1cce3b1
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import org.mockito.stubbing.Answer;


import java.util.ArrayDeque;
import java.util.Queue;

import static io.netty.handler.codec.http2.Http2CodecUtil.*;
import static io.netty.handler.codec.http2.Http2Error.CANCEL;
import static io.netty.handler.codec.http2.Http2Error.ENHANCE_YOUR_CALM;
Expand Down Expand Up @@ -71,6 +74,8 @@ public class Http2ControlFrameLimitEncoderTest {

private int numWrites;

private Queue<ChannelPromise> goAwayPromises = new ArrayDeque<ChannelPromise>();

/**
* Init fields and do mocking.
*/
Expand Down Expand Up @@ -116,7 +121,9 @@ public ChannelFuture answer(InvocationOnMock invocationOnMock) {
@Override
public ChannelFuture answer(InvocationOnMock invocationOnMock) {
ReferenceCountUtil.release(invocationOnMock.getArgument(3));
return handlePromise(invocationOnMock, 4);
ChannelPromise promise = invocationOnMock.getArgument(4);
goAwayPromises.offer(promise);
return promise;
}
});
Http2Connection connection = new DefaultHttp2Connection(false);
Expand Down Expand Up @@ -168,6 +175,16 @@ private ChannelPromise handlePromise(InvocationOnMock invocationOnMock, int prom
public void teardown() {
// Close and release any buffered frames.
encoder.close();

// Notify all goAway ChannelPromise instances now as these will also release the retained ByteBuf for the
// debugData.
for (;;) {
ChannelPromise promise = goAwayPromises.poll();
if (promise == null) {
break;
}
promise.setSuccess();
}
}

@Test
Expand Down Expand Up @@ -254,12 +271,6 @@ private void verifyFlushAndClose(int invocations, boolean failed) {
}
}

private static void assertWriteFailure(ChannelFuture future) {
Http2Exception exception = (Http2Exception) future.cause();
assertEquals(ShutdownHint.HARD_SHUTDOWN, exception.shutdownHint());
assertEquals(Http2Error.ENHANCE_YOUR_CALM, exception.error());
}

private ChannelPromise newPromise() {
return new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
}
Expand Down

0 comments on commit 1cce3b1

Please sign in to comment.