Skip to content

Commit 3fde88b

Browse files
committed
Add failure test and changelog entry
Add testNoJarFileLeakOnFailure to verify that a failed JAR configuration load (missing entry) does not leak file descriptors or hold a lock on the JAR file. Add changelog entry for the ConfigurationSource URL leak fix.
1 parent 75388ff commit 3fde88b

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ void testLoadConfigurationSourceFromJarFile() throws Exception {
104104
}
105105
}
106106

107+
/**
108+
* Verifies that a failed JAR configuration load (missing entry) does not leak file descriptors
109+
* or hold a lock on the JAR file.
110+
*/
111+
@Test
112+
void testNoJarFileLeakOnFailure() throws Exception {
113+
final Path jarFile = prepareJarConfigURL(tempDir);
114+
// Path inside JAR that does NOT exist
115+
final URL brokenJarConfigURL = new URL("jar:" + jarFile.toUri().toURL() + "!/missing/file.xml");
116+
final long expectedFdCount = getOpenFileDescriptorCount();
117+
final ConfigurationSource configSource = ConfigurationSource.fromUri(brokenJarConfigURL.toURI());
118+
assertNull(configSource, "Source should be null for a missing JAR entry");
119+
// This can only fail on UNIX
120+
assertEquals(expectedFdCount, getOpenFileDescriptorCount(), "File descriptors leaked after failed JAR load");
121+
// This can only fail on Windows
122+
try {
123+
Files.delete(jarFile);
124+
} catch (IOException e) {
125+
fail("JAR file locked after failed load: " + e.getMessage());
126+
}
127+
}
128+
107129
private long getOpenFileDescriptorCount() {
108130
final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
109131
if (os instanceof UnixOperatingSystemMXBean) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="4127" link="https://github.com/apache/logging-log4j2/pull/4127"/>
7+
<description format="asciidoc">Fix resource leaks in `ConfigurationSource` when loading configuration via URL fails</description>
8+
</entry>

0 commit comments

Comments
 (0)