diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index fdbc46ad20..61fec71d92 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -859,13 +859,17 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha ChannelOutboundBuffer outboundBuffer = this.outboundBuffer; if (outboundBuffer == null) { - // If the outboundBuffer is null we know the channel was closed and so - // need to fail the future right away. If it is not null the handling of the rest - // will be done in flush0() - // See https://github.com/netty/netty/issues/2362 - safeSetFailure(promise, newClosedChannelException(initialCloseCause, "write(Object, ChannelPromise)")); - // release message now to prevent resource-leak - ReferenceCountUtil.release(msg); + try { + // release message now to prevent resource-leak + ReferenceCountUtil.release(msg); + } finally { + // If the outboundBuffer is null we know the channel was closed and so + // need to fail the future right away. If it is not null the handling of the rest + // will be done in flush0() + // See https://github.com/netty/netty/issues/2362 + safeSetFailure(promise, + newClosedChannelException(initialCloseCause, "write(Object, ChannelPromise)")); + } return; } @@ -877,8 +881,11 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha size = 0; } } catch (Throwable t) { - safeSetFailure(promise, t); - ReferenceCountUtil.release(msg); + try { + ReferenceCountUtil.release(msg); + } finally { + safeSetFailure(promise, t); + } return; } diff --git a/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java index 22fa05a2c0..348ba3a558 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java @@ -991,11 +991,11 @@ abstract class AbstractChannelHandlerContext implements ChannelHandlerContext, R return true; } catch (Throwable cause) { try { - promise.setFailure(cause); - } finally { if (msg != null) { ReferenceCountUtil.release(msg); } + } finally { + promise.setFailure(cause); } return false; }