You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ma...@apache.org on 2021/06/21 11:14:49 UTC

[zookeeper] branch master updated: ZOOKEEPER-4311: Close AtomicFileOutputStream properly

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 65dba9d  ZOOKEEPER-4311: Close AtomicFileOutputStream properly
65dba9d is described below

commit 65dba9d9ab35cdef751473345af52089abc0da6e
Author: Dmitrii Kovalkov <di...@gmail.com>
AuthorDate: Mon Jun 21 19:14:37 2021 +0800

    ZOOKEEPER-4311: Close AtomicFileOutputStream properly
    
    Author: Dmitrii Kovalkov <di...@gmail.com>
    
    Reviewers: Enrico Olivelli <eo...@apache.org>, Andor Molnar <an...@apache.org>, maoling <ma...@apache.org>
    
    Closes #1706 from DimasKovas/close_atomic_file_output_stream and squashes the following commits:
    
    1623c84da [Dmitrii Kovalkov] remove trailing whitespace
    75e525617 [Dmitrii Kovalkov] cosmetics
    449ba8a61 [Dmitrii Kovalkov] Fix
---
 .../apache/zookeeper/common/AtomicFileWritingIdiom.java    | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java
index 6e20b78..903f8e4 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java
@@ -59,7 +59,7 @@ public class AtomicFileWritingIdiom {
         OutputStreamStatement osStmt,
         WriterStatement wStmt) throws IOException {
         AtomicFileOutputStream out = null;
-        boolean error = true;
+        boolean triedToClose = false;
         try {
             out = new AtomicFileOutputStream(targetFile);
             if (wStmt == null) {
@@ -71,20 +71,18 @@ public class AtomicFileWritingIdiom {
                 wStmt.write(bw);
                 bw.flush();
             }
-            out.flush();
+            triedToClose = true;
+            // close() will do the best to clean up file/resources in case of errors
+            // worst case the tmp file may still exist
+            out.close();
             // everything went ok
-            error = false;
         } finally {
             // nothing interesting to do if out == null
             if (out != null) {
-                if (error) {
+                if (!triedToClose) {
                     // worst case here the tmp file/resources(fd) are not cleaned up
                     // and the caller will be notified (IOException)
                     out.abort();
-                } else {
-                    // if the close operation (rename) fails we'll get notified.
-                    // worst case the tmp file may still exist
-                    IOUtils.closeStream(out);
                 }
             }
         }