Review of all calls to user code prior to calling InstanceManager.destroy()
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1750995 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/java/org/apache/catalina/core/ApplicationFilterConfig.java b/java/org/apache/catalina/core/ApplicationFilterConfig.java
index 0899fd0..43318a3 100644
--- a/java/org/apache/catalina/core/ApplicationFilterConfig.java
+++ b/java/org/apache/catalina/core/ApplicationFilterConfig.java
@@ -300,8 +300,7 @@
unregisterJMX();
- if (this.filter != null)
- {
+ if (this.filter != null) {
try {
if (Globals.IS_SECURITY_ENABLED) {
try {
diff --git a/java/org/apache/catalina/core/LocalStrings.properties b/java/org/apache/catalina/core/LocalStrings.properties
index e707374..0bbaf26 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -204,6 +204,7 @@
standardWrapper.allocateException=Allocate exception for servlet {0}
standardWrapper.deallocateException=Deallocate exception for servlet {0}
standardWrapper.destroyException=Servlet.destroy() for servlet {0} threw exception
+standardWrapper.destroyInstance=InstanceManager.destroy() for servlet [{0}] threw exception
standardWrapper.initException=Servlet.init() for servlet {0} threw exception
standardWrapper.instantiate=Error instantiating servlet class {0}
standardWrapper.isUnavailable=Servlet {0} is currently unavailable
diff --git a/java/org/apache/catalina/core/StandardWrapper.java b/java/org/apache/catalina/core/StandardWrapper.java
index 13d462e..75999dc 100644
--- a/java/org/apache/catalina/core/StandardWrapper.java
+++ b/java/org/apache/catalina/core/StandardWrapper.java
@@ -1370,10 +1370,6 @@
instance.destroy();
}
- // Annotation processing
- if (!((Context) getParent()).getIgnoreAnnotations()) {
- ((StandardContext)getParent()).getInstanceManager().destroyInstance(instance);
- }
} catch (Throwable t) {
t = ExceptionUtils.unwrapInvocationTargetException(t);
ExceptionUtils.handleThrowable(t);
@@ -1386,6 +1382,15 @@
(sm.getString("standardWrapper.destroyException", getName()),
t);
} finally {
+ // Annotation processing
+ if (!((Context) getParent()).getIgnoreAnnotations()) {
+ try {
+ ((Context)getParent()).getInstanceManager().destroyInstance(instance);
+ } catch (Throwable t) {
+ ExceptionUtils.handleThrowable(t);
+ log.error(sm.getString("standardWrapper.destroyInstance", getName()), t);
+ }
+ }
// Write captured output
if (swallowOutput) {
String log = SystemLogHandler.stopCapture();
diff --git a/java/org/apache/coyote/AbstractProtocol.java b/java/org/apache/coyote/AbstractProtocol.java
index 10de2f8..ebd3890 100644
--- a/java/org/apache/coyote/AbstractProtocol.java
+++ b/java/org/apache/coyote/AbstractProtocol.java
@@ -880,8 +880,13 @@
ClassLoader oldCL = upgradeToken.getContextBind().bind(false, null);
try {
httpUpgradeHandler.destroy();
- instanceManager.destroyInstance(httpUpgradeHandler);
} finally {
+ try {
+ instanceManager.destroyInstance(httpUpgradeHandler);
+ } catch (Throwable e) {
+ ExceptionUtils.handleThrowable(e);
+ getLog().error(sm.getString("abstractConnectionHandler.error"), e);
+ }
upgradeToken.getContextBind().unbind(false, oldCL);
}
}
diff --git a/java/org/apache/jasper/runtime/TagHandlerPool.java b/java/org/apache/jasper/runtime/TagHandlerPool.java
index 5d2c9f9..bef2f26 100644
--- a/java/org/apache/jasper/runtime/TagHandlerPool.java
+++ b/java/org/apache/jasper/runtime/TagHandlerPool.java
@@ -144,15 +144,7 @@
}
}
// There is no need for other threads to wait for us to release
- handler.release();
- try {
- instanceManager.destroyInstance(handler);
- } catch (Exception e) {
- Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
- ExceptionUtils.handleThrowable(t);
- log.warn("Error processing preDestroy on tag instance of " +
- handler.getClass().getName(), t);
- }
+ doRelease(handler);
}
/**
@@ -161,19 +153,30 @@
*/
public synchronized void release() {
for (int i = current; i >= 0; i--) {
- Tag handler = handlers[i];
- handler.release();
- try {
- instanceManager.destroyInstance(handler);
- } catch (Exception e) {
- Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
- ExceptionUtils.handleThrowable(t);
- log.warn("Error processing preDestroy on tag instance of "
- + handler.getClass().getName(), t);
- }
+ doRelease(handlers[i]);
}
}
+
+ private void doRelease(Tag handler) {
+ try {
+ handler.release();
+ } catch (Throwable t) {
+ ExceptionUtils.handleThrowable(t);
+ log.warn("Error processing release on tag instance of "
+ + handler.getClass().getName(), t);
+ }
+ try {
+ instanceManager.destroyInstance(handler);
+ } catch (Exception e) {
+ Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
+ ExceptionUtils.handleThrowable(t);
+ log.warn("Error processing preDestroy on tag instance of "
+ + handler.getClass().getName(), t);
+ }
+ }
+
+
protected static String getOption(ServletConfig config, String name,
String defaultV) {
if (config == null)
diff --git a/java/org/apache/tomcat/websocket/WsSession.java b/java/org/apache/tomcat/websocket/WsSession.java
index de8815f..14fb41f 100644
--- a/java/org/apache/tomcat/websocket/WsSession.java
+++ b/java/org/apache/tomcat/websocket/WsSession.java
@@ -537,24 +537,36 @@
private void fireEndpointOnClose(CloseReason closeReason) {
// Fire the onClose event
+ Throwable throwable = null;
InstanceManager instanceManager = webSocketContainer.getInstanceManager();
+ if (instanceManager == null) {
+ instanceManager = InstanceManagerBindings.get(applicationClassLoader);
+ }
Thread t = Thread.currentThread();
ClassLoader cl = t.getContextClassLoader();
t.setContextClassLoader(applicationClassLoader);
try {
localEndpoint.onClose(this, closeReason);
- if (instanceManager == null) {
- instanceManager = InstanceManagerBindings.get(applicationClassLoader);
- }
- if (instanceManager != null) {
- instanceManager.destroyInstance(localEndpoint);
- }
- } catch (Throwable throwable) {
- ExceptionUtils.handleThrowable(throwable);
- localEndpoint.onError(this, throwable);
+ } catch (Throwable t1) {
+ ExceptionUtils.handleThrowable(t1);
+ throwable = t1;
} finally {
+ if (instanceManager != null) {
+ try {
+ instanceManager.destroyInstance(localEndpoint);
+ } catch (Throwable t2) {
+ ExceptionUtils.handleThrowable(t2);
+ if (throwable == null) {
+ throwable = t2;
+ }
+ }
+ }
t.setContextClassLoader(cl);
}
+
+ if (throwable != null) {
+ fireEndpointOnError(throwable);
+ }
}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index fe2ff15..71144ca 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -84,6 +84,11 @@
at that point and the web resources may not be correctly configured.
(markt)
</fix>
+ <fix>
+ Improve error handling around user code prior to calling
+ <code>InstanceManager.destroy()</code> to ensure that the method is
+ executed. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">
@@ -110,14 +115,19 @@
secure HTTP/2 connections that meant an incorrect value was retured when
using the default port. (markt)
</fix>
+ <fix>
+ Improve error handling around user code prior to calling
+ <code>InstanceManager.destroy()</code> to ensure that the method is
+ executed. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Jasper">
<changelog>
<fix>
- Catch and log any <code>Exception</code>s during calls to
- <code>Servlet.destroy()</code> when destroying the Servlet associated
- with a JSP page. (markt)
+ Improve error handling around user code prior to calling
+ <code>InstanceManager.destroy()</code> to ensure that the method is
+ executed. (markt)
</fix>
</changelog>
</subsection>
@@ -129,6 +139,11 @@
dedicated WebSocket Executor. It has been replaced by the use of the
Connector/Endpoint provided Executor. (markt)
</scode>
+ <fix>
+ Improve error handling around user code prior to calling
+ <code>InstanceManager.destroy()</code> to ensure that the method is
+ executed. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Web Applications">