vanzin commented on a change in pull request #26609: [SPARK-29971] Fix multiple
possible buffer leaks in `TransportFrameDecoder/TransportCipher`
URL: https://github.com/apache/spark/pull/26609#discussion_r349280324
##########
File path:
common/network-common/src/main/java/org/apache/spark/network/crypto/TransportCipher.java
##########
@@ -166,34 +167,46 @@ boolean isCipherValid() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object data) throws
Exception {
- if (!isCipherValid) {
- throw new IOException("Cipher is in invalid state.");
- }
- byteChannel.feedData((ByteBuf) data);
-
- byte[] decryptedData = new byte[byteChannel.readableBytes()];
- int offset = 0;
- while (offset < decryptedData.length) {
- // SPARK-25535: workaround for CRYPTO-141.
- try {
- offset += cis.read(decryptedData, offset, decryptedData.length -
offset);
- } catch (InternalError ie) {
- isCipherValid = false;
- throw ie;
+ ByteBuf buffer = (ByteBuf) data;
+
+ try {
+ if (!isCipherValid) {
+ throw new IOException("Cipher is in invalid state.");
+ }
+ byte[] decryptedData = new byte[buffer.readableBytes()];
+ byteChannel.feedData(buffer);
+
+ int offset = 0;
+ while (offset < decryptedData.length) {
+ // SPARK-25535: workaround for CRYPTO-141.
+ try {
+ offset += cis.read(decryptedData, offset, decryptedData.length -
offset);
+ } catch (InternalError ie) {
+ isCipherValid = false;
+ throw ie;
+ }
}
- }
- ctx.fireChannelRead(Unpooled.wrappedBuffer(decryptedData, 0,
decryptedData.length));
+ ctx.fireChannelRead(Unpooled.wrappedBuffer(decryptedData, 0,
decryptedData.length));
+ } finally {
+ buffer.release();
+ }
}
@Override
- public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
+ // We do the closing of the stream / channel in handlerRemoved(...) as
+ // this method will be called
+ // in all cases:
Review comment:
fits in previous line
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]