This is an automated email from the ASF dual-hosted git repository.

maoling pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 766e173  ZOOKEEPER-4246: Resource leaks in 
org.apache.zookeeper.server.persistence.SnapStream#getInputStream and 
#getOutputStream
766e173 is described below

commit 766e173e9d51b6354920ebc136b246d221b87ec1
Author: Martin Kellogg <[email protected]>
AuthorDate: Sat May 15 23:50:02 2021 +0800

    ZOOKEEPER-4246: Resource leaks in 
org.apache.zookeeper.server.persistence.SnapStream#getInputStream and 
#getOutputStream
    
    Bug report is here: https://issues.apache.org/jira/browse/ZOOKEEPER-4246
    
    This fix is simple: it just closes the possibly-leaked streams and 
re-throws the exception. We can't use a try-with-resources or a `finally` block 
here because in the happy case the resulting streams need to be returned open. 
I checked each of the other constructor calls in these methods, and none of the 
others can throw an exception as far as I can tell.
    
    Author: Martin Kellogg <[email protected]>
    
    Reviewers: Andor Molnar <[email protected]>, Enrico Olivelli 
<[email protected]>, maoling <[email protected]>
    
    Closes #1638 from kelloggm/ZOOKEEPER-4246 and squashes the following 
commits:
    
    fa1c0f08f [Martin Kellogg] surround whole switch with one try block instead 
of two inside the switch
    f023851ba [Martin Kellogg] remove all the tabs for real
    04c4b2fa6 [Martin Kellogg] fix accidental tab
    e896efa6a [Martin Kellogg] ZOOKEEPER-4246: Resource leaks in 
org.apache.zookeeper.server.persistence.SnapStream#getInputStream and 
#getOutputStream
---
 .../zookeeper/server/persistence/SnapStream.java   | 36 ++++++++++++++--------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git 
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java
 
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java
index e73df43..9bc8ee5 100644
--- 
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java
+++ 
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java
@@ -102,18 +102,23 @@ public class SnapStream {
     public static CheckedInputStream getInputStream(File file) throws 
IOException {
         FileInputStream fis = new FileInputStream(file);
         InputStream is;
-        switch (getStreamMode(file.getName())) {
-        case GZIP:
-            is = new GZIPInputStream(fis);
-            break;
-        case SNAPPY:
-            is = new SnappyInputStream(fis);
-            break;
-        case CHECKED:
-        default:
-            is = new BufferedInputStream(fis);
+        try {
+            switch (getStreamMode(file.getName())) {
+                case GZIP:
+                    is = new GZIPInputStream(fis);
+                    break;
+                case SNAPPY:
+                    is = new SnappyInputStream(fis);
+                    break;
+                case CHECKED:
+                default:
+                    is = new BufferedInputStream(fis);
+            }
+            return new CheckedInputStream(is, new Adler32());
+        } catch (IOException e) {
+            fis.close();
+            throw e;
         }
-        return new CheckedInputStream(is, new Adler32());
     }
 
     /**
@@ -129,9 +134,16 @@ public class SnapStream {
         OutputStream os;
         switch (streamMode) {
         case GZIP:
-            os = new GZIPOutputStream(fos);
+            try {
+                os = new GZIPOutputStream(fos);
+            } catch (IOException e) {
+                fos.close();
+                throw e;
+            }
             break;
         case SNAPPY:
+            // Unlike SnappyInputStream, the SnappyOutputStream
+            // constructor cannot throw an IOException.
             os = new SnappyOutputStream(fos);
             break;
         case CHECKED:

Reply via email to