[ 
https://issues.apache.org/jira/browse/IVY-639?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542088
 ] 

Xavier Hanin commented on IVY-639:
----------------------------------

To add context pushing and poping on all org.apache.ivy.Ivy methods, I've 
investigated the AOP solution to get something clean. Here is a basic aspect 
which do what we want (after modification of the IvyContext API):
{code}
public aspect IvyContextAspect {
    pointcut ivyClass(): within(Ivy);
    pointcut ivyMethod(Ivy ivy): ivyClass() && execution(* *(..))&& target(ivy);

    before(Ivy ivy): ivyMethod(ivy) {
        pushContext(ivy);
    }
    after(Ivy ivy): ivyMethod(ivy) {
        popContext(ivy);
    }

    private void pushContext(Ivy ivy) {
        if (IvyContext.getContext().peekIvy() != ivy) {
            IvyContext.pushNewContext();
            IvyContext.getContext().setIvy(ivy);
        } else {
            IvyContext.pushContext(IvyContext.getContext());
        }
    }

    private void popContext(Ivy ivy) {
        IvyContext.popContext();
    }
}
{code}
I've used AspectJ to test this aspect, and it works fine... except that it 
requires aspectjrt.jar in the classpath at runtime, the compile time bytecode 
weaving is not independent from aspectj. I've asked on the aspectj mailing list 
if there's an option to avoid this runtime dependency, with no success:
http://www.nabble.com/When-is-aspectjrt.jar-required-at-runtime--tf4758561.html#a13608313

Hence I've abandoned the idea, and handle the problem manually in all methods. 
The aspect is here for record, in case we would later have an AOP solution not 
requiring any runtime dependency.

> Bad IvyContext scope causing memory leak and bad handling of subproject builds
> ------------------------------------------------------------------------------
>
>                 Key: IVY-639
>                 URL: https://issues.apache.org/jira/browse/IVY-639
>             Project: Ivy
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.0.0-alpha-2
>            Reporter: Xavier Hanin
>            Assignee: Xavier Hanin
>            Priority: Critical
>             Fix For: 2.0.0-beta-1
>
>
> Ivy relies on a mechanism of IvyContext to access to some contextual 
> information without passing them throughout all methods parameters.
> The problem is that only one context is attached to the current thread, and 
> it is never cleaned. This leads to two kind of problems:
> - memory leak, which is the root cause of an issue reported in Ant:
> http://issues.apache.org/bugzilla/show_bug.cgi?id=42742
> - bad management of subprojects, in which the parent IvyContext is losed when 
> the subproject starts. Details about this have been reported by Peter Reilly:
> http://www.nabble.com/use-of-IvyContext-tf4472787.html#a12752879
> The solution to this problem is to use a stack of IvyContext rather than a 
> single one, and push the context on each Ivy method call, and pop it at the 
> end.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to