You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Latest Spring AOP + CGLib caches classes from the same classloader causing CCE in Grails [SPR-4153] #8831
In Grails we have a custom class loader that reloads classes when they are changed. Previously Spring's AOP support had no problem with this, but with Spring 2.5 and cglib proxies CGLib, what happens is when we reload a class we get a ClassCastException.
The problem appears to be the Enhancer class in CGLib, it will cache classes based on their originating ClassLoader. This may be fine for Java systems, but with Groovy and Grails we use a reloading capable class loader.
The problem is even bigger because there is no way in Spring to disable caching using the useCache property defined in AbstractClassGenerator.
If there was some way in ProxyFactoryBean and AbstractSingletonProxyFactoryBean to set to disable class caching in CGLib we could get round this. The original issue in the Grails JIRA is:
For the moment my solution is the wrap the classloader in another class loader by passing the class loader as a parent to a child classloader. But this may have other side effects and is generally undesirable.
Graeme, have you verified that setting useCache=false on the AbstractClassGenerator actually solves the problem for your scenario?
I guess we could also consider checking the ClassLoader and setting the useCache flag automatically: e.g. through a ReloadingClassLoader marker interface that Spring defines and checks for.
Yes I checked this. CGlib uses a map with the class loader as the key to look-up cache entries. A marker interface provided by Spring would also be a great solution.
I've introduced a SmartClassLoader marker interface, declaring a "boolean isClassReloadable(Class clazz)" operation. The AOP ProxyFactory caches CGLIB proxies only when underlying (Smart)ClassLoader does not indicate a reloadable Class. A ClassLoader may return true for all isClassReloadable calls, in which case no proxy classes will ever get cached. However, it is advisable to differentiate between actually reloadable classes (i.e. locally managed Groovy classes) and 'standard' classes (e.g. from a parent ClassLoader).
This should be available in the next 2.5.1 snapshot. Let me know whether it works for your purposes...
It appears that this issue was not actually solved. The Grails issue that originally caused this to be opened wasn't actually caused by this and was solved in a different way. However, I have now hit this actual issue with Grails and the SmartClassLoader solution does not fix the problem. The problem only occurs when classes are using aspect weaving, whereas the previous issue was regarding transactional proxies (I realise that this is also AOP, but the distinction will become clear).
In Grails, we can use Spring's @Transactional annotation to enable transaction handling on a particular method. When a change is made to the class and it is reloaded during development, all subsequent use produces a CCE. If the class is called TransactionalService the CCE states that TransactionalService cannot be cast to TransactionalService, meaning that something is holding on to an instance of the old class.
I have a test case in Grails that creates a bean with @Transactional and calls the transactional method. The class is then reloaded, redefined in the application context, then retrieved. When the transactional method is called on this reloaded version we get the CCE. In the output below I have caught the CCE and grabbed it's trace.
java.lang.ClassCastException: TransactionalService cannot be cast to TransactionalService
at TransactionalService$$FastClassByCGLIB$$110a170c.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$FixedChainStaticTargetInterceptor.intercept(Cglib2AopProxy.java:572)
at TransactionalService$$EnhancerByCGLIB$$99063dc4_2.myMethod(<generated>)
We are using a SmartClassLoader, which is evident by the _2 suffix on the TransactionalService$$EnhancerByCGLIB$$99063dc4 class. I am also using a custom AnnotationAwareAspectJAutoProxyCreator that overrides getCacheKey() to create keys based on the hash code of the given class, so reloaded classes yield different keys. Neither of these factors seem to have any impact.
If I then try to reload the class again and invoke the method, I get the following:
at TransactionalService$$FastClassByCGLIB$$110a170c.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$FixedChainStaticTargetInterceptor.intercept(Cglib2AopProxy.java:572)
at TransactionalService$$EnhancerByCGLIB$$99063dc4_3.myMethod(<generated>)
Notice that the FastClassByCGLIB at the top of the stack seems to be of the same class. It appears that cglib (or Spring?) is still doing some kind of caching here even though setUseCache(false) is definitely being called on the Enhancer instance created by the Cglib2AopProxy.
If I use a new class loader when creating the proxy for the reloaded version everything works as it should, but it should be possible to achieve reloading without resorting to that I would think.
I have also tried upgrading grails from cglib 2.1_3 to 2.2 but this made no difference.
Hi, i'm facing exactly the same problem as described by Luke. I tried to implement the SmartClassLoader too, which didn't solved the problem as well.
Chris, do you still need a reproduction project, or is it going to be solved only upon #12840?
PS: I'm using cglib 2.2.2 and spring 3.0.6 here.
UPDATE: After debugging a bit harder, i found out that the problem in on FastClass$Generator.create() call which is not taking the original Enhacer cache flag into account. Now It seems majorly a cglib bug.
Would it be possible for you to confirm whether you still see this behavior against Spring Framework 3.2.1? Note than this version now includes its own embedded repackaging of CGLIB 3.0, meaning you can eliminate your dependency on CGLIB 2.2.2 entirely.
Activity
spring-projects-issues commentedon Nov 22, 2007
Juergen Hoeller commented
Graeme, have you verified that setting useCache=false on the AbstractClassGenerator actually solves the problem for your scenario?
I guess we could also consider checking the ClassLoader and setting the useCache flag automatically: e.g. through a ReloadingClassLoader marker interface that Spring defines and checks for.
Juergen
spring-projects-issues commentedon Nov 22, 2007
Graeme Rocher commented
Yes I checked this. CGlib uses a map with the class loader as the key to look-up cache entries. A marker interface provided by Spring would also be a great solution.
Thanks
Graeme
spring-projects-issues commentedon Nov 24, 2007
Juergen Hoeller commented
I've introduced a SmartClassLoader marker interface, declaring a "boolean isClassReloadable(Class clazz)" operation. The AOP ProxyFactory caches CGLIB proxies only when underlying (Smart)ClassLoader does not indicate a reloadable Class. A ClassLoader may return true for all isClassReloadable calls, in which case no proxy classes will ever get cached. However, it is advisable to differentiate between actually reloadable classes (i.e. locally managed Groovy classes) and 'standard' classes (e.g. from a parent ClassLoader).
This should be available in the next 2.5.1 snapshot. Let me know whether it works for your purposes...
Juergen
spring-projects-issues commentedon Oct 19, 2010
Luke Daley commented
It appears that this issue was not actually solved. The Grails issue that originally caused this to be opened wasn't actually caused by this and was solved in a different way. However, I have now hit this actual issue with Grails and the SmartClassLoader solution does not fix the problem. The problem only occurs when classes are using aspect weaving, whereas the previous issue was regarding transactional proxies (I realise that this is also AOP, but the distinction will become clear).
In Grails, we can use Spring's
@Transactionalannotation to enable transaction handling on a particular method. When a change is made to the class and it is reloaded during development, all subsequent use produces a CCE. If the class is called TransactionalService the CCE states that TransactionalService cannot be cast to TransactionalService, meaning that something is holding on to an instance of the old class.I have a test case in Grails that creates a bean with
@Transactionaland calls the transactional method. The class is then reloaded, redefined in the application context, then retrieved. When the transactional method is called on this reloaded version we get the CCE. In the output below I have caught the CCE and grabbed it's trace.We are using a SmartClassLoader, which is evident by the _2 suffix on the TransactionalService$$EnhancerByCGLIB$$99063dc4 class. I am also using a custom AnnotationAwareAspectJAutoProxyCreator that overrides getCacheKey() to create keys based on the hash code of the given class, so reloaded classes yield different keys. Neither of these factors seem to have any impact.
If I then try to reload the class again and invoke the method, I get the following:
Notice that the FastClassByCGLIB at the top of the stack seems to be of the same class. It appears that cglib (or Spring?) is still doing some kind of caching here even though setUseCache(false) is definitely being called on the Enhancer instance created by the Cglib2AopProxy.
If I use a new class loader when creating the proxy for the reloaded version everything works as it should, but it should be possible to achieve reloading without resorting to that I would think.
I have also tried upgrading grails from cglib 2.1_3 to 2.2 but this made no difference.
spring-projects-issues commentedon Jun 20, 2012
Chris Beams commented
Hi Luke,
Catching up on this one - is it still relevant for you? If so, would it be possible to provide a reproduction project? Thanks.
spring-projects-issues commentedon Feb 12, 2013
Luiz Henrique Rolim commented
Hi, i'm facing exactly the same problem as described by Luke. I tried to implement the SmartClassLoader too, which didn't solved the problem as well.
Chris, do you still need a reproduction project, or is it going to be solved only upon #12840?
PS: I'm using cglib 2.2.2 and spring 3.0.6 here.
UPDATE: After debugging a bit harder, i found out that the problem in on FastClass$Generator.create() call which is not taking the original Enhacer cache flag into account. Now It seems majorly a cglib bug.
spring-projects-issues commentedon Feb 27, 2013
Chris Beams commented
Hi Luiz,
Would it be possible for you to confirm whether you still see this behavior against Spring Framework 3.2.1? Note than this version now includes its own embedded repackaging of CGLIB 3.0, meaning you can eliminate your dependency on CGLIB 2.2.2 entirely.
Thanks.
spring-projects-issues commentedon Nov 4, 2014
Sébastien Deleuze commented
Graeme Rocher I plan to close this very old issue, please comment if it is already relevant.