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.
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.
- About phantom references: https://www.baeldung.com/java-phantom-reference
- Netty resource leak detector: https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/ResourceLeakDetector.java
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`.
Provide an abstract class, Disposable.LeakAware:
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.
- 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.