aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-API-Patches/0103-Close-Plugin-Class-Loaders-on-Disable.patch
blob: 812af2e5c77973a12f92195463547de4b37f6d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
From 8b9f3c48dcc40317ca51b6138eb6e3b766a8652c Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 1 May 2018 21:33:35 -0400
Subject: [PATCH] Close Plugin Class Loaders on Disable

This should close more memory leaks from /reload and disabling plugins,
by closing the class loader and the jar file.

diff --git a/src/main/java/org/bukkit/plugin/PluginLoader.java b/src/main/java/org/bukkit/plugin/PluginLoader.java
index e7981a1d..d34756f1 100644
--- a/src/main/java/org/bukkit/plugin/PluginLoader.java
+++ b/src/main/java/org/bukkit/plugin/PluginLoader.java
@@ -73,4 +73,19 @@ public interface PluginLoader {
      * @param plugin Plugin to disable
      */
     public void disablePlugin(Plugin plugin);
+
+    // Paper start - close Classloader on disable
+    /**
+     * Disables the specified plugin
+     * <p>
+     * Attempting to disable a plugin that is not enabled will have no effect
+     *
+     * @param plugin Plugin to disable
+     * @param closeClassloader if the classloader for the Plugin should be closed
+     */
+    // provide default to allow other PluginLoader implementations to work
+    default public void disablePlugin(Plugin plugin, boolean closeClassloader) {
+        disablePlugin(plugin);
+    }
+    // Paper end - close Classloader on disable
 }
diff --git a/src/main/java/org/bukkit/plugin/PluginManager.java b/src/main/java/org/bukkit/plugin/PluginManager.java
index e5638d56..b72d5a9b 100644
--- a/src/main/java/org/bukkit/plugin/PluginManager.java
+++ b/src/main/java/org/bukkit/plugin/PluginManager.java
@@ -154,6 +154,18 @@ public interface PluginManager {
      */
     public void disablePlugin(Plugin plugin);
 
+    // Paper start - close Classloader on disable
+    /**
+     * Disables the specified plugin
+     * <p>
+     * Attempting to disable a plugin that is not enabled will have no effect
+     *
+     * @param plugin Plugin to disable
+     * @param closeClassloader if the classloader for the Plugin should be closed
+     */
+    public void disablePlugin(Plugin plugin, boolean closeClassloader);
+    // Paper end - close Classloader on disable
+
     /**
      * Gets a {@link Permission} from its fully qualified name
      *
diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
index bd0588a2..cb2b0b9c 100644
--- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java
+++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
@@ -412,17 +412,29 @@ public final class SimplePluginManager implements PluginManager {
         }
     }
 
+    // Paper start - close Classloader on disable
     public void disablePlugins() {
+        disablePlugins(false);
+    }
+
+    public void disablePlugins(boolean closeClassloaders) {
+        // Paper end - close Classloader on disable
         Plugin[] plugins = getPlugins();
         for (int i = plugins.length - 1; i >= 0; i--) {
-            disablePlugin(plugins[i]);
+            disablePlugin(plugins[i], closeClassloaders); // Paper - close Classloader on disable
         }
     }
 
-    public void disablePlugin(final Plugin plugin) {
+    // Paper start - close Classloader on disable
+    public void disablePlugin(Plugin plugin) {
+        disablePlugin(plugin, false);
+    }
+
+    public void disablePlugin(final Plugin plugin, boolean closeClassloader) {
+        // Paper end - close Classloader on disable
         if (plugin.isEnabled()) {
             try {
-                plugin.getPluginLoader().disablePlugin(plugin);
+                plugin.getPluginLoader().disablePlugin(plugin, closeClassloader); // Paper - close Classloader on disable
             } catch (Throwable ex) {
                 handlePluginException("Error occurred (in the plugin loader) while disabling "
                         + plugin.getDescription().getFullName() + " (Is it up to date?)", ex, plugin); // Paper
@@ -468,7 +480,7 @@ public final class SimplePluginManager implements PluginManager {
 
     public void clearPlugins() {
         synchronized (this) {
-            disablePlugins();
+            disablePlugins(true); // Paper - close Classloader on disable
             plugins.clear();
             lookupNames.clear();
             HandlerList.unregisterAll();
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
index 40fd71dc..8b5cea64 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@@ -317,7 +317,7 @@ public final class JavaPluginLoader implements PluginLoader {
             } catch (Throwable ex) {
                 server.getLogger().log(Level.SEVERE, "Error occurred while enabling " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex);
                 // Paper start - Disable plugins that fail to load
-                disablePlugin(jPlugin);
+                server.getPluginManager().disablePlugin(jPlugin, true); // Paper - close Classloader on disable - She's dead jim
                 return;
                 // Paper end
             }
@@ -328,7 +328,13 @@ public final class JavaPluginLoader implements PluginLoader {
         }
     }
 
+    // Paper start - close Classloader on disable
     public void disablePlugin(Plugin plugin) {
+        disablePlugin(plugin, false); // Retain old behavior unless requested
+    }
+
+    public void disablePlugin(Plugin plugin, boolean closeClassloader) {
+        // Paper end - close Class Loader on disable
         Validate.isTrue(plugin instanceof JavaPlugin, "Plugin is not associated with this PluginLoader");
 
         if (plugin.isEnabled()) {
@@ -355,6 +361,16 @@ public final class JavaPluginLoader implements PluginLoader {
                 for (String name : names) {
                     removeClass(name);
                 }
+                // Paper start - close Class Loader on disable
+                try {
+                    if (closeClassloader) {
+                        loader.close();
+                    }
+                } catch (IOException e) {
+                    server.getLogger().log(Level.WARNING, "Error closing the Plugin Class Loader for " + plugin.getDescription().getFullName());
+                    e.printStackTrace();
+                }
+                // Paper end
             }
         }
     }
-- 
2.19.2