Skip to content

Commit f5d35e1

Browse files
committed
Fix resource leak in ThingFactory class
Change ThingFactory class to use try-with-resource statement to manage the InputStream used to read the file "thing.properties", so that it's (always) closed. The issue manifested while exercising, a lot, the OWASP Benchmark application with OWASP ZAP (for zaproxy/zaproxy#1890). After many requests sent Benchmark started to fail to fulfil ZAP's requests. In the output of Tomcat there were many entries like: [INFO] [talledLocalContainer] Can't find thing.properties and also: [INFO] [talledLocalContainer] SEVERE: Socket accept failed [INFO] [talledLocalContainer] java.io.IOException: Too many open files the reason why ZAP's requests were no longer fulfilled. Other attempts to read other files also led to same issue, for example: ...webapps/benchmark/WEB-INF/classes/employees.xml (Too many open files) Looking at the list of Benchmark's open files it was evident that the leak was related to read of "thing.properties" file, given how many open "thing.properties" files there were. After the change OWASP Benchmark was able to withstand more than twice the requests without exhibiting the aforementioned issue.
1 parent 5ec064e commit f5d35e1

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

src/main/java/org/owasp/benchmark/helpers/ThingFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public static ThingInterface createThing() {
2929
Properties props = new Properties();
3030

3131
// create a thing using reflection
32-
try {
33-
InputStream thingproperties = ThingFactory.class.getClassLoader().getResourceAsStream("thing.properties");
32+
try (InputStream thingproperties = ThingFactory.class.getClassLoader().getResourceAsStream("thing.properties")) {
3433
if (thingproperties == null) {
3534
System.out.println("Can't find thing.properties");
3635
return new Thing2();

0 commit comments

Comments
 (0)