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

emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new dcc329f  GROOVY-10519: v9 ClassFinder closes existing FileSystems that 
it doesn't own
dcc329f is described below

commit dcc329fc09e86178ebe39cd07063cc720aa41d0a
Author: Paul King <[email protected]>
AuthorDate: Fri Mar 4 11:26:10 2022 +1000

    GROOVY-10519: v9 ClassFinder closes existing FileSystems that it doesn't own
---
 .../codehaus/groovy/vmplugin/v9/ClassFinder.java   | 26 +++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java 
b/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java
index f952598..9c4befe 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java
@@ -18,6 +18,8 @@
  */
 package org.codehaus.groovy.vmplugin.v9;
 
+import groovy.lang.Tuple2;
+
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
@@ -115,8 +117,10 @@ public class ClassFinder {
         final int prefixElemCnt = prefix.trim().isEmpty() ? 0 : 
prefix.split(sepPatten).length;
 
         Map<String, Set<String>> result = new LinkedHashMap<>();
-        try (FileSystem fs = newFileSystem(uri)) {
-            Files.walkFileTree(fs.getPath(prefix + "/" + packageName), new 
SimpleFileVisitor<Path>() {
+        Tuple2<FileSystem, Boolean> fsMaybeNew = null;
+        try {
+            fsMaybeNew = maybeNewFileSystem(uri);
+            Files.walkFileTree(fsMaybeNew.getV1().getPath(prefix + "/" + 
packageName), new SimpleFileVisitor<Path>() {
                 @Override
                 public FileVisitResult preVisitDirectory(Path path, 
BasicFileAttributes attrs) {
                     return FileVisitResult.CONTINUE;
@@ -151,16 +155,28 @@ public class ClassFinder {
                     String.format("Failed to find classes via uri: %s, prefix: 
%s, packageName: %s, recursive: %s",
                             uri, prefix, packageName, recursive
                     ), e);
+        } finally {
+            // we only close file systems we opened
+            if (fsMaybeNew != null && fsMaybeNew.getV2()) {
+                closeQuietly(fsMaybeNew.getV1());
+            }
         }
 
         return result;
     }
 
-    private static FileSystem newFileSystem(URI uri) throws IOException {
+    private static void closeQuietly(FileSystem fs) {
+        try {
+            fs.close();
+        } catch (IOException ignore) {
+        }
+    }
+
+    private static Tuple2<FileSystem, Boolean> maybeNewFileSystem(URI uri) 
throws IOException {
         try {
-            return FileSystems.newFileSystem(uri, Collections.emptyMap());
+            return new Tuple2(FileSystems.newFileSystem(uri, 
Collections.emptyMap()), true);
         } catch (FileSystemAlreadyExistsException e) {
-            return FileSystems.getFileSystem(uri);
+            return new Tuple2(FileSystems.getFileSystem(uri), false);
         }
     }
 

Reply via email to