From bd3a60b7eb5e73c5f43d2cc7256aa545993ff3e5 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 20 Mar 2014 23:26:07 +0100 Subject: [PATCH] Don't intercept catastrophic errors such as ThreadDeath and OOM. May fix ticket 206. --- .../com/comphenix/protocol/Application.java | 2 +- .../comphenix/protocol/ProtocolLibrary.java | 30 +++++++++++++++++-- .../protocol/async/AsyncListenerHandler.java | 4 +++ .../protocol/error/DetailedErrorReporter.java | 4 +-- .../protocol/injector/PacketConstructor.java | 4 +++ .../injector/PacketFilterManager.java | 4 +++ .../injector/packet/ReadPacketModifier.java | 5 ++++ .../injector/packet/WritePacketModifier.java | 4 +++ .../player/InjectedServerConnection.java | 4 +++ .../injector/player/NetLoginInjector.java | 8 +++++ .../player/NetworkServerInjector.java | 4 +++ .../injector/player/PlayerInjector.java | 4 +++ .../reflect/compiler/BackgroundCompiler.java | 6 ++++ 13 files changed, 78 insertions(+), 5 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/Application.java b/ProtocolLib/src/main/java/com/comphenix/protocol/Application.java index baa6be30..249f2115 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/Application.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/Application.java @@ -41,7 +41,7 @@ public class Application { if (primaryMethod) { try { return Bukkit.isPrimaryThread(); - } catch (Throwable e) { + } catch (LinkageError e) { primaryMethod = false; } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index 6ff8a6ec..52e49edf 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -242,6 +242,10 @@ public class ProtocolLibrary extends JavaPlugin { initializeCommands(); setupBroadcastUsers(PERMISSION_INFO); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { reporter.reportDetailed(this, Report.newBuilder(REPORT_PLUGIN_LOAD_ERROR).error(e).callerParam(protocolManager)); disablePlugin(); @@ -263,6 +267,10 @@ public class ProtocolLibrary extends JavaPlugin { case PACKET: commandPacket = new CommandPacket(reporter, this, logger, commandFilter, protocolManager); break; } + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { reporter.reportWarning(this, Report.newBuilder(REPORT_CANNOT_REGISTER_COMMAND). messageParam(command.name(), e.getMessage()).error(e)); @@ -395,6 +403,10 @@ public class ProtocolLibrary extends JavaPlugin { // It also performs the update check. createPacketTask(server); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { reporter.reportDetailed(this, Report.newBuilder(REPORT_PLUGIN_ENABLE_ERROR).error(e)); disablePlugin(); @@ -406,6 +418,10 @@ public class ProtocolLibrary extends JavaPlugin { if (config.isMetricsEnabled()) { statistisc = new Statistics(this); } + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (IOException e) { reporter.reportDetailed(this, Report.newBuilder(REPORT_METRICS_IO_ERROR).error(e).callerParam(statistisc)); } catch (Throwable e) { @@ -537,6 +553,10 @@ public class ProtocolLibrary extends JavaPlugin { } }, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { if (packetTask == -1) { reporter.reportDetailed(this, Report.newBuilder(REPORT_CANNOT_CREATE_TIMEOUT_TASK).error(e)); @@ -625,10 +645,16 @@ public class ProtocolLibrary extends JavaPlugin { // Get the Bukkit logger first, before we try to create our own private Logger getLoggerSafely() { Logger log = null; - + try { log = getLogger(); - } catch (Throwable e) { } + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; + } catch (Throwable e) { + // Ignore + } // Use the default logger instead if (log == null) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncListenerHandler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncListenerHandler.java index 840cffc1..d141a57e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncListenerHandler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncListenerHandler.java @@ -604,6 +604,10 @@ public class AsyncListenerHandler { } } + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { // Minecraft doesn't want your Exception. filterManager.getErrorReporter().reportMinimal(listener.getPlugin(), methodName, e); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java index 9fab0e8c..f579b377 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/error/DetailedErrorReporter.java @@ -125,7 +125,7 @@ public class DetailedErrorReporter implements ErrorReporter { private static Logger getBukkitLogger() { try { return Bukkit.getLogger(); - } catch (Throwable e) { + } catch (LinkageError e) { return Logger.getLogger("Minecraft"); } } @@ -418,7 +418,7 @@ public class DetailedErrorReporter implements ErrorReporter { try { if (!apacheCommonsMissing) return (ToStringBuilder.reflectionToString(value, ToStringStyle.MULTI_LINE_STYLE, false, null)); - } catch (Throwable ex) { + } catch (LinkageError ex) { // Apache is probably missing apacheCommonsMissing = true; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java index 70c5e026..ef569ff1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketConstructor.java @@ -141,6 +141,10 @@ public class PacketConstructor { try { result = unwrapper.unwrapItem(values[i]); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { lastException = e; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java index 522069a1..0770454e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -238,6 +238,10 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok // The plugin verifier - we don't want to stop ProtocolLib just because its failing try { this.pluginVerifier = new PluginVerifier(builder.getLibrary()); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { reporter.reportWarning(this, Report.newBuilder(REPORT_PLUGIN_VERIFIER_ERROR). messageParam(e.getMessage()).error(e)); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ReadPacketModifier.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ReadPacketModifier.java index 153411c2..865db75d 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ReadPacketModifier.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ReadPacketModifier.java @@ -156,6 +156,11 @@ class ReadPacketModifier implements MethodInterceptor { override.put(thisObj, result); } } + + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { // Minecraft cannot handle this error reporter.reportDetailed(this, diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/WritePacketModifier.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/WritePacketModifier.java index 9712ef52..3b0a9904 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/WritePacketModifier.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/WritePacketModifier.java @@ -120,6 +120,10 @@ public class WritePacketModifier implements MethodInterceptor { output.write(outputBuffer); return null; + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { // Minecraft cannot handle this error reporter.reportDetailed(this, diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java index 935bbb19..95284548 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java @@ -359,6 +359,10 @@ public class InjectedServerConnection { // If so, copy the content of the old element to the new try { writer.copyTo(inserting, replacement, inserting.getClass()); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { reporter.reportDetailed(InjectedServerConnection.this, Report.newBuilder(REPORT_CANNOT_COPY_OLD_TO_NEW).messageParam(inserting).callerParam(inserting, replacement).error(e) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java index 5e008f79..0deec025 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java @@ -86,6 +86,10 @@ class NetLoginInjector { // NetServerInjector can never work (currently), so we don't need to replace the NetLoginHandler return inserting; + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { // Minecraft can't handle this, so we'll deal with it here reporter.reportDetailed(this, @@ -129,6 +133,10 @@ class NetLoginInjector { } } + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { // Don't leak this to Minecraft reporter.reportDetailed(this, diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java index 464b67e3..6d522aaf 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java @@ -221,6 +221,10 @@ class NetworkServerInjector extends PlayerInjector { if (proxyServerField != null && !proxyServerField.equals(serverHandlerRef.getField())) { try { return FieldUtils.readField(proxyServerField, serverHandler, true); + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { // Oh well } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java index 359b5d0a..0bd8045c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java @@ -642,6 +642,10 @@ public abstract class PlayerInjector implements SocketInjector { return result; } + } catch (OutOfMemoryError e) { + throw e; + } catch (ThreadDeath e) { + throw e; } catch (Throwable e) { reporter.reportDetailed(this, Report.newBuilder(REPORT_CANNOT_HANDLE_PACKET).error(e).callerParam(packet)); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/BackgroundCompiler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/BackgroundCompiler.java index 4883523a..c207f579 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/BackgroundCompiler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/BackgroundCompiler.java @@ -221,6 +221,12 @@ public class BackgroundCompiler { } } + } catch (OutOfMemoryError e) { + setEnabled(false); + throw e; + } catch (ThreadDeath e) { + setEnabled(false); + throw e; } catch (Throwable e) { // Disable future compilations! setEnabled(false);