每次关闭tomcat时就会出错误
七月 16, 2014 5:01:35 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [] created a ThreadLocal with key of type [java.lang.ThreadLocal]
(value [java.lang.ThreadLocal@7da150]) and a value of type [org.r113.servlet3.MyClass]
(value [org.r113.servlet3.MyClass@37e98b70]) but failed to remove it when the web application was stopped.
Threads are going to be renewed over time to try and avoid a probable memory leak.
环境tomcat7.0.47 jdk 7u55
这个是一个tomcat的 listener 报出来的. conf/server.xml 当中有一个listener
32:
从异常上看. org.r113.servlet3.MyClass 这个 servlet. 使用到了 java.lang.ThreadLocal 的上下文. 在servlet destory的时候没有被 unset ...
这个没什么问题,只是tomcat 7 自己报出的一个警告类信息。
sorry ,其实想发的是dubbo的,每次关闭tomcat时,JVM进程都会hang住.
有一种情况是dubbo的RpcContext ThreadLocal 没有释放,就是下面的strace.
Nov 04, 2014 1:21:50 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [] created a ThreadLocal with key of type [com.alibaba.dubbo.rpc.RpcContext$1]
(value [com.alibaba.dubbo.rpc.RpcContext$1@5c08b06e]) and a value of type [com.alibaba.dubbo.rpc.RpcContext]
(value [com.alibaba.dubbo.rpc.RpcContext@3e7734f6]) but failed to remove it when the web application was stopped.
Threads are going to be renewed over time to try and avoid a probable memory leak.
com.alibaba.dubbo.rpc.RpcContext里 getContext()和removeContext() 这两个方法分别是ThreadLocal 的get 和remove()
我在项目页面https://github.com/alibaba/dubbo/blob/977bd905c306d3ce08994eec616b391432275d4d/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ContextFilter.java 找到了调用removeContext() 的地方,所以搞不明白为什么ThreadLocal 没有释放
还有一种情况,大概是这样
WARN] 2014-11-26 15:49:09,190 [DubboShutdownHook] transport.netty.NettyServer com.alibaba.dubbo.remoting.transport.netty.NettyServer.doClose(NettyServer.java:126) - [DUBBO] org/jboss/netty/util/internal/ExecutorUtil, dubbo version: 2.4.9, current host: 10.1.1.1
java.lang.NoClassDefFoundError: org/jboss/netty/util/internal/ExecutorUtil
at org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory.releaseExternalResources(NioServerSocketChannelFactory.java:146)
at org.jboss.netty.bootstrap.Bootstrap.releaseExternalResources(Bootstrap.java:324)
at com.alibaba.dubbo.remoting.transport.netty.NettyServer.doClose(NettyServer.java:123)
at com.alibaba.dubbo.remoting.transport.AbstractServer.close(AbstractServer.java:155)
at com.alibaba.dubbo.remoting.transport.AbstractServer.close(AbstractServer.java:163)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer.close(HeaderExchangeServer.java:121)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.destroy(DubboProtocol.java:395)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.destroy(ProtocolListenerWrapper.java:72)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.destroy(ProtocolFilterWrapper.java:66)
at com.alibaba.dubbo.config.ProtocolConfig.destroyAll(ProtocolConfig.java:416)
at com.alibaba.dubbo.config.AbstractConfig$1.run(AbstractConfig.java:452)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.jboss.netty.util.internal.ExecutorUtil
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
... 12 more
https://issues.apache.org/bugzilla/show_bug.cgi?id=56387 这里有关于这个异常的讨论,大致说tomcat shutdown后,netty仍然加载从webappclassloder中加载类,导致异常. 代码也在项目里https://github.com/alibaba/dubbo/blob/977bd905c306d3ce08994eec616b391432275d4d/dubbo-remoting/dubbo-remoting-netty/src/main/java/com/alibaba/dubbo/remoting/transport/netty/NettyServer.java#L100 找到了,124行的 bootstrap.releaseExternalResources();
其实我想知道的是 为什么 不能dubbo正常释放资源,我每次shutdown后需要在OS里kill掉JVM进程
hi,我是给tomcat提bug的那位qiuboboy.
如果dubbo应用在容器中运行,dubbo没有提供容器销毁时的钩子,只用了shutdownhook.会导致上面的问题.
你可以在ServletContextListener#contextDestroyed里面调用:
final Class protocolConfig = Class.forName("com.alibaba.dubbo.config.ProtocolConfig");
final Method method = protocolConfig.getMethod("destroyAll");
method.invoke(protocolConfig);
来主动关闭dubbo.
dubbo的优雅停机也有问题.我们内部版本已经修复.以后我们会把这些修改提交到dubbox上去.
建议shutdownhook最好也慎用,我们的应用会在容器关闭时,主动去调用shutdownhook.
static {
//add dubbo shutdown hook
try {
final Class protocolConfig = Class.forName("com.alibaba.dubbo.config.ProtocolConfig");
final Method method = protocolConfig.getMethod("destroyAll");
addShutdownHook(new Runnable() {
@Override
public void run() {
try {
method.invoke(protocolConfig);
} catch (Exception e) {
throw Exceptions.newRuntimeExceptionWithoutStackTrace(e);
}
}
}, "DubboShutdownHook");
} catch (Exception e) {
//ignore
}
}
static { Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { if (logger.isInfoEnabled()) { logger.info("Run shutdown hook now."); } ProtocolConfig.destroyAll(); } }, "DubboShutdownHook")); }
如果ShutdownHook不能生效,可以自行调用:
ProtocolConfig.destroyAll(); http://alibaba.github.io/dubbo-doc-static/Graceful+Shutdown-zh.htm
thanks very much~ 十分感谢
@bohrqiu 请问直接注释掉 shutdownhook 是否可行?测试了下在tomcat中能正常关闭
/*static {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
if (logger.isInfoEnabled()) {
logger.info("Run shutdown hook now.");
}
ProtocolConfig.destroyAll();
}
}, "DubboShutdownHook"));
}*/
@bohrqiu 按照你说的办法,我这边报错:
信息: Illegal access: this web application instance has been stopped already. Could not load org.apache.zookeeper.server.ZooTrace. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1196)
三月 07, 2015 9:27:40 上午 org.apache.catalina.loader.WebappClassLoader loadClass
信息: Illegal access: this web application instance has been stopped already. Could not load org.apache.log4j.spi.ThrowableInformation. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:165)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.error(Category.java:322)
at org.apache.zookeeper.ClientCnxn$1.uncaughtException(ClientCnxn.java:422)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1986)
可以指导一下么?不胜感激
@kazaff 你是怎么做的呢
@bohrqiu 首先谢谢答复。 web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/consumer.xml</param-value>
</context-param>
<listener>
<listener-class>cn.ztydata.listener.DubboShutDownListener</listener-class>
</listener>
<listener>
<listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
按照你的方法,我绑定了一个自己实现的监听器,源码如下:
public class DubboShutDownListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0){
//System.out.println("DubboShutDownListener.contextDestroyed called!");
try {
final Class protocolConfig = Class.forName("com.alibaba.dubbo.config.ProtocolConfig");
final Method method = protocolConfig.getMethod("destroyAll");
method.invoke(protocolConfig);
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public void contextInitialized(ServletContextEvent arg0){
//System.out.println("DubboShutDownListener.contextInitialized called!");
}
}
也曾尝试过在web.xml中修改监听器的位置,不过都无法解决该报错~
@kazaff 你试着打断点看看,异常信息是在DubboShutDownListener执行过程中打印出来的么?
@bohrqiu 我还真不知道该咋单步测试这个方法,因为在ide里点击stop按钮后,就直接关闭了debug进程了,没停在断点位置,但不确定这说明是异常不来自这里,还是说明这种操作方式无法触发断点。我只能把详细日志信息贴一下:
"D:\Program Files\apache-tomcat-7.0.53\bin\catalina.bat" stop
Disconnected from the target VM, address: '127.0.0.1:65191', transport: 'socket'
Using CATALINA_BASE: "C:\Users\Administrator\.IntelliJIdea14\system\tomcat\Unnamed_na-rest_api"
Using CATALINA_HOME: "D:\Program Files\apache-tomcat-7.0.53"
Using CATALINA_TMPDIR: "D:\Program Files\apache-tomcat-7.0.53\temp"
Using JRE_HOME: "D:\Program Files\Java\jdk1.7.0_55"
Using CLASSPATH: "D:\Program Files\apache-tomcat-7.0.53\bin\bootstrap.jar;D:\Program Files\apache-tomcat-7.0.53\bin\tomcat-juli.jar"
三月 08, 2015 11:22:12 上午 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
三月 08, 2015 11:22:12 上午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-apr-8080"]
三月 08, 2015 11:22:12 上午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-apr-8009"]
三月 08, 2015 11:22:12 上午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO support.XmlWebApplicationContext: Closing WebApplicationContext for namespace 'na-rest_api-servlet': startup date [Sun Mar 08 11:20:12 CST 2015]; parent: Root WebApplicationContext
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO support.DefaultListableBeanFactory: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f290193: defining beans [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,demo,userImplement,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,exceptionResolver,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@2209ad9a
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO support.XmlWebApplicationContext: Closing Root WebApplicationContext: startup date [Sun Mar 08 11:19:44 CST 2015]; root of context hierarchy
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO support.DefaultListableBeanFactory: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2209ad9a: defining beans [na-rest_api,com.alibaba.dubbo.config.RegistryConfig,userRemote]; root of factory hierarchy
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO zookeeper.ZookeeperRegistry: [DUBBO] Unsubscribe: consumer://192.168.137.23/cn.ztydata.User?application=na-rest_api&category=providers,configurators,routers&dubbo=2.8.3&interface=cn.ztydata.User&methods=who&organization=cn.ztydata.developer&owner=kazaff&pid=8440&revision=api&side=consumer×tamp=1425784824666, dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO netty.NettyChannel: [DUBBO] Close netty channel [id: 0x41bfa8c8, /192.168.137.23:55447 => /192.168.137.23:20880], dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] DubboSharedHandler-thread-1 INFO dubbo.DubboProtocol: [DUBBO] disconected from /192.168.137.23:20880,url:dubbo://192.168.137.23:20880/cn.ztydata.User?anyhost=true&application=na-rest_api&check=false&codec=dubbo&dubbo=2.8.3&generic=false&heartbeat=60000&interface=cn.ztydata.User&methods=who&organization=cn.ztydata.developer&owner=kazaff&pid=8440&revision=api&side=consumer×tamp=1425784824666, dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO support.AbstractRegistryFactory: [DUBBO] Close all registries [zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=na-rest_api&dubbo=2.8.3&interface=com.alibaba.dubbo.registry.RegistryService&organization=cn.ztydata.developer&owner=kazaff&pid=8440×tamp=1425784825891], dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO zookeeper.ZookeeperRegistry: [DUBBO] Destroy registry:zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=na-rest_api&dubbo=2.8.3&interface=com.alibaba.dubbo.registry.RegistryService&organization=cn.ztydata.developer&owner=kazaff&pid=8440×tamp=1425784825891, dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO zookeeper.ZookeeperRegistry: [DUBBO] Unregister: consumer://192.168.137.23/cn.ztydata.User?application=na-rest_api&category=consumers&check=false&dubbo=2.8.3&interface=cn.ztydata.User&methods=who&organization=cn.ztydata.developer&owner=kazaff&pid=8440&revision=api&side=consumer×tamp=1425784824666, dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO zookeeper.ZookeeperRegistry: [DUBBO] Destroy unregister url consumer://192.168.137.23/cn.ztydata.User?application=na-rest_api&category=consumers&check=false&dubbo=2.8.3&interface=cn.ztydata.User&methods=who&organization=cn.ztydata.developer&owner=kazaff&pid=8440&revision=api&side=consumer×tamp=1425784824666, dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] ZkClient-EventThread-43-127.0.0.1:2181 INFO zkclient.ZkEventThread: Terminate ZkClient event thread.
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO zookeeper.ZooKeeper: Session: 0x14bf75a317c0005 closed
[08/03/15 11:22:12:012 CST] RMI TCP Connection(5)-127.0.0.1-EventThread INFO zookeeper.ClientCnxn: EventThread shut down
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO dubbo.DubboProtocol: [DUBBO] Close dubbo connect: 192.168.137.23:0-->192.168.137.23:20880, dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] localhost-startStop-2 INFO dubbo.DubboProtocol: [DUBBO] Close dubbo connect: 192.168.137.23:0-->192.168.137.23:20880, dubbo version: 2.8.3, current host: 192.168.137.23
三月 08, 2015 11:22:12 上午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [] created a ThreadLocal with key of type [com.alibaba.dubbo.rpc.RpcContext$1] (value [com.alibaba.dubbo.rpc.RpcContext$1@337b6361]) and a value of type [com.alibaba.dubbo.rpc.RpcContext] (value [com.alibaba.dubbo.rpc.RpcContext@69b98fae]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
三月 08, 2015 11:22:12 上午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [] created a ThreadLocal with key of type [com.alibaba.dubbo.rpc.RpcContext$1] (value [com.alibaba.dubbo.rpc.RpcContext$1@337b6361]) and a value of type [com.alibaba.dubbo.rpc.RpcContext] (value [com.alibaba.dubbo.rpc.RpcContext@15881ebc]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
三月 08, 2015 11:22:12 上午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["http-apr-8080"]
三月 08, 2015 11:22:12 上午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["ajp-apr-8009"]
三月 08, 2015 11:22:12 上午 org.apache.catalina.loader.WebappClassLoader loadClass
信息: Illegal access: this web application instance has been stopped already. Could not load org.apache.zookeeper.server.ZooTrace. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1196)
三月 08, 2015 11:22:12 上午 org.apache.catalina.loader.WebappClassLoader loadClass
信息: Illegal access: this web application instance has been stopped already. Could not load org.apache.log4j.spi.ThrowableInformation. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:165)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.error(Category.java:322)
at org.apache.zookeeper.ClientCnxn$1.uncaughtException(ClientCnxn.java:422)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1986)
三月 08, 2015 11:22:12 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["http-apr-8080"]
三月 08, 2015 11:22:12 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["ajp-apr-8009"]
[08/03/15 11:22:12:012 CST] DubboShutdownHook INFO config.AbstractConfig: [DUBBO] Run shutdown hook now., dubbo version: 2.8.3, current host: 192.168.137.23
[08/03/15 11:22:12:012 CST] DubboShutdownHook INFO support.AbstractRegistryFactory: [DUBBO] Close all registries [], dubbo version: 2.8.3, current host: 192.168.137.23
Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "RMI TCP Connection(127.0.0.1:2181)"
Disconnected from server
有什么线索么?
@kazaff 修改下试试呢:
public class DubboShutDownListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0){
arg0.getServletContext().log("DubboShutDownListener.contextDestroyed called!");
try {
final Class protocolConfig = Class.forName("com.alibaba.dubbo.config.ProtocolConfig");
final Method method = protocolConfig.getMethod("destroyAll");
method.invoke(protocolConfig);
} catch (Exception e){
arg0.getServletContext().log("destory dubbo failed:"+e.getMessage(), e);
}
}
@Override
public void contextInitialized(ServletContextEvent arg0){
//System.out.println("DubboShutDownListener.contextInitialized called!");
}
}
@bohrqiu 还是老样子,报错信息我比对了一下,并无差别~~如果使用内嵌的tomcat方式部署,就不会有这个问题了,也不需要自己写对应的监听器了啊~
从这里看的话,dubbo中DefaultFutrue.java的L309一定会造成tomcat的leak提醒,不管怎么写都不会避免吧?
@kazaff "DubboShutDownListener.contextDestroyed called!" 这行日志打印在什么位置?
@bohrqiu sorry,我在IDE的控制台上根本就没有找到这行日志,我不太明白你那么写的初衷是啥?可否给予解释~~再次感谢
@kazaff 我是想知道ProtocolConfig.destroyAll是在ServletContextListener里触发的还是在ShutdownHook中触发的。如果在ServletContextListener里触发还有问题,说明destroyAll时有些资源没有清理干净。
@bohrqiu 哦,了解了,不过按照我直接向控制台打印log的方式处理的话,确实是在报错之前能看到打印的那句log,这是否能说明一些问题?
而且我还有个连带问题,就是tomcat结合下,我都没配好log4j,导致没有办法查阅dubbo的debug信息,而在内嵌tomcat的环境下是完全没问题的。
这也可能导致你的** arg0.getServletContext().log("DubboShutDownListener.contextDestroyed called!");**无法正常显示吧?对么?
@liuyangc3 HI,朋友你的问题解决了吗?我也遇到相同的问题