From 8b21df0ee87c911eb6cd685ce052a63609c977eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Tabin?= Date: Thu, 16 Feb 2023 17:45:42 +0100 Subject: [PATCH] Removes internal throable reference to avoid potential ClassLoader leaks --- .../enterprise/loader/ASURLClassLoader.java | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/nucleus/common/common-util/src/main/java/com/sun/enterprise/loader/ASURLClassLoader.java b/nucleus/common/common-util/src/main/java/com/sun/enterprise/loader/ASURLClassLoader.java index 258d0a1de6c..53fa2758fba 100644 --- a/nucleus/common/common-util/src/main/java/com/sun/enterprise/loader/ASURLClassLoader.java +++ b/nucleus/common/common-util/src/main/java/com/sun/enterprise/loader/ASURLClassLoader.java @@ -1073,11 +1073,7 @@ public void close() { private synchronized void closeOpenStreams() { SentinelInputStream[] toClose = streams.toArray(new SentinelInputStream[0]); for (SentinelInputStream s : toClose) { - try { - s.closeWithWarning(); - } catch (IOException ioe) { - _logger.log(Level.WARNING, CULoggerInfo.exceptionClosingStream, ioe); - } + s.closeWithWarning(); } streams.clear(); } @@ -1091,7 +1087,7 @@ private synchronized void closeOpenStreams() { */ protected final class SentinelInputStream extends FilterInputStream { private volatile boolean closed = false; - private final Throwable throwable; + private volatile Throwable throwable; /** * Constructs new FilteredInputStream which reports InputStreams not closed properly. @@ -1123,17 +1119,7 @@ public void close() throws IOException { * relates to _close() is unclear. */ public final void registerStopEvent() { - CleanerFactory.create().register(this, () -> { - if (!closed && this.in != null) { - try { - in.close(); - } catch (IOException ignored) { - //Cannot do anything here. - } - //Well, give them a stack trace! - report(); - } - }); + CleanerFactory.create().register(this, () -> closeWithWarning()); } /** @@ -1152,20 +1138,33 @@ private synchronized void _close() throws IOException { // race condition with above check, but should have no harmful effects closed = true; + throwable = null; getStreams().remove(this); super.close(); } - private void closeWithWarning() throws IOException { - _close(); - report(); + private void closeWithWarning() { + if ( closed ) { + return; + } + + // stores the internal Throwable as it will be set to null in the _close method + Throwable localThrowable = this.throwable; + + try { + _close(); + } catch (IOException ioe) { + _logger.log(Level.WARNING, CULoggerInfo.exceptionClosingStream, ioe); + } + + report(localThrowable); } /** * Report "left-overs"! */ - private void report(){ - _logger.log(Level.WARNING, CULoggerInfo.inputStreamFinalized, this.throwable); + private void report(Throwable localThrowable) { + _logger.log(Level.WARNING, CULoggerInfo.inputStreamFinalized, localThrowable); } } /**