CodecOutputList should be recycled in a finally block (#10186)

Motivation:

To ensure we always recycle the CodecOutputList we should better do it in a finally block

Modifications:

Call CodecOutputList.recycle() in finally

Result:

Less chances of non-recycled lists. Related to https://github.com/netty/netty/issues/10183
This commit is contained in:
Norman Maurer 2020-04-15 09:08:19 +02:00 committed by GitHub
parent 1b3e1985b4
commit 4c9d7492fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 32 deletions

View File

@ -279,21 +279,24 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
} catch (Exception e) { } catch (Exception e) {
throw new DecoderException(e); throw new DecoderException(e);
} finally { } finally {
if (cumulation != null && !cumulation.isReadable()) { try {
numReads = 0; if (cumulation != null && !cumulation.isReadable()) {
cumulation.release(); numReads = 0;
cumulation = null; cumulation.release();
} else if (++ numReads >= discardAfterReads) { cumulation = null;
// We did enough reads already try to discard some bytes so we not risk to see a OOME. } else if (++numReads >= discardAfterReads) {
// See https://github.com/netty/netty/issues/4275 // We did enough reads already try to discard some bytes so we not risk to see a OOME.
numReads = 0; // See https://github.com/netty/netty/issues/4275
discardSomeReadBytes(); numReads = 0;
} discardSomeReadBytes();
}
int size = out.size(); int size = out.size();
firedChannelRead |= out.insertSinceRecycled(); firedChannelRead |= out.insertSinceRecycled();
fireChannelRead(ctx, out, size); fireChannelRead(ctx, out, size);
out.recycle(); } finally {
out.recycle();
}
} }
} else { } else {
ctx.fireChannelRead(msg); ctx.fireChannelRead(msg);

View File

@ -97,11 +97,14 @@ public abstract class MessageToMessageDecoder<I> extends ChannelInboundHandlerAd
} catch (Exception e) { } catch (Exception e) {
throw new DecoderException(e); throw new DecoderException(e);
} finally { } finally {
int size = out.size(); try {
for (int i = 0; i < size; i ++) { int size = out.size();
ctx.fireChannelRead(out.getUnsafe(i)); for (int i = 0; i < size; i++) {
ctx.fireChannelRead(out.getUnsafe(i));
}
} finally {
out.recycle();
} }
out.recycle();
} }
} }

View File

@ -92,9 +92,6 @@ public abstract class MessageToMessageEncoder<I> extends ChannelOutboundHandlerA
} }
if (out.isEmpty()) { if (out.isEmpty()) {
out.recycle();
out = null;
throw new EncoderException( throw new EncoderException(
StringUtil.simpleClassName(this) + " must produce at least one message."); StringUtil.simpleClassName(this) + " must produce at least one message.");
} }
@ -107,19 +104,22 @@ public abstract class MessageToMessageEncoder<I> extends ChannelOutboundHandlerA
throw new EncoderException(t); throw new EncoderException(t);
} finally { } finally {
if (out != null) { if (out != null) {
final int sizeMinusOne = out.size() - 1; try {
if (sizeMinusOne == 0) { final int sizeMinusOne = out.size() - 1;
ctx.write(out.getUnsafe(0), promise); if (sizeMinusOne == 0) {
} else if (sizeMinusOne > 0) { ctx.write(out.getUnsafe(0), promise);
// Check if we can use a voidPromise for our extra writes to reduce GC-Pressure } else if (sizeMinusOne > 0) {
// See https://github.com/netty/netty/issues/2525 // Check if we can use a voidPromise for our extra writes to reduce GC-Pressure
if (promise == ctx.voidPromise()) { // See https://github.com/netty/netty/issues/2525
writeVoidPromise(ctx, out); if (promise == ctx.voidPromise()) {
} else { writeVoidPromise(ctx, out);
writePromiseCombiner(ctx, out, promise); } else {
writePromiseCombiner(ctx, out, promise);
}
} }
} finally {
out.recycle();
} }
out.recycle();
} }
} }
} }