Skip to content

Commit 654bea2

Browse files
Jens Elknervladak
authored andcommitted
fix dtags.eftar file descriptor leak
fixes #535
1 parent 4733a97 commit 654bea2

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

src/org/opensolaris/opengrok/web/PageConfig.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.regex.Pattern;
4545
import javax.servlet.http.Cookie;
4646
import javax.servlet.http.HttpServletRequest;
47+
import javax.servlet.ServletRequest;
4748
import org.apache.commons.jrcs.diff.Diff;
4849
import org.apache.commons.jrcs.diff.DifferentiationFailedException;
4950
import org.opensolaris.opengrok.analysis.AnalyzerGuru;
@@ -58,10 +59,10 @@
5859
import org.opensolaris.opengrok.util.IOUtils;
5960

6061
/**
61-
* A simple container to lazy initialize common vars wrt. a single request. It
62-
* MUST NOT be shared between several requests and {@link #cleanup()} should be
63-
* called before the page context gets destroyed (e.g. by overwriting
64-
* {@code jspDestroy()} or when leaving the {@code service} method. <p> Purpose
62+
* A simple container to lazy initialize common vars wrt. a single request.
63+
* It MUST NOT be shared between several requests and
64+
* {@link #cleanup(ServletRequest)} should be called before the page context
65+
* gets destroyed (e.g.when leaving the {@code service} method). <p> Purpose
6566
* is to decouple implementation details from web design, so that the JSP
6667
* developer does not need to know every implementation detail and normally has
6768
* to deal with this class/wrapper, only (so some people may like to call this
@@ -1202,6 +1203,7 @@ public static PageConfig get(HttpServletRequest request) {
12021203
request.setAttribute(ATTR_NAME, pcfg);
12031204
return pcfg;
12041205
}
1206+
12051207
private static final String ATTR_NAME = PageConfig.class.getCanonicalName();
12061208
private HttpServletRequest req;
12071209

@@ -1210,17 +1212,24 @@ private PageConfig(HttpServletRequest req) {
12101212
}
12111213

12121214
/**
1213-
* Cleanup all allocated resources. Should always be called right before
1214-
* leaving the _jspService / service.
1215+
* Cleanup all allocated resources (if any) from the instance attached to
1216+
* the given request.
1217+
* @param sr request to check, cleanup. Ignored if {@code null}.
1218+
* @see PageConfig#get(HttpServletRequest)
1219+
*
12151220
*/
1216-
public void cleanup() {
1217-
if (req != null) {
1218-
req.removeAttribute(ATTR_NAME);
1219-
req = null;
1221+
public static void cleanup(ServletRequest sr) {
1222+
if (sr == null) {
1223+
return;
12201224
}
1221-
env = null;
1222-
if (eftarReader != null) {
1223-
eftarReader.close();
1225+
PageConfig cfg = (PageConfig) sr.getAttribute(ATTR_NAME);
1226+
if (cfg == null) {
1227+
return;
1228+
}
1229+
sr.removeAttribute(ATTR_NAME);
1230+
cfg.env = null;
1231+
if (cfg.eftarReader != null) {
1232+
cfg.eftarReader.close();
12241233
}
12251234
}
12261235
}

src/org/opensolaris/opengrok/web/WebappListener.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import javax.servlet.ServletContext;
3333
import javax.servlet.ServletContextEvent;
3434
import javax.servlet.ServletContextListener;
35+
import javax.servlet.ServletRequestEvent;
36+
import javax.servlet.ServletRequestListener;
3537
import org.opensolaris.opengrok.OpenGrokLogger;
3638
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
3739

@@ -40,7 +42,8 @@
4042
*
4143
* @author Trond Norbye
4244
*/
43-
public final class WebappListener implements ServletContextListener {
45+
public final class WebappListener
46+
implements ServletContextListener, ServletRequestListener {
4447

4548
@Override
4649
public void contextInitialized(final ServletContextEvent servletContextEvent) {
@@ -86,4 +89,20 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) {
8689
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
8790
RuntimeEnvironment.getInstance().stopConfigurationListenerThread();
8891
}
92+
93+
/**
94+
* {@inheritDoc}
95+
*/
96+
@Override
97+
public void requestDestroyed(ServletRequestEvent e) {
98+
PageConfig.cleanup(e.getServletRequest());
99+
}
100+
101+
/**
102+
* {@inheritDoc}
103+
*/
104+
@Override
105+
public void requestInitialized(ServletRequestEvent e) {
106+
// pass through
107+
}
89108
}

web/pageconfig.jspf

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,4 @@ Portions Copyright 2011 Jens Elkner.
2626
org.opensolaris.opengrok.web.PageConfig"
2727
%><%!
2828
private PageConfig cfg;
29-
/** Just cleanup cached objects */
30-
31-
public void jspDestroy() {
32-
if (cfg != null) {
33-
cfg.cleanup();
34-
}
35-
}
3629
%>

0 commit comments

Comments
 (0)