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

Race condition in java.util.ServiceLoader

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_8

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 8.1
      Java 11.0.2+9

      A DESCRIPTION OF THE PROBLEM :
      When java.util.ServiceLoader concurrently acts as an iterator exceptions are thrown due to a race condition in the ServiceLoader implementation.

      Effects of this behavior have also been reported elsewhere, e.g.:
      https://github.com/eclipse/paho.mqtt.java/issues/572
      https://github.com/talsma-ict/context-propagation/issues/8


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the following test program below until exceptions occur.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exceptions.
      ACTUAL -

      Exception in thread "Thread-39" java.util.NoSuchElementException
      at java.base/java.lang.CompoundEnumeration.nextElement(ClassLoader.java:3036)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1205)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1220)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264)
      at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1299)
      at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1384)
      at de.uplanet.lucy.server.mqtt.JdkBug.bugsGoHere(JdkBug.java:39)
      at de.uplanet.lucy.server.mqtt.JdkBug.lambda$0(JdkBug.java:28)
      at java.base/java.lang.Thread.run(Thread.java:834)
      Exception in thread "Thread-37" java.util.NoSuchElementException
      at java.base/jdk.internal.loader.URLClassPath$1.nextElement(URLClassPath.java:356)
      at java.base/jdk.internal.loader.URLClassPath$1.nextElement(URLClassPath.java:331)
      at java.base/jdk.internal.loader.BuiltinClassLoader$1.hasNext(BuiltinClassLoader.java:358)
      at java.base/jdk.internal.loader.BuiltinClassLoader$1.hasMoreElements(BuiltinClassLoader.java:365)
      at java.base/java.lang.CompoundEnumeration.next(ClassLoader.java:3022)
      at java.base/java.lang.CompoundEnumeration.hasMoreElements(ClassLoader.java:3031)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1202)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1220)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264)
      at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1299)
      at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1384)
      at de.uplanet.lucy.server.mqtt.JdkBug.bugsGoHere(JdkBug.java:39)
      at de.uplanet.lucy.server.mqtt.JdkBug.lambda$0(JdkBug.java:28)
      at java.base/java.lang.Thread.run(Thread.java:834)


      ---------- BEGIN SOURCE ----------
      import java.security.Provider;
      import java.util.ServiceLoader;

      public final class JdkBug
      {
        private static final ServiceLoader<Provider> SERVICE_LOADER =
          ServiceLoader.load(Provider.class, Provider.class.getClassLoader());


        public static void main(String[] args) throws Exception
        {
          for (int i = 0; i < 50; i++)
            new Thread(() -> bugsGoHere()).start();

          Thread.sleep(10000L);
        }


        public static void bugsGoHere()
        {
          int i = 0;

          for (java.security.Provider prov : SERVICE_LOADER) // throws the NoSuchElementException
          {
            if (prov.isConfigured())
              i++;
          }
        }
      }

      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal (Inactive)
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: