Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8345684

OperatingSystemMXBean.getSystemCpuLoad() throws NPE

XMLWordPrintable

        ADDITIONAL SYSTEM INFORMATION :
        Linux

        A DESCRIPTION OF THE PROBLEM :
        Occassionally, calls to com.sun.management.OperatingSystemMXBean.getSystemCpuLoad() throw an NPE:

        java.lang.NullPointerException: Cannot read the array length because the return value of "jdk.internal.platform.Metrics.getCpuSetCpus()" is null
        at jdk.management/com.sun.management.internal.OperatingSystemImpl.isCpuSetSameAsHostCpuSet(Unknown Source)
        at jdk.management/com.sun.management.internal.OperatingSystemImpl$ContainerCpuTicks.getContainerCpuLoad(Unknown Source)
        at jdk.management/com.sun.management.internal.OperatingSystemImpl.getCpuLoad(Unknown Source)
        at jdk.management/com.sun.management.OperatingSystemMXBean.getSystemCpuLoad(Unknown Source)
                ...

        This appears to be caused by a TOCTOU (time of check, time of use) issue with a null check at https://github.com/openjdk/jdk/blob/16ef6e2a187181f49b7b2d601c660bbd25ab3845/src/jdk.management/unix/classes/com/sun/management/internal/OperatingSystemImpl.java#L277-L278.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        This issue is flaky and there is no clear reproducer. The exception has been observed running the build system Bazel in a container setup in CI (https://github.com/bazelbuild/bazel/issues/24519).

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Method does not throw any exceptions.
        ACTUAL -
        Method throws:

        java.lang.NullPointerException: Cannot read the array length because the return value of "jdk.internal.platform.Metrics.getCpuSetCpus()" is null
        at jdk.management/com.sun.management.internal.OperatingSystemImpl.isCpuSetSameAsHostCpuSet(Unknown Source)
        at jdk.management/com.sun.management.internal.OperatingSystemImpl$ContainerCpuTicks.getContainerCpuLoad(Unknown Source)
        at jdk.management/com.sun.management.internal.OperatingSystemImpl.getCpuLoad(Unknown Source)
        at jdk.management/com.sun.management.OperatingSystemMXBean.getSystemCpuLoad(Unknown Source)
                ...

        This appears to be

        CUSTOMER SUBMITTED WORKAROUND :
        Catching any NPEs thrown by the method.

        FREQUENCY : occasionally


              kevinw Kevin Walls
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: