Class-initialisation deadlock between ConversionService and DefaultMutableConversionService

Author: alvarosanchezCreated Sep 24, 2026Updated Sep 24, 2026
Labelstype: bugpriority: low

Expected Behavior

ConversionService and DefaultMutableConversionService (DMCS) can be initialised from any thread, in any order, without hanging. ConversionService.SHARED is constructed after DMCS's static initialiser has run.

Actual Behaviour

ConversionService.<clinit> runs SHARED = new DefaultMutableConversionService(). ConversionService declares default methods, so under JVMS §5.5 the JVM initialises it before DMCS's own static initialiser. The order depends on which class a thread touches first:

  • ConversionService first, by reading SHARED, ConvertibleValues.EMPTY or building an AnnotationValue. DMCS initialises completely inside ConversionService.<clinit>. Fine.
  • DMCS first, through new DefaultMutableConversionService(), which is what DefaultBeanContext.<init> and MutableConversionService.create() do. DMCS is marked in progress, and ConversionService.<clinit> constructs SHARED before DMCS's static fields are set.

This has two consequences.

  1. Deadlock. One thread starts from DMCS while another starts from ConversionService, in a fresh JVM or class loader. Each holds one class's initialisation lock and waits for the other forever:
    "A"  waiting on the Class initialization monitor for io.micronaut.core.convert.ConversionService
    "B"  at io.micronaut.core.convert.ConversionService.<clinit>(ConversionService.java:35)
         waiting on the Class initialization monitor for io.micronaut.core.convert.DefaultMutableConversionService
  2. Null statics during SHARED's construction on a typical startup, because DefaultBeanContext starts from DMCS. Nothing reads those statics on that path today. Suppose a TypeConverterRegistrar looks up an unregistered pair in register(), for example canConvert(Thread.class, Runnable.class). It reaches resolveHierarchy, and ConversionService fails to initialise with ExceptionInInitializerError, caused by NullPointerException: ... COMMON_TYPE_HIERARCHY is null. It works when ConversionService is initialised first.

A single stock startup does not deadlock, because the parallel definition load starts only after line 348. The deadlock becomes reachable once another thread initialises bean definitions while the first context is being constructed. In our case, a worker ran $Definition.<clinit> → the AnnotationMetadataSupport static block → new AnnotationValue → ConvertibleValues.EMPTY → ConvertibleValuesMap, which reads SHARED.

How it was found. We were profiling micronaut-runner on its benchmark-large sample. A prototype launcher started DefaultBeanDefinitionsProvider.provide() on the common pool, through the builder's BeanDefinitionsProvider, before build(). With -Xlog:class+init,class+load, 9 of 20 launches hung; without logging, 0 of 15 hung. jstack showed main in DefaultBeanContext.<init> waiting on ConversionService, and a worker in ConversionService.<clinit> waiting on DMCS.

Standalone reproduction on micronaut-core 5.2.2, with no Runner involved. Each iteration used a fresh URLClassLoader whose parent was the platform loader, holding only core, slf4j-api and jspecify. Two threads started together at a barrier, and each was joined with a timeout.

Thread A Thread B Iterations that hung
Class.forName(DMCS, true, cl) Class.forName(ConversionService, true, cl) 18 of 50
MutableConversionService.create() reads ConversionService.SHARED 2 of 40
ConversionService first, then DMCS (the change below) Class.forName(ConversionService, true, cl) 0 of 200

Each hang showed the cycle above. The registrar failure reproduces every time.

Steps To Reproduce

  1. In a fresh class loader holding micronaut-core, start two threads together: A calls MutableConversionService.create(), and B reads ConversionService.SHARED.
  2. Join with a timeout, and repeat with a new loader each time. Some iterations never finish.
  3. For the null statics, add a registrar service as described above, and call create() first in a fresh loader.

Possible Solutions

Make core's own DMCS construction sites initialise ConversionService first. ConversionService.<clinit> then always runs first, and DMCS initialises completely inside it. A thread that finds ConversionService in progress on another thread waits without holding the DMCS lock, so the cycle cannot close. There is no runtime cost: SHARED is built at the same point and on the same thread as today.

Sketch
diff
 // MutableConversionService.java
     static MutableConversionService create() {
+        // Initialise ConversionService before DefaultMutableConversionService (class-init deadlock).
+        // Not requireNonNull: SHARED is null if this runs during SHARED's own construction.
+        ConversionService shared = SHARED;
         return new DefaultMutableConversionService();
     }

 // DefaultBeanContext.java (drop the now-unused import)
-        this.conversionService = new DefaultMutableConversionService();
+        this.conversionService = MutableConversionService.create();

Compatibility. The normal path is unchanged, and create() still returns a DMCS. Native image initialises io.micronaut.core.convert at build time (native-image.properties:25), so it is unaffected.

Out of scope.

  • A lazy SHARED. It would remove the dependency completely, but it changes the runtime class of a public constant that has existed since 1.0. It also moves the default-converter registration, which in the sample loads 235 classes in about 27 ms (logged) on main. At most a 6.0 topic.
  • Direct new DefaultMutableConversionService() calls in other modules, such as micronaut-data's DataConversionServiceImpl. These still start from DMCS, but they normally run once both classes are initialised.

Tests (next to DefaultConversionServiceSpec). Build the loader from the code-source locations of ConversionService, slf4j-api and jspecify.

  • Race. Run about 100 iterations of steps 1–2 and assert that both threads finish. Thread A must call create().
  • Registrar. Add a test-only registrar; calling create() first must not throw.

Environment Information

  • macOS arm64, 12 CPUs, OpenJDK 25.0.4.1
  • Standalone probe: micronaut-core 5.2.2, slf4j-api 2.0.13, jspecify 1.0.0

Example Application

https://github.com/alvarosanchez/micronaut-runner/tree/5fb62e39cadfee8f087e3f3b0a0d6be424885b39/test-suite/samples/benchmark-large (the prefetch case). The standalone reproduction needs only micronaut-core.

Version

Reproduced on 5.1.15 (Runner prototype) and 5.2.2 (standalone). The code is unchanged on 5.3.x at ea3a123.

Related

Source: micronaut-projects/micronaut-core