Uploaded image for project: 'James Server'
  1. James Server
  2. JAMES-3724

Lifecycle API should embed a leak detector



    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.7.0
    • 3.8.0
    • James Core
    • None


      Why ?

      JAMES-3720 showed how easy to miss calling Lifecycle::dispose on email objects, which causes potentially temporary file leaks. If core developer / core components are subject to leaks while core contributors are aware of the importance of life-cycle management of mail objects, I cannot but be worry of users writing extensions and messing up.


      Thus I would like a defense mechanism that:

      • Log errors if an email is not disposed, and ideally contains a stacktrace to where that email was created to ease debug.
      • GC should eventually reclaim those temporary files, this is better than leaking them.

      Solution overview

      I have in mind something much similar to Netty 4 pooled buffer leak detection:

      • Rely on phantom references to track GCed items
      • Fill a stack trace upon object creation
      • Upon GC verify that resources were released, if not do it and warn the user about it.


      API overview

      Use system properties to configure leak detection:

      • `-Djames.ligecycle.leak.detection.mode=none` would behave as today.
      • `-Djames.ligecycle.leak.detection.mode=simple` would output a simple ERROR log if a leak is encountered and would free the resources.
      • `-Djames.ligecycle.leak.detection.mode=advanced` would output an extented ERROR log implying the place of allocation of the underlying object and would free resources.
      • `-Djames.ligecycle.leak.detection.mode=testing` would output an extented ERROR log implying the place of allocation of the underlying object and rethrow an error, unsuring test fails and that action is being taken by the development team.

      `jvm.properties` files and documentation would document this setting.

      Also we IMO should default to `simple`.

      Implementation details

      Provide an abstract class, Disposable.LeakAware:

      public interface Disposable {
           * Dispose the object
          void dispose();
          static abstract class LeakAware implements Disposable {
              private static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<>();
              private Atomic<Boolean> isDisposed:
              protected LeakAware() {
                  isDisposed = new AtomicBoolean(false);
              public void track() {
                   // Create a phantom reference, of course if detection mode is none
                   new LeakAwareFinalizer(this, REFERENCE_QUEUE)
                   // POLL the reference queue here to check for leaks!
                   // of course don't do it if the leak detection mode is none
                   while ((referenceFromQueue = REFERENCE_QUEUE.poll()) != null) {
          public class LeakAwareFinalizer extends PhantomReference<LeakAware> {
              public LeakAwareFinalizer(LeakAware referent, ReferenceQueue<? super LeakAware> q) {
                  super(referent, q);
              public void detectLeak() {
                  if (!referent.isDisposed()) {
                      // add starttrace of creation if needed
                      LOGGER.error("Leak detected!!!");
                      // clean if needed

      Then sensitive classes, likely only `MimeMessageInputStreamSource` would extend it, and thus automatically gain leak detection.

      We can turn `MimeMessageSource` into an interface with a default size method if needed.

      Acceptance criteria

      • Run tests in testing leak detection mode. This can be done by passing system properties to the surefire plugin.
      • Unit tests including logging checks for each mode.
      • See this for logging assertions: PeriodicalHealthCheckTest
      • Call System.gc() to force a GC and get the leak detection running
      • Manage dynamically system variables in unit tests to test each mode.
      • Verify release was called (via atomic boolean in a custom Disposable.LeakAware object?)
      • Document this feature.


        Issue Links



              Unassigned Unassigned
              btellier Benoit Tellier
              0 Vote for this issue
              1 Start watching this issue



                Time Tracking

                  Original Estimate - Not Specified
                  Not Specified
                  Remaining Estimate - 0h
                  Time Spent - 4h 40m
                  4h 40m