Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ private static synchronized String extractLibrary(String name, InputStream resou
String basename = new File(name).getName();
String[] names = basename.split("\\.");

// by putting the file into the jruby temp dir, it's automatically cleaned up
Path tempfile = Files.createTempFile(JRubyClassLoader.getTempDir().toPath(), names[0], "." + names[names.length - 1]);
Files.copy(resourceAsStream, tempfile, StandardCopyOption.REPLACE_EXISTING);

File file = tempfile.toFile();
// the library stays loaded and cached for the life of this JRuby, so it can only be deleted at exit
file.deleteOnExit();
file.setExecutable(true); // do this after we write the file, otherwise dlopen doesn't work
extractedLibraries.put(name, file.getAbsolutePath());
return file.getAbsolutePath();
Expand Down
25 changes: 5 additions & 20 deletions core/src/main/java/org/jruby/util/JRubyClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,9 @@ public static synchronized File getTempDir() {
if (tempDir != null) return tempDir;

tempDir = new File(tempDir(), tempDirName());
if (tempDir.mkdirs()) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
for (File f : tempDir.listFiles()) {
try {
f.delete();
} catch (Exception ex) {
LOG.debug(ex);
}
}
try {
tempDir.delete();
} catch (Exception ex) {
LOG.info("failed to delete temp dir " + tempDir + " : " + ex);
}
}
});
}
// Deleted at exit, once the files in it are. A shutdown hook would be a class loaded by the class loader that
// loaded JRuby, and keep that class loader alive until the JVM exits.
if (tempDir.mkdirs()) tempDir.deleteOnExit();
return tempDir;
}

Expand Down Expand Up @@ -220,9 +205,9 @@ protected void terminateJarIndexCacheEntries() {
// Remove reference from jar cache
JarResource.removeJarResource(path);

// Delete temp jar on disk
// Delete temp jar on disk, or at exit if it can't be deleted yet, e.g. while it's open on Windows
File jarFile = new File(path);
jarFile.delete();
if (!jarFile.delete() && jarFile.exists()) jarFile.deleteOnExit();
} catch (Exception e) {
// keep trying to clean up other temp jars
}
Expand Down
Loading