Release message before notify promise (#10726)
Motivation: We should preferable always release the message before we notify the promise. Thhis has a few advantages: - Release memory as soon as possible - Listeners observe the "more correct" reference count Modifications: Release message before fail the promises Result: Faster releasing of resources. This came up in https://github.com/netty/netty/issues/10723
This commit is contained in:
parent
4ecd78e104
commit
f5185ed73b
@ -859,13 +859,17 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
|
|||||||
|
|
||||||
ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;
|
ChannelOutboundBuffer outboundBuffer = this.outboundBuffer;
|
||||||
if (outboundBuffer == null) {
|
if (outboundBuffer == null) {
|
||||||
|
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
|
// 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
|
// need to fail the future right away. If it is not null the handling of the rest
|
||||||
// will be done in flush0()
|
// will be done in flush0()
|
||||||
// See https://github.com/netty/netty/issues/2362
|
// See https://github.com/netty/netty/issues/2362
|
||||||
safeSetFailure(promise, newClosedChannelException(initialCloseCause, "write(Object, ChannelPromise)"));
|
safeSetFailure(promise,
|
||||||
// release message now to prevent resource-leak
|
newClosedChannelException(initialCloseCause, "write(Object, ChannelPromise)"));
|
||||||
ReferenceCountUtil.release(msg);
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -877,8 +881,11 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
|
|||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
safeSetFailure(promise, t);
|
try {
|
||||||
ReferenceCountUtil.release(msg);
|
ReferenceCountUtil.release(msg);
|
||||||
|
} finally {
|
||||||
|
safeSetFailure(promise, t);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -991,11 +991,11 @@ abstract class AbstractChannelHandlerContext implements ChannelHandlerContext, R
|
|||||||
return true;
|
return true;
|
||||||
} catch (Throwable cause) {
|
} catch (Throwable cause) {
|
||||||
try {
|
try {
|
||||||
promise.setFailure(cause);
|
|
||||||
} finally {
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
ReferenceCountUtil.release(msg);
|
ReferenceCountUtil.release(msg);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
promise.setFailure(cause);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user