Skip to content

Commit

Permalink
Removes internal throable reference to avoid potential ClassLoader leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Tabin authored and ctabin committed Feb 17, 2023
1 parent 82ff339 commit 8b21df0
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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.
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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);
}
}
/**
Expand Down

0 comments on commit 8b21df0

Please sign in to comment.