From 679ab6fbcbdbfa9f5ab251b364f730ba65f94702 Mon Sep 17 00:00:00 2001 From: Thue Andersen <62244542+udengaardandersent-ELS@users.noreply.github.com> Date: Fri, 27 Nov 2026 08:28:21 +0100 Subject: [PATCH] Fix schemagen failing at random in parallel builds Maven gives all modules that use the plugin one shared plugin realm. Each schemagen execution adds the system tools packages to that realm with ClassRealm.importFrom, which writes to an unsynchronised TreeSet. Two executions at the same moment can corrupt the set and fail with a NullPointerException in TreeMap.rotateRight. Make the imports while holding the realm's lock. Fixes #475 Co-Authored-By: Claude Opus 5.5 --- .../AbstractXsdGeneratorMojo.java | 21 +++++++-- .../AbstractXsdGeneratorMojoTest.java | 43 +++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojo.java b/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojo.java index 26a94dd5..e7525a40 100644 --- a/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojo.java +++ b/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojo.java @@ -367,6 +367,22 @@ protected boolean isReGenerationRequired() throws MojoExecutionException { return stale; } + /** + * Imports the {@link #SYSTEM_TOOLS_CLASSLOADER_PACKAGES} into the given plugin realm. + * Maven gives all modules of a build that use this plugin the same plugin realm, so schemagen + * executions in a parallel build call this at the same time. {@link ClassRealm#importFrom(ClassLoader, String)} + * is not thread-safe, so the imports are made while holding the realm's lock. + * + * @param realm the plugin realm that loaded this mojo. + */ + static void importSystemToolsPackages(final ClassRealm realm) { + synchronized (realm) { + for (String current : SYSTEM_TOOLS_CLASSLOADER_PACKAGES) { + realm.importFrom((ClassLoader) null, current); + } + } + } + /** * {@inheritDoc} */ @@ -383,10 +399,7 @@ protected boolean performExecution() throws MojoExecutionException, MojoFailureE // is delegated to that ClassLoader, to comply with SchemaGen's internal reflective loading // of classes. Otherwise we will have ClassCastExceptions instead of proper execution. // - final ClassRealm localRealm = (ClassRealm) getClass().getClassLoader(); - for (String current : SYSTEM_TOOLS_CLASSLOADER_PACKAGES) { - localRealm.importFrom((ClassLoader) null, current); - } + importSystemToolsPackages((ClassRealm) getClass().getClassLoader()); // Configure the ThreadContextClassLoaderBuilder, to enable synthesizing a correct ClassPath for the tool. final ThreadContextClassLoaderBuilder classLoaderBuilder = ThreadContextClassLoaderBuilder.createFor( diff --git a/src/test/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojoTest.java b/src/test/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojoTest.java index f5626482..c9cbc1ef 100644 --- a/src/test/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojoTest.java +++ b/src/test/java/org/codehaus/mojo/jaxb2/schemageneration/AbstractXsdGeneratorMojoTest.java @@ -22,11 +22,18 @@ import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import org.apache.maven.model.Resource; import org.codehaus.mojo.jaxb2.BufferingLog; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -155,6 +162,42 @@ void validateReGenerationNotRequiredWhenOutputDirHasFilesAndStaleFileFresh(@Temp assertTrue(mojo.isReGenerationRequired()); } + @Test + void importSystemToolsPackagesFromParallelExecutions() throws Exception { + // Maven shares one plugin realm between all modules that use the plugin, so parallel schemagen + // executions import into the same realm at the same moment. Without a lock, this corrupts the + // realm's TreeSet of imports about once per few thousand rounds. + final int threads = 2; + final int rounds = 20_000; + final ClassWorld world = new ClassWorld(); + final ExecutorService pool = Executors.newFixedThreadPool(threads); + try { + for (int round = 0; round < rounds; round++) { + final ClassRealm pluginRealm = world.newRealm("plugin-" + round); + // With a build extension, Maven imports the project realm into the plugin realm first. + pluginRealm.importFrom(world.newRealm("project-" + round), ""); + + final CyclicBarrier start = new CyclicBarrier(threads); + final List> executions = new ArrayList<>(); + for (int i = 0; i < threads; i++) { + executions.add(pool.submit(() -> { + start.await(); + AbstractXsdGeneratorMojo.importSystemToolsPackages(pluginRealm); + return null; + })); + } + for (Future execution : executions) { + execution.get(); + } + + world.disposeRealm("plugin-" + round); + world.disposeRealm("project-" + round); + } + } finally { + pool.shutdownNow(); + } + } + private static URL createFileUrl(final File file) throws MalformedURLException { return file.toPath().toUri().toURL(); }