[#2644] Correctly release buffer when exception happens during send DatagramPacket or SctpMessage
Motivation: When an exception is thrown during try to send DatagramPacket or SctpMessage a buffer may leak. Modification: Correctly handle allocated buffers in case of exception Result: No more leaks
This commit is contained in:
parent
26c20c91bd
commit
21aa3d8997
@ -316,22 +316,26 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett
|
|||||||
data = alloc.directBuffer(dataLen).writeBytes(data);
|
data = alloc.directBuffer(dataLen).writeBytes(data);
|
||||||
nioData = data.nioBuffer();
|
nioData = data.nioBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
|
final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
|
||||||
mi.payloadProtocolID(packet.protocolIdentifier());
|
mi.payloadProtocolID(packet.protocolIdentifier());
|
||||||
mi.streamNumber(packet.streamIdentifier());
|
mi.streamNumber(packet.streamIdentifier());
|
||||||
|
|
||||||
final int writtenBytes = javaChannel().send(nioData, mi);
|
boolean done = false;
|
||||||
|
try {
|
||||||
boolean done = writtenBytes > 0;
|
final int writtenBytes = javaChannel().send(nioData, mi);
|
||||||
if (needsCopy) {
|
done = writtenBytes > 0;
|
||||||
if (!done) {
|
return done;
|
||||||
in.current(new SctpMessage(mi, data));
|
} finally {
|
||||||
} else {
|
// Handle this in the finally block to make sure we release the old buffer in all cases
|
||||||
in.current(data);
|
// See https://github.com/netty/netty/issues/2644
|
||||||
|
if (needsCopy) {
|
||||||
|
if (!done) {
|
||||||
|
in.current(new SctpMessage(mi, data));
|
||||||
|
} else {
|
||||||
|
in.current(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -300,31 +300,36 @@ public final class NioDatagramChannel
|
|||||||
nioData = data.nioBuffer();
|
nioData = data.nioBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
final int writtenBytes;
|
boolean done = false;
|
||||||
if (remoteAddress != null) {
|
try {
|
||||||
writtenBytes = javaChannel().send(nioData, remoteAddress);
|
final int writtenBytes;
|
||||||
} else {
|
if (remoteAddress != null) {
|
||||||
writtenBytes = javaChannel().write(nioData);
|
writtenBytes = javaChannel().send(nioData, remoteAddress);
|
||||||
}
|
|
||||||
|
|
||||||
boolean done = writtenBytes > 0;
|
|
||||||
if (needsCopy) {
|
|
||||||
// This means we have allocated a new buffer and need to store it back so we not need to allocate it again
|
|
||||||
// later
|
|
||||||
if (remoteAddress == null) {
|
|
||||||
// remoteAddress is null which means we can handle it as ByteBuf directly
|
|
||||||
in.current(data);
|
|
||||||
} else {
|
} else {
|
||||||
if (!done) {
|
writtenBytes = javaChannel().write(nioData);
|
||||||
// store it back with all the needed informations
|
}
|
||||||
in.current(new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(data, remoteAddress));
|
done = writtenBytes > 0;
|
||||||
} else {
|
return done;
|
||||||
// Just store back the new create buffer so it is cleaned up once in.remove() is called.
|
} finally {
|
||||||
|
// Handle this in the finally block to make sure we release the old buffer in all cases
|
||||||
|
// See https://github.com/netty/netty/issues/2644
|
||||||
|
if (needsCopy) {
|
||||||
|
// This means we have allocated a new buffer and need to store it back so we not need to allocate it
|
||||||
|
// later again
|
||||||
|
if (remoteAddress == null) {
|
||||||
|
// remoteAddress is null which means we can handle it as ByteBuf directly
|
||||||
in.current(data);
|
in.current(data);
|
||||||
|
} else {
|
||||||
|
if (!done) {
|
||||||
|
// store it back with all the needed informations
|
||||||
|
in.current(new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(data, remoteAddress));
|
||||||
|
} else {
|
||||||
|
// Just store back the new created buffer so it is cleaned up once in.remove() is called.
|
||||||
|
in.current(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user