Skip to content

Fix schemagen failing at random in parallel builds - #476

Open
udengaardandersent-ELS wants to merge 1 commit into
mojohaus:masterfrom
udengaardandersent-ELS:fix-schemagen-parallel-realm-imports
Open

udengaardandersent-ELS wants to merge 1 commit into
mojohaus:masterfrom
udengaardandersent-ELS:fix-schemagen-parallel-realm-imports

Conversation

@udengaardandersent-ELS

Copy link
Copy Markdown

Fixes #475.

Why

Maven gives all modules that use this plugin one shared plugin realm. Each schemagen run adds the system tools packages to that realm with ClassRealm.importFrom. That method writes to a plain TreeSet and is not thread-safe.

In a parallel build (-T), two runs can do this at the same moment. The set gets corrupted, and the build fails at random:

java.lang.NullPointerException: Cannot read field "right" because "l" is null
    at java.util.TreeMap.rotateRight
    ...
    at org.codehaus.plexus.classworlds.realm.ClassRealm.importFrom
    at org.codehaus.mojo.jaxb2.schemageneration.AbstractXsdGeneratorMojo.performExecution

What changed

  • The import loop moved into a small method, importSystemToolsPackages.
  • It holds the realm's lock while it imports. All runs share the realm object, so they now take turns.

No parameters, defaults or output change, so no documentation update is needed.

Test

AbstractXsdGeneratorMojoTest.importSystemToolsPackagesFromParallelExecutions calls the method from two threads at once, 20,000 times. Each round uses a fresh realm, set up as Maven does it. It takes about half a second.

  • With the lock: passed 5 times out of 5.
  • Without the lock: failed 5 times out of 5 with the same NPE.

An integration test under src/it would not catch this reliably. The race window is a few microseconds, and a 16-module build with -T 16 passed 40 times out of 40 without the fix.

mvn -P run-its verify passes: 183 unit tests and 45 integration tests.

🤖 Generated with Claude Code

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 mojohaus#475

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Cleanup, refactoring or other internal change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

schemagen fails at random in parallel builds: NullPointerException in ClassRealm.importFrom

1 participant