Skip to content

Latest Spring AOP + CGLib caches classes from the same classloader causing CCE in Grails [SPR-4153] #8831

Closed

Description

@spring-projects-issues

Graeme Rocher opened SPR-4153 and commented

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:

http://jira.codehaus.org/browse/GRAILS-1771

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.


Affects: 2.5 final

Issue Links:

3 votes, 5 watchers

Activity

  1. spring-projects-issues commented on Nov 22, 2007

    @spring-projects-issues
    CollaboratorAuthor

    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

  2. spring-projects-issues commented on Nov 22, 2007

    @spring-projects-issues
    CollaboratorAuthor

    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

  3. spring-projects-issues commented on Nov 24, 2007

    @spring-projects-issues
    CollaboratorAuthor

    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

  4. spring-projects-issues commented on Oct 19, 2010

    @spring-projects-issues
    CollaboratorAuthor

    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 @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.

  5. spring-projects-issues commented on Jun 20, 2012

    @spring-projects-issues
    CollaboratorAuthor

    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.

  6. spring-projects-issues commented on Feb 12, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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.

  7. spring-projects-issues commented on Feb 27, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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.

  8. spring-projects-issues commented on Nov 4, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sébastien Deleuze commented

    Graeme Rocher I plan to close this very old issue, please comment if it is already relevant.

  9. added
    type: bug
    in: core
    and removed
    type: bug
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions