Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Native image crashes with NullPointerException on Kubernetes (probably GraalVM bug with workaround) #34314

@ndwinton

Description

@ndwinton

Under Spring Boot 3.0.2, native-compiled container image running on Kubernetes (in this case a "vanilla" 1.24.7 3-node cluster) fails with a NullPointerException. The same image runs perfectly directly under Docker.

After some research, I believe the underlying cause to be a current GraalVM bug. As per that issue, there is an available workaround (see below) which could be incorporated into the build process pending resolution of the underlying issue.

Steps to reproduce

# Generate simplest web app
curl https://start.spring.io/starter.zip -d dependencies=web,native -d bootVersion=3.0.2.RELEASE -d baseDir=demo -o demo.zip

# Unpack and build
unzip demo.zip
cd demo
./gradlew bootBuildImage

# Tag and upload to a registry
docker tag demo:0.0.1-SNAPSHOT someuser/demo:1.0
docker push someuser/demo:1.0

# Run on Kubernetes
kubectl run demo --image someuser/demo:1.0

# Observe the state
kubectl get pod demo
kubectl logs demo

The state of the pod will be "Error" or "CrashLoopBackoff"

The output of kubectl logs is:

Exception in thread "main" java.lang.IllegalArgumentException: Unable to instantiate factory class [org.springframework.boot.autoconfigure.BackgroundPreinitializer] for factory type [org.springframework.context.ApplicationListener]
        at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$throwing$0(SpringFactoriesLoader.java:650)
        at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$handleMessage$3(SpringFactoriesLoader.java:674)
        at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:231)
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206)
        at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160)
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:459)
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:455)
        at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:274)
        at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:252)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291)
        at com.example.demo.DemoApplication.main(DemoApplication.java:10)
Caused by: java.lang.InternalError: java.lang.reflect.InvocationTargetException
        at com.oracle.svm.core.containers.Metrics.systemMetrics(Metrics.java:67)
        at com.oracle.svm.core.containers.Container.metrics(Container.java:44)
        at com.oracle.svm.core.ContainerInfo.<init>(ContainerInfo.java:34)
        at com.oracle.svm.core.Containers.activeProcessorCount(Containers.java:125)
        at [email protected]/java.lang.Runtime.availableProcessors(Runtime.java:247)
        at org.springframework.boot.autoconfigure.BackgroundPreinitializer.<clinit>(BackgroundPreinitializer.java:68)
        at [email protected]/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at [email protected]/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at org.springframework.core.io.support.SpringFactoriesLoader$FactoryInstantiator.instantiate(SpringFactoriesLoader.java:381)
        at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:228)
        ... 9 more
Caused by: java.lang.reflect.InvocationTargetException
        at [email protected]/java.lang.reflect.Method.invoke(Method.java:568)
        at com.oracle.svm.core.containers.Metrics.systemMetrics(Metrics.java:63)
        ... 18 more
Caused by: java.lang.ExceptionInInitializerError
        at com.oracle.svm.core.containers.CgroupSubsystemFactory.create(CgroupSubsystemFactory.java:78)
        at com.oracle.svm.core.containers.CgroupMetrics.getInstance(CgroupMetrics.java:164)
        ... 20 more
Caused by: java.lang.NullPointerException
        at [email protected]/java.util.Objects.requireNonNull(Objects.java:208)
        at [email protected]/sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:263)
        at [email protected]/java.nio.file.Path.of(Path.java:147)
        at [email protected]/java.nio.file.Paths.get(Paths.java:69)
        at com.oracle.svm.core.containers.CgroupUtil.lambda$readStringValue$0(CgroupUtil.java:57)
        at [email protected]/java.security.AccessController.executePrivileged(AccessController.java:144)
        at [email protected]/java.security.AccessController.doPrivileged(AccessController.java:569)
        at com.oracle.svm.core.containers.CgroupUtil.readStringValue(CgroupUtil.java:59)
        at com.oracle.svm.core.containers.CgroupSubsystemController.getStringValue(CgroupSubsystemController.java:66)
        at com.oracle.svm.core.containers.CgroupSubsystemController.getLongValue(CgroupSubsystemController.java:125)
        at com.oracle.svm.core.containers.cgroupv1.CgroupV1Subsystem.getLongValue(CgroupV1Subsystem.java:269)
        at com.oracle.svm.core.containers.cgroupv1.CgroupV1Subsystem.getHierarchical(CgroupV1Subsystem.java:215)
        at com.oracle.svm.core.containers.cgroupv1.CgroupV1Subsystem.setSubSystemControllerPath(CgroupV1Subsystem.java:203)
        at com.oracle.svm.core.containers.cgroupv1.CgroupV1Subsystem.initSubSystem(CgroupV1Subsystem.java:111)
        at com.oracle.svm.core.containers.cgroupv1.CgroupV1Subsystem.<clinit>(CgroupV1Subsystem.java:47)
        ... 22 more

Workaround

A workaround is to incorporate the following into the build.gradle:

tasks.named("bootBuildImage") {
    // Workaround for NPE - see https://github.com/oracle/graal/issues/4757
    environment["BP_NATIVE_IMAGE_BUILD_ARGUMENTS"] = "-H:-UseContainerSupport"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions