Skip to content

Instantly share code, notes, and snippets.

@headius
Created June 5, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/6130d94eb71763c534917e6f1a9c3955 to your computer and use it in GitHub Desktop.
Save headius/6130d94eb71763c534917e6f1a9c3955 to your computer and use it in GitHub Desktop.
diff --git a/core/src/main/java/org/jruby/util/JRubyClassLoader.java b/core/src/main/java/org/jruby/util/JRubyClassLoader.java
index 18c1cc8453..a6326ed6eb 100644
--- a/core/src/main/java/org/jruby/util/JRubyClassLoader.java
+++ b/core/src/main/java/org/jruby/util/JRubyClassLoader.java
@@ -37,6 +37,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.net.URL;
+import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -71,7 +72,17 @@ public class JRubyClassLoader extends ClassDefiningJRubyClassLoader {
private final List<String> cachedJarPaths = Collections.synchronizedList(new ArrayList<String>());
public JRubyClassLoader(ClassLoader parent) {
- super(parent);
+ super(new DontCloseMeBroClassLoader(parent));
+ }
+
+ private static class DontCloseMeBroClassLoader extends URLClassLoader {
+ public DontCloseMeBroClassLoader(ClassLoader parent) {
+ super(new URL[0], parent);
+ }
+
+ protected void addURL(URL url) {
+ super.addURL(url);
+ }
}
// Change visibility so others can see it
@@ -103,8 +114,13 @@ public class JRubyClassLoader extends ClassDefiningJRubyClassLoader {
} catch (IOException e) {
throw new RuntimeException("BUG: we can not copy embedded jar to temp directory", e);
}
+
+ // add temp URLs directly to this classloader, which will be closed
+ super.addURL( url );
+ } else {
+ // add normal paths and other forms to non-closing parent classloader
+ ((DontCloseMeBroClassLoader) getParent()).addURL(url);
}
- super.addURL( url );
}
private static synchronized File getTempDir() {
@@ -171,6 +187,10 @@ public class JRubyClassLoader extends ClassDefiningJRubyClassLoader {
@Override
public void close() {
try {
+ // Close this classloader, which will clean up temp jars.
+ // The parent classloader with other forms of jars remains unclosed, allowing cached JarFile instances to
+ // remain usable.
+ // See https://github.com/jruby/jruby/issues/6218
super.close();
}
catch (Exception ex) { LOG.debug(ex); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment