You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/10/16 11:32:52 UTC

(maven) branch fix/packaging-provider-leak created (now b9bdcd4a7d)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a change to branch fix/packaging-provider-leak
in repository https://gitbox.apache.org/repos/asf/maven.git


      at b9bdcd4a7d fix: filter SPI providers by classloader visibility to prevent reactor leaks

This branch includes the following new commits:

     new b9bdcd4a7d fix: filter SPI providers by classloader visibility to prevent reactor leaks

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven) 01/01: fix: filter SPI providers by classloader visibility to prevent reactor leaks

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch fix/packaging-provider-leak
in repository https://gitbox.apache.org/repos/asf/maven.git

commit b9bdcd4a7dacc678d8c970edc04b77c492fc94fb
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Fri Oct 16 13:32:38 2026 +0200

    fix: filter SPI providers by classloader visibility to prevent reactor leaks
    
    The DefaultExtensibleEnumRegistry.lookup() method uses
    Lookup.lookupList() to find dynamically-contributed SPI providers
    (e.g. PackagingProvider from project extensions). This returns ALL
    providers registered with SISU globally, without classloader-based
    filtering.
    
    In a reactor build, Maven 4's model builder processes modules in
    parallel via PhasingExecutor. When one module's extension registers a
    PackagingProvider, that provider becomes visible to sibling modules
    that did not declare the extension — a provider leak. This caused
    MavenITgh13306PackagingProviderTest to be flaky on Windows+JDK27,
    where timing consistently let the extension load before the sibling
    resolved its packaging.
    
    The fix adds classloader visibility filtering: before considering a
    provider, check that its class is loadable from the current thread's
    context classloader (set to the project's realm by selectProjectRealm
    before packaging resolution). Providers from extension realms are only
    visible to projects whose realm imports that extension.
    
    Co-Authored-By: Claude Opus 4.6 (1M context) <no...@anthropic.com>
---
 .../maven/impl/ExtensibleEnumRegistries.java       | 32 ++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
index bfbd307bf5..d3d5a5b888 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/ExtensibleEnumRegistries.java
@@ -118,6 +118,11 @@ public DefaultExtensibleEnumRegistry(
          * thousands; callers that need to look up the same value repeatedly in a tight loop should
          * cache the result themselves.</p>
          *
+         * <p><b>Classloader visibility:</b> providers contributed by project extensions are only
+         * visible when the current thread's context classloader can load the provider's class.
+         * This prevents extension-scoped providers from leaking across reactor modules during
+         * parallel builds.</p>
+         *
          * @param id the enum value id, must not be {@code null}
          * @return the matching value, or {@code Optional.empty()} if not found
          */
@@ -125,7 +130,9 @@ public DefaultExtensibleEnumRegistry(
         public Optional<T> lookup(String id) {
             String key = requireNonNull(id, "id").toLowerCase(Locale.ROOT);
             if (lookup != null && providerType != null) {
+                ClassLoader tccl = Thread.currentThread().getContextClassLoader();
                 Optional<T> result = lookup.lookupList(providerType).stream()
+                        .filter(p -> isVisibleFrom(p, tccl))
                         .flatMap(p -> p.provides().stream())
                         .filter(t -> key.equals(t.id().toLowerCase(Locale.ROOT)))
                         .findFirst();
@@ -135,5 +142,30 @@ public Optional<T> lookup(String id) {
             }
             return Optional.ofNullable(values.get(key));
         }
+
+        /**
+         * Checks whether a provider is visible from the given classloader.
+         * Providers from project extensions are loaded by extension-specific classloaders
+         * (realms) that are only imported into the project realm that declared the extension.
+         */
+        private boolean isVisibleFrom(P provider, ClassLoader classLoader) {
+            if (classLoader == null) {
+                return true;
+            }
+            Class<?> providerClass = provider.getClass();
+            try {
+                return classLoader.loadClass(providerClass.getName()) == providerClass;
+            } catch (ClassNotFoundException e) {
+                // Hidden classes (lambdas, dynamic proxies) cannot be loaded by name;
+                // fall back to classloader hierarchy check.
+                ClassLoader providerCl = providerClass.getClassLoader();
+                for (ClassLoader cl = classLoader; cl != null; cl = cl.getParent()) {
+                    if (cl == providerCl) {
+                        return true;
+                    }
+                }
+                return false;
+            }
+        }
     }
 }