Skip to content

Commit

Permalink
Avoid ThreadLocal memory leak in JoinPointImpl
Browse files Browse the repository at this point in the history
according to https://rules.sonarsource.com/java/tag/leak/RSPEC-5164/.
Now, there no longer is a thread-local stack of AroundClosure instances,
but rather a list of them, which can only grow but never shrink.
Instead, we now have a thread-local (integer) list index, for every
thread being initialised with pointing to the last element. I.e., every
thread can unwind by decrementing the index while proceeding,
independently of other threads.

A positive side effect is that this approach also works for long-lived
threads from thread pools, used by executor services. Hence, test
Bugs199Tests.testAsyncProceedNestedAroundAdviceThreadPool_gh128, which
was previously commented out, has been activated and passes, see #141.

I am not sure if this brings @AspectJ style, non-inlined, nested around
advice execution functionally on par with native ones, but at least for
current scenarios it seems to work.

Fixes #288, #141.

Signed-off-by: Alexander Kriegisch <[email protected]>
  • Loading branch information
kriegaex committed Mar 6, 2024
1 parent 1f1d429 commit eb4d567
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,16 @@ public final String toLongString() {
if (arcs == null) {
arcs = new InheritableThreadLocalAroundClosureStack();
}
if (arc==null) {
this.arcs.get().pop();
Stack<AroundClosure> closureStack = arcs.get();
if (arc == null) {
closureStack.pop();
if (closureStack.empty()) {
// Avoid ThreadLocal memory leak according to https://rules.sonarsource.com/java/tag/leak/RSPEC-5164/.
// Should fix https://github.com/eclipse-aspectj/aspectj/issues/288.
arcs.remove();
}
} else {
this.arcs.get().push(arc);
closureStack.push(arc);
}
}

Expand Down

0 comments on commit eb4d567

Please sign in to comment.