Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Classes that should be initialized at run time got initialized during image building #6284

Closed
romain-grecourt opened this issue Feb 24, 2023 · 7 comments · Fixed by #6426
Assignees
Labels
3.x Issues for 3.x version branch bug Something isn't working MP native-image P2
Milestone

Comments

@romain-grecourt
Copy link
Contributor

romain-grecourt commented Feb 24, 2023

Since #5308 building Helidon MP application with native image fails:

helidon init --batch --version 3.1.1 -Dflavor=mp -Dapp-type=quickstart -Dpackage=com.acme
cd quickstart-mp
mvn package -Pnative-image -DskipTests

Fails with:

Error: Classes that should be initialized at run time got initialized during image building:
 com.acme.GreetResource$Proxy$_$$_WeldClientProxy was unintentionally initialized at build time. To see why com.acme.GreetResource$Proxy$_$$_WeldClientProxy got initialized use --trace-class-initialization=com.acme.GreetResource$Proxy$_$$_WeldClientProxy
com.acme.GreetingProvider$Proxy$_$$_WeldClientProxy was unintentionally initialized at build time. To see why com.acme.GreetingProvider$Proxy$_$$_WeldClientProxy got initialized use --trace-class-initialization=com.acme.GreetingProvider$Proxy$_$$_WeldClientProxy
com.acme.SimpleGreetResource$Proxy$_$$_WeldClientProxy was unintentionally initialized at build time. To see why com.acme.SimpleGreetResource$Proxy$_$$_WeldClientProxy got initialized use --trace-class-initialization=com.acme.SimpleGreetResource$Proxy$_$$_WeldClientProxy
To see how the classes got initialized, use --trace-class-initialization=com.acme.GreetResource$Proxy$_$$_WeldClientProxy,com.acme.GreetingProvider$Proxy$_$$_WeldClientProxy,com.acme.SimpleGreetResource$Proxy$_$$_WeldClientProxy
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
native-image --version
GraalVM 22.3.1 Java 17 CE (Java Version 17.0.6+10-jvmci-22.3-b13)

A work-around is to use --initialize-at-build-time=com.acme (work-around provided in #6260):

<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <buildArgs>--initialize-at-build-time=com.acme</buildArgs>
    </configuration>
</plugin>

We used to have --initialize-at-build-time inside the native-image.properties bundled in helidon-microprofile-cdi.jar ; however that is deprecated:

--initialize-at-build-time without arguments has been deprecated when not using --diagnostics-mode. With GraalVM 22.0.0 --initialize-at-build-time will only work with --diagnostics-mode for debugging purposes.
The reason for deprecation is that --initalize-at-build-time does not compose, i.e., a single library can make assumptions that the whole classpath can be safely initialized at build time; that assumption is often incorrect.

See the relevant documentation here:


Should we require users to pass --initialize-at-build-time=com.acme

Or can we do something that does not require hard-coding the package ?

The class that are failing are initialized by io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature:

com.acme.GreetResource$Proxy$_$$_WeldClientProxy caused initialization of this class with the following trace: 
	at com.acme.GreetResource$Proxy$_$$_WeldClientProxy.<clinit>(Unknown Source)
	at jdk.internal.misc.Unsafe.allocateInstance(Unknown Source)
	at sun.misc.Unsafe.allocateInstance(Unsafe.java:864)
	at org.jboss.weld.bean.proxy.UnsafeProxyInstantiator.newInstance(UnsafeProxyInstantiator.java:48)
	at org.jboss.weld.bean.proxy.ProxyFactory.run(ProxyFactory.java:411)
	at org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:395)
	at org.jboss.weld.bean.proxy.ClientProxyFactory.create(ClientProxyFactory.java:83)
	at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:205)
	at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:195)
	at org.jboss.weld.bean.proxy.ClientProxyProvider.access$100(ClientProxyProvider.java:44)
	at org.jboss.weld.bean.proxy.ClientProxyProvider$CreateClientProxy.apply(ClientProxyProvider.java:52)
	at org.jboss.weld.bean.proxy.ClientProxyProvider$CreateClientProxy.apply(ClientProxyProvider.java:48)
	at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.lambda$new$0(ReentrantMapBackedComputingCache.java:55)
	at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache$$Lambda$1177/0x00000007c15050e8.get(Unknown Source)
	at org.jboss.weld.util.LazyValueHolder$1.computeValue(LazyValueHolder.java:32)
	at org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:46)
	at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.getValue(ReentrantMapBackedComputingCache.java:72)
	at org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.getCastValue(ReentrantMapBackedComputingCache.java:78)
	at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:211)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:175)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$iterateBeans$11(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$1459/0x00000007c15369b8.accept(Unknown Source)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.iterateBeans(WeldFeature.java:191)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature.lambda$duringSetup$4(WeldFeature.java:82)
	at io.helidon.integrations.graal.mp.nativeimage.extension.WeldFeature$$Lambda$455/0x00000007c10db788.apply(Unknown Source)
	at com.oracle.graal.pointsto.meta.AnalysisUniverse.replaceObject(AnalysisUniverse.java:595)
	at com.oracle.svm.hosted.ameta.AnalysisConstantReflectionProvider.replaceObject(AnalysisConstantReflectionProvider.java:177)
	at com.oracle.svm.hosted.ameta.AnalysisConstantReflectionProvider.interceptValue(AnalysisConstantReflectionProvider.java:148)
	at com.oracle.svm.hosted.heap.SVMImageHeapScanner.transformFieldValue(SVMImageHeapScanner.java:127)
	at com.oracle.graal.pointsto.heap.ImageHeapScanner.onFieldValueReachable(ImageHeapScanner.java:351)
	at com.oracle.graal.pointsto.heap.ImageHeapScanner.lambda$createImageHeapObject$3(ImageHeapScanner.java:284)
	at com.oracle.graal.pointsto.heap.ImageHeapScanner$$Lambda$694/0x00000007c1435ab0.call(Unknown Source)
	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at com.oracle.graal.pointsto.util.AnalysisFuture.ensureDone(AnalysisFuture.java:63)
	at com.oracle.graal.pointsto.heap.ImageHeapScanner.lambda$postTask$12(ImageHeapScanner.java:663)
	at com.oracle.graal.pointsto.heap.ImageHeapScanner$$Lambda$523/0x00000007c1412a40.run(Unknown Source)
	at com.oracle.graal.pointsto.util.CompletionExecutor.executeCommand(CompletionExecutor.java:193)
	at com.oracle.graal.pointsto.util.CompletionExecutor.lambda$executeService$0(CompletionExecutor.java:177)
	at com.oracle.graal.pointsto.util.CompletionExecutor$$Lambda$686/0x00000007c1434480.run(Unknown Source)
	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1395)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

Perhaps we can use RuntimeClassInitialization.java) in WeldFeature to ensure that user beans are initialized at build time...


The reason why we did not catch this is that we have defined --initialize-at-build-time=io.helidon in a couple of places and our integration test applications use io.helidon.xxx.

If possible we need to avoid using --initialize-at-build-time=io.helidon and enumerate all packages. (i.e. no wildcard)

We should also consider refactoring our tests to use a package that does not start with io.helidon ; E.g. com.acme.

@romain-grecourt romain-grecourt added bug Something isn't working MP native-image 3.x Issues for 3.x version branch labels Feb 24, 2023
@romain-grecourt romain-grecourt added this to the 3.1.3 milestone Feb 24, 2023
@romain-grecourt
Copy link
Contributor Author

CC @tomas-langer

@romain-grecourt
Copy link
Contributor Author

see also #6260

@romain-grecourt romain-grecourt self-assigned this Mar 2, 2023
@m0mus m0mus added the P2 label Mar 2, 2023
romain-grecourt added a commit to romain-grecourt/helidon that referenced this issue Mar 17, 2023
- Isolate the use of --static with to Linx only.
- Add a native-image.properties file in archetypes for all generated project

Fixes helidon-io#6284
romain-grecourt added a commit that referenced this issue Mar 17, 2023
* Fix native-image build-time initialization

- Isolate the use of --static with to Linx only.
- Add a native-image.properties file in archetypes for all generated project

Fixes #6284

* specify build-args for the build-native-image execution, and ensure it's merged
@mraible
Copy link

mraible commented Jun 17, 2023

I created a new project with the Helidon CLI:

helidon init --flavor MP --groupid com.okta.rest \
  --artifactid helidon --package com.okta.rest

Then, I configured it with MicroProfile JWT and set it up as a resource server. Everything works when running with helidon dev, but it fails to build:

mvn package -Pnative-image

...

Error: Classes that should be initialized at run time got initialized during image building:
 com.sun.beans.TypeResolver was unintentionally initialized at build time. To see why com.sun.beans.TypeResolver got initialized use --trace-class-initialization=com.sun.beans.TypeResolver
To see how the classes got initialized, use --trace-class-initialization=com.sun.beans.TypeResolver
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
--------------------------------------------------------------------------------------------------------------
                   2.6s (6.9% of total time) in 23 GCs | Peak RSS: 5.35GB | CPU load: 5.29
==============================================================================================================
Failed generating 'helidon' after 38.0s.
Error: Image build request failed with exit status 1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  41.098 s
[INFO] Finished at: 2023-06-17T09:47:24-06:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.16:compile (build-native-image) on project helidon: Execution of /Users/mraible/.sdkman/candidates/java/current/bin/native-image @target/tmp/native-image-8953443567303768299.args returned non-zero result -> [Help 1]

I tried using the workaround in this issue, but it does not help.

<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <buildArgs>--initialize-at-build-time=com.okta</buildArgs>
    </configuration>
</plugin>

My pom.xml has Helidon v3.2.1. My JDK version:

openjdk 19.0.2 2023-01-17
OpenJDK Runtime Environment GraalVM CE 22.3.1 (build 19.0.2+7-jvmci-22.3-b12)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.1 (build 19.0.2+7-jvmci-22.3-b12, mixed mode, sharing)

You can see my project at https://github.com/oktadev/auth0-java-rest-api-examples.

@mraible
Copy link

mraible commented Jun 18, 2023

Today I discovered that everything works with Java 17 but not 19. I'm using SDKMAN:

22.3.r17-grl works, 22.3.r19-grl does not
22.3.1.r17-grl works, 22.3.1.r19-grl does not

This is unfortunate since the latest versions of Micronaut, Quarkus, and Spring Boot all work with Java 19.

@mraible
Copy link

mraible commented Jun 20, 2023

I tried it with 22.3.3.r20-grl today, and it seems to have a different error now:

Error: java.util.concurrent.ExecutionException: com.oracle.svm.hosted.substitute.DeletedElementException: 
Unsupported method java.lang.Object.wait0(long) is reachable

@danielkec danielkec self-assigned this Jun 21, 2023
@danielkec
Copy link
Contributor

Hi Matt @mraible, it's interesting that TypeResolver initialization is a problem only for GraalVM 22.x.r19, workaround is --initialize-at-run-time=io.helidon.openapi.ExpandedTypeDescription.

@danielkec
Copy link
Contributor

Deleted element exception is even better, it doesn't happen when you try to diagnose it with --report-unsupported-elements-at-runtime.
Workaround for both issues:

Index: helidon/src/main/resources/META-INF/native-image/com.okta.rest/helidon/native-image.properties
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>ISO-8859-1
===================================================================
diff --git a/helidon/src/main/resources/META-INF/native-image/com.okta.rest/helidon/native-image.properties b/helidon/src/main/resources/META-INF/native-image/com.okta.rest/helidon/native-image.properties
--- a/helidon/src/main/resources/META-INF/native-image/com.okta.rest/helidon/native-image.properties	(revision 1536ded03974fc455762ee4ae3fbf09d4a1d29cb)
+++ b/helidon/src/main/resources/META-INF/native-image/com.okta.rest/helidon/native-image.properties	(date 1687348599487)
@@ -1,1 +1,3 @@
-Args=--initialize-at-build-time=com.okta.rest
+Args=--initialize-at-build-time=com.okta.rest \
+  --initialize-at-run-time=io.helidon.openapi.ExpandedTypeDescription \
+  --report-unsupported-elements-at-runtime
\ No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issues for 3.x version branch bug Something isn't working MP native-image P2
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants