Ensure removal of all ThreadLocal values ----------------------------------------
Key: OWB-466 URL: https://issues.apache.org/jira/browse/OWB-466 Project: OpenWebBeans Issue Type: Bug Affects Versions: 1.0.0-alpha-2 Reporter: Jakob Korherr Assignee: Gurkan Erdogdu While running our automated webapp tests with MyFaces CODI and OWB, I always got the following log entries: 30.09.2010 16:14:14 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap SCHWERWIEGEND: A web application created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.threadlo...@543c944f]) and a value of type [org.apache.webbeans.inject.impl.InjectionPointImpl] (value [Field Injection Point, field name : postConstructApplicationEvent, Bean Owner : [Name:systemEventBroadcaster,WebBeans Type:MANAGED,API Types:[java.lang.Object,org.apache.myfaces.extensions.cdi.jsf2.impl.listener.system.SystemEventBroadcaster],Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default,javax.inject.Named]]]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed. 30.09.2010 16:14:14 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap SCHWERWIEGEND: A web application created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.threadlo...@552cf9bd]) and a value of type [org.apache.webbeans.context.SessionContext] (value [org.apache.webbeans.context.sessioncont...@7bc012fa]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed. 30.09.2010 16:14:14 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap SCHWERWIEGEND: A web application created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.threadlo...@552cf9bd]) and a value of type [org.apache.webbeans.context.SessionContext] (value [org.apache.webbeans.context.sessioncont...@7f1e1a8e]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed. 30.09.2010 16:14:14 org.apache.coyote.http11.Http11Protocol destroy ..saying that Tomcat's webappclassloader had to remove some ThreadLocal values from the ThreadMap. I was able to track the affected ThreadLocal instances down: InstanceBean.local and WebContextsService.sessionContext. After digging into the code, I found out that there are many remove() operations on various ThreadLocal instances, but it was not thought that remove() only works for the current Thread. Thus when a ThreadLocal is set during a request it won't be removed when the container stops, because the shutdown-thread is clearly not the same as the request-thread. This means the request-ThreadLocal instances have to be removed after every request has ended - in requestDestroyed(ServletRequestEvent event). The provided patch adds some remove() calls in requestDestroyed() and also in afterStopApplication(). With this patch none of the above error-logs are shown in any of my test cases. However there might be some other ThreadLocals which should be cleaned up at every request, but this solution currently works for me! -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.