Skip to content

Commit 01b6e2d

Browse files
committed
Fix resource leak
1 parent 8803ecb commit 01b6e2d

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MutableThreadContextMapFilter.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2222
import java.io.File;
2323
import java.io.FileInputStream;
24+
import java.io.IOException;
2425
import java.io.InputStream;
2526
import java.net.URI;
2627
import java.util.ArrayList;
@@ -425,38 +426,49 @@ private static ConfigResult getConfig(
425426
final PropertyEnvironment props,
426427
final SslConfiguration sslConfiguration) {
427428
final File inputFile = source.getFile();
429+
final ConfigResult configResult = new ConfigResult();
428430
InputStream inputStream = null;
429431
HttpInputStreamUtil.Result result = null;
430432
final long lastModified = source.getLastModified();
431-
if (inputFile != null && inputFile.exists()) {
432-
try {
433-
final long modified = inputFile.lastModified();
434-
if (modified > lastModified) {
435-
source.setLastModified(modified);
436-
inputStream = new FileInputStream(inputFile);
437-
result = new HttpInputStreamUtil.Result(Status.SUCCESS);
438-
} else {
439-
result = new HttpInputStreamUtil.Result(Status.NOT_MODIFIED);
433+
try {
434+
if (inputFile != null && inputFile.exists()) {
435+
try {
436+
final long modified = inputFile.lastModified();
437+
if (modified > lastModified) {
438+
source.setLastModified(modified);
439+
inputStream = new FileInputStream(inputFile);
440+
result = new HttpInputStreamUtil.Result(Status.SUCCESS);
441+
} else {
442+
result = new HttpInputStreamUtil.Result(Status.NOT_MODIFIED);
443+
}
444+
} catch (Exception ex) {
445+
result = new HttpInputStreamUtil.Result(Status.ERROR);
446+
}
447+
} else if (source.getURI() != null) {
448+
try {
449+
result = HttpInputStreamUtil.getInputStream(source, props, authorizationProvider, sslConfiguration);
450+
inputStream = result.getInputStream();
451+
} catch (ConfigurationException ex) {
452+
result = new HttpInputStreamUtil.Result(Status.ERROR);
440453
}
441-
} catch (Exception ex) {
442-
result = new HttpInputStreamUtil.Result(Status.ERROR);
454+
} else {
455+
result = new HttpInputStreamUtil.Result(Status.NOT_FOUND);
443456
}
444-
} else if (source.getURI() != null) {
445-
try {
446-
result = HttpInputStreamUtil.getInputStream(source, props, authorizationProvider, sslConfiguration);
447-
inputStream = result.getInputStream();
448-
} catch (ConfigurationException ex) {
449-
result = new HttpInputStreamUtil.Result(Status.ERROR);
457+
if (result.getStatus() == Status.SUCCESS) {
458+
LOGGER.debug("Processing Debug key/value pairs from: {}", source.toString());
459+
parseJsonConfiguration(inputStream, configResult);
460+
} else {
461+
configResult.status = result.getStatus();
462+
}
463+
} finally {
464+
if (inputStream != null) {
465+
try {
466+
inputStream.close();
467+
} catch (IOException e) {
468+
LOGGER.warn("Failed to close {}.", source, e);
469+
configResult.status = Status.ERROR;
470+
}
450471
}
451-
} else {
452-
result = new HttpInputStreamUtil.Result(Status.NOT_FOUND);
453-
}
454-
final ConfigResult configResult = new ConfigResult();
455-
if (result.getStatus() == Status.SUCCESS) {
456-
LOGGER.debug("Processing Debug key/value pairs from: {}", source.toString());
457-
parseJsonConfiguration(inputStream, configResult);
458-
} else {
459-
configResult.status = result.getStatus();
460472
}
461473
return configResult;
462474
}

0 commit comments

Comments
 (0)