Class-initialisation deadlock between ConversionService and DefaultMutableConversionService
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:
ConversionServicefirst, by readingSHARED,ConvertibleValues.EMPTYor building anAnnotationValue. DMCS initialises completely insideConversionService.<clinit>. Fine.- DMCS first, through
new DefaultMutableConversionService(), which is whatDefaultBeanContext.<init>andMutableConversionService.create()do. DMCS is marked in progress, andConversionService.<clinit>constructsSHAREDbefore DMCS's static fields are set.
This has two consequences.
- 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 - Null statics during
SHARED's construction on a typical startup, becauseDefaultBeanContextstarts from DMCS. Nothing reads those statics on that path today. Suppose aTypeConverterRegistrarlooks up an unregistered pair inregister(), for examplecanConvert(Thread.class, Runnable.class). It reachesresolveHierarchy, andConversionServicefails to initialise withExceptionInInitializerError, caused byNullPointerException: ... COMMON_TYPE_HIERARCHY is null. It works whenConversionServiceis 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
- In a fresh class loader holding micronaut-core, start two threads together: A calls
MutableConversionService.create(), and B readsConversionService.SHARED. - Join with a timeout, and repeat with a new loader each time. Some iterations never finish.
- 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.
// 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) onmain. At most a 6.0 topic. - Direct
new DefaultMutableConversionService()calls in other modules, such as micronaut-data'sDataConversionServiceImpl. 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
- Runner's definition prefetch: https://github.com/alvarosanchez/micronaut-runner/issues/155. Its prefetch worker builds
ConversionService.SHAREDfirst. A guard on the thread that builds the context then initialisesConversionService, or waits for the worker to finish it, beforeDefaultBeanContext.<init>runs. So it does not depend on this fix. - Runner tracking issue: https://github.com/alvarosanchez/micronaut-runner/issues/109
Source: micronaut-projects/micronaut-core