[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [collector/master] Make sure that the DirectoryStream gets closed.



commit 83850daa9de893cf2d7f846e67191a469cc64900
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Sat Dec 5 22:22:22 2020 +0100

    Make sure that the DirectoryStream gets closed.
    
    As the docs say, "If timely disposal of file system resources is
    required, the try-with-resources construct should be used to ensure
    that the stream's close method is invoked after the stream operations
    are completed."
    
    Turns out that without closing the stream, the JVM runs out of memory
    pretty quickly. Doing this is not optional but mandatory.
---
 .../torproject/metrics/collector/persist/PersistenceUtils.java | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/collector/persist/PersistenceUtils.java b/src/main/java/org/torproject/metrics/collector/persist/PersistenceUtils.java
index 2b7621d..1dc36d6 100644
--- a/src/main/java/org/torproject/metrics/collector/persist/PersistenceUtils.java
+++ b/src/main/java/org/torproject/metrics/collector/persist/PersistenceUtils.java
@@ -20,6 +20,7 @@ import java.nio.file.attribute.BasicFileAttributes;
 import java.text.SimpleDateFormat;
 import java.time.Instant;
 import java.util.Date;
+import java.util.stream.Stream;
 
 public class PersistenceUtils {
 
@@ -132,9 +133,12 @@ public class PersistenceUtils {
       @Override
       public FileVisitResult postVisitDirectory(Path dir, IOException exc)
           throws IOException {
-        if (!pathToClean.equals(dir)
-            && !Files.list(dir).findFirst().isPresent()) {
-          Files.delete(dir);
+        if (!pathToClean.equals(dir)) {
+          try (Stream<Path> files = Files.list(dir)) {
+            if (!files.findFirst().isPresent()) {
+              Files.delete(dir);
+            }
+          }
         }
         return FileVisitResult.CONTINUE;
       }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits