Skip to content

Commit

Permalink
Ensure buffer is released even if future fails (java-native-access#320)
Browse files Browse the repository at this point in the history
Motivation:

We should release in a finally blog so we don't leak on failure

Modifications:

Move release() to finally block.

Result:

No more leaks when write fails
  • Loading branch information
normanmaurer authored Aug 16, 2021
1 parent 2c70568 commit 6113c9f
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public boolean isSharable() {
});
Channel channel = QuicTestUtils.newClient();
QuicChannelValidationHandler clientHandler = new QuicChannelValidationHandler();
ByteBuf data = Unpooled.directBuffer().writeLong(8);
try {
QuicChannel quicChannel = QuicChannel.newBootstrap(channel)
.handler(clientHandler)
Expand All @@ -90,7 +91,6 @@ public boolean isSharable() {
.connect()
.get();

ByteBuf data = Unpooled.directBuffer().writeLong(8);
List<Channel> streams = new ArrayList<>();
for (int i = 0; i < numOfStreams; i++) {
QuicStreamChannel stream = quicChannel.createStream(
Expand All @@ -102,7 +102,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
}).get();
streams.add(stream.writeAndFlush(data.retainedSlice()).sync().channel());
}
data.release();
latch.await();
while (bytesRead.get() < expectedDataRead) {
Thread.sleep(50);
Expand All @@ -118,6 +117,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
serverHandler.assertState();
clientHandler.assertState();
} finally {
data.release();
server.close().sync();
// Close the parent Datagram channel as well.
channel.close().sync();
Expand Down

0 comments on commit 6113c9f

Please sign in to comment.