From ec9b5c13b879f44c04fa28ee6c8b113a165c9e57 Mon Sep 17 00:00:00 2001 From: James Nord Date: Thu, 17 Nov 2016 19:03:00 +0000 Subject: [PATCH] Be extra defensive about Errors and Exceptions [JENKINS-39835] Be even more defensive then against leaving connections dangling. --- .../protocol/impl/NIONetworkLayer.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jenkinsci/remoting/protocol/impl/NIONetworkLayer.java b/src/main/java/org/jenkinsci/remoting/protocol/impl/NIONetworkLayer.java index 6411b5548..5ddc2727c 100644 --- a/src/main/java/org/jenkinsci/remoting/protocol/impl/NIONetworkLayer.java +++ b/src/main/java/org/jenkinsci/remoting/protocol/impl/NIONetworkLayer.java @@ -177,15 +177,19 @@ public void ready(boolean accept, boolean connect, boolean read, boolean write) } recvKey.cancel(); onRecvClosed(); - } catch (RuntimeException e) { + } catch (Throwable t) { // this should *never* happen... but just in case it does we will log & close connection - if (LOGGER.isLoggable(Level.WARNING)) { - LogRecord record = new LogRecord(Level.WARNING, "[{0}] Uncaught {1}"); - record.setThrown(e); - record.setParameters(new Object[]{stack().name(), e.getClass().getSimpleName()}); + try { + if (LOGGER.isLoggable(Level.SEVERE)) { + LogRecord record = new LogRecord(Level.SEVERE, "[{0}] Uncaught {1}"); + record.setThrown(t); + record.setParameters(new Object[]{stack().name(), t.getClass().getSimpleName()}); + } + } finally { + // incase this was an OOMErr and logging caused another OOMErr + recvKey.cancel(); + onRecvClosed(); } - recvKey.cancel(); - onRecvClosed(); } finally { release(recv); }