From 2183b37892851dc5d993652a3b9d88f11015366d Mon Sep 17 00:00:00 2001 From: prgitpr <15207561449@163.com> Date: Thu, 14 May 2020 16:16:16 +0800 Subject: [PATCH] Fix a potential fd leak in AbstractDiskHttpData.getChunk (#10270) Motivation: `FileChannel.read()` may throw an IOException. We must deal with this in case of the occurrence of `I/O` error. Modification: Place the `FileChannel.read()` method call in the `try-finally` block. Result: Advoid fd leak. Co-authored-by: Norman Maurer --- .../http/multipart/AbstractDiskHttpData.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java index e0a16b352a..cf7ba5eaf9 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java @@ -300,15 +300,17 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { } int read = 0; ByteBuffer byteBuffer = ByteBuffer.allocate(length); - while (read < length) { - int readnow = fileChannel.read(byteBuffer); - if (readnow == -1) { - fileChannel.close(); - fileChannel = null; - break; - } else { + try { + while (read < length) { + int readnow = fileChannel.read(byteBuffer); + if (readnow == -1) { + break; + } read += readnow; } + } finally { + fileChannel.close(); + fileChannel = null; } if (read == 0) { return EMPTY_BUFFER;