From 2ad6c390e07f82d6267f2366366718bad2bb27f7 Mon Sep 17 00:00:00 2001 From: Emily Date: Tue, 28 Feb 2023 08:34:33 -0300 Subject: [PATCH] Close plugin jar file on classloader close and after retrieving name for updating (#8902) --- .../Enable-multi-release-plugin-jars.patch | 4 ++-- patches/api/Paper-Plugins.patch | 8 +++++--- ...s-to-contain-the-source-jars-in-stac.patch | 2 +- patches/server/Paper-Plugins.patch | 19 +++++++++---------- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/patches/api/Enable-multi-release-plugin-jars.patch b/patches/api/Enable-multi-release-plugin-jars.patch index 21890c1e89..28fdaef2e6 100644 --- a/patches/api/Enable-multi-release-plugin-jars.patch +++ b/patches/api/Enable-multi-release-plugin-jars.patch @@ -12,8 +12,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 this.description = description; this.dataFolder = dataFolder; this.file = file; -- this.jar = new JarFile(file); -+ this.jar = new JarFile(file, true, java.util.zip.ZipFile.OPEN_READ, JarFile.runtimeVersion()); // Paper - enable multi-release jars for Java 9+ +- this.jar = jarFile == null ? new JarFile(file) : jarFile; // Paper - use JarFile provided by SpigotPluginProvider ++ this.jar = jarFile == null ? new JarFile(file, true, java.util.zip.ZipFile.OPEN_READ, JarFile.runtimeVersion()) : jarFile; // Paper - use JarFile provided by SpigotPluginProvider // Paper - enable multi-release jars for Java 9+ this.manifest = jar.getManifest(); this.url = file.toURI().toURL(); this.libraryLoader = libraryLoader; diff --git a/patches/api/Paper-Plugins.patch b/patches/api/Paper-Plugins.patch index d8099fc30e..2d51ba3c24 100644 --- a/patches/api/Paper-Plugins.patch +++ b/patches/api/Paper-Plugins.patch @@ -1993,7 +1993,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 final PluginClassLoader loader; try { - loader = new PluginClassLoader(this, getClass().getClassLoader(), description, dataFolder, file, (libraryLoader != null) ? libraryLoader.createLoader(description) : null); -+ loader = new PluginClassLoader(getClass().getClassLoader(), description, dataFolder, file, (libraryLoader != null) ? libraryLoader.createLoader(description) : null); // Paper ++ loader = new PluginClassLoader(getClass().getClassLoader(), description, dataFolder, file, (libraryLoader != null) ? libraryLoader.createLoader(description) : null, null); // Paper } catch (InvalidPluginException ex) { throw ex; } catch (Throwable ex) { @@ -2040,7 +2040,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - PluginClassLoader(@NotNull final JavaPluginLoader loader, @Nullable final ClassLoader parent, @NotNull final PluginDescriptionFile description, @NotNull final File dataFolder, @NotNull final File file, @Nullable ClassLoader libraryLoader) throws IOException, InvalidPluginException, MalformedURLException { + @org.jetbrains.annotations.ApiStatus.Internal // Paper -+ public PluginClassLoader(@Nullable final ClassLoader parent, @NotNull final PluginDescriptionFile description, @NotNull final File dataFolder, @NotNull final File file, @Nullable ClassLoader libraryLoader) throws IOException, InvalidPluginException, MalformedURLException { // Paper ++ public PluginClassLoader(@Nullable final ClassLoader parent, @NotNull final PluginDescriptionFile description, @NotNull final File dataFolder, @NotNull final File file, @Nullable ClassLoader libraryLoader, @Nullable JarFile jarFile) throws IOException, InvalidPluginException, MalformedURLException { // Paper // Paper - use JarFile provided by SpigotPluginProvider super(new URL[] {file.toURI().toURL()}, parent); - Preconditions.checkArgument(loader != null, "Loader cannot be null"); + this.loader = null; // Paper - pass null into loader field @@ -2049,7 +2049,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 this.description = description; this.dataFolder = dataFolder; this.file = file; -@@ -0,0 +0,0 @@ final class PluginClassLoader extends URLClassLoader { +- this.jar = new JarFile(file); ++ this.jar = jarFile == null ? new JarFile(file) : jarFile; // Paper - use JarFile provided by SpigotPluginProvider + this.manifest = jar.getManifest(); this.url = file.toURI().toURL(); this.libraryLoader = libraryLoader; diff --git a/patches/api/Rewrite-LogEvents-to-contain-the-source-jars-in-stac.patch b/patches/api/Rewrite-LogEvents-to-contain-the-source-jars-in-stac.patch index 044b6229b6..98ecc419f0 100644 --- a/patches/api/Rewrite-LogEvents-to-contain-the-source-jars-in-stac.patch +++ b/patches/api/Rewrite-LogEvents-to-contain-the-source-jars-in-stac.patch @@ -11,7 +11,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm @org.jetbrains.annotations.ApiStatus.Internal // Paper - public PluginClassLoader(@Nullable final ClassLoader parent, @NotNull final PluginDescriptionFile description, @NotNull final File dataFolder, @NotNull final File file, @Nullable ClassLoader libraryLoader) throws IOException, InvalidPluginException, MalformedURLException { // Paper + public PluginClassLoader(@Nullable final ClassLoader parent, @NotNull final PluginDescriptionFile description, @NotNull final File dataFolder, @NotNull final File file, @Nullable ClassLoader libraryLoader, @Nullable JarFile jarFile) throws IOException, InvalidPluginException, MalformedURLException { // Paper // Paper - use JarFile provided by SpigotPluginProvider - super(new URL[] {file.toURI().toURL()}, parent); + super(file.getName(), new URL[] {file.toURI().toURL()}, parent); this.loader = null; // Paper - pass null into loader field diff --git a/patches/server/Paper-Plugins.patch b/patches/server/Paper-Plugins.patch index d7b8d03b18..0d68604a41 100644 --- a/patches/server/Paper-Plugins.patch +++ b/patches/server/Paper-Plugins.patch @@ -1004,10 +1004,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public void close() throws IOException { -+ try { ++ try (this.jar; this.libraryLoader) { + super.close(); -+ } finally { -+ this.libraryLoader.close(); + } + } +} @@ -5089,13 +5087,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + private String getPluginName(Path path) throws Exception { -+ JarFile file = new JarFile(path.toFile()); -+ PluginFileType type = PluginFileType.guessType(file); -+ if (type == null) { -+ throw new IllegalArgumentException(path + " is not a valid plugin file, cannot load a plugin from it!"); -+ } ++ try (JarFile file = new JarFile(path.toFile())) { ++ PluginFileType type = PluginFileType.guessType(file); ++ if (type == null) { ++ throw new IllegalArgumentException(path + " is not a valid plugin file, cannot load a plugin from it!"); ++ } + -+ return type.getConfig(file).getName(); ++ return type.getConfig(file).getName(); ++ } + } + + private class UpdateFileVisitor implements FileVisitor { @@ -5944,7 +5943,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + final PluginClassLoader loader; + try { -+ loader = new PluginClassLoader(this.getClass().getClassLoader(), this.description, dataFolder, this.path.toFile(), LIBRARY_LOADER.createLoader(this.description)); // Paper ++ loader = new PluginClassLoader(this.getClass().getClassLoader(), this.description, dataFolder, this.path.toFile(), LIBRARY_LOADER.createLoader(this.description), this.jarFile); // Paper + } catch (InvalidPluginException ex) { + throw ex; + } catch (Throwable ex) {