Hi, I have encountered an issue similar to MNG-4785 [1] while using plugins from Eclipse's Tycho project, where code running in a worker thread of Eclipse's job framework uses LegacySupport to get the current Maven session. It is used for artifact resolution [2], just like in the ticket. This works until LegacySupport#setSession is called from the main thread, at which point the default implementation [3]:
- sets the value of the current AtomicReference to null; - if the new session is not null, it also replaces the AtomicReference with another instance, leaving child threads with the null-ed out earlier version. The thread pool maintained by the job manager does not retire worker threads immediately after they are finished with the current job, so it is purely based on workload/chance when a new thread is spawned, and when an existing thread takes on the next submitted job. The new threads have no issues, as InheritableThreadLocal can initialize the variable with a "good" AtomicReference from the parent thread; this is also mentioned in the comments section of the JIRA ticket. The latter however fail with an NPE as a result of the above. Given that DefaultLegacySupport is already marked Singleton, could the static ThreadLocal be replaced with a final AtomicReference instance field? This would be accessible across all threads, has less chance of leaking and can still be updated in a thread-safe manner. Let me know if I should file a JIRA issue for this. Thanks, András [1] https://issues.apache.org/jira/browse/MNG-4785 [2] https://github.com/eclipse/tycho/blob/995b6a252686492e49f8c250017302b574a92ba8/tycho-core/src/main/java/org/eclipse/tycho/osgi/configuration/MavenDependenciesResolverConfigurer.java#L60 [3] https://github.com/apache/maven/blob/c3cf29438e3d65d6ee5c5726f8611af99d9a649a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultLegacySupport.java#L48-L64
