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

CopyOnWriteArrayList subList needs more synchronization

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
      None

      Description

      Looking at CopyOnWriteArrayList.COWSubList, I see some methods that should have more synchronization:

      remove(Object o)

      void forEach(Consumer<? super E> action)
       array and indices must be read under lock, although elements themselves can be processed outside of lock!

      Spliterator<E> spliterator()
       Similarly.

      Method add(Object) is inherited from AbstractList, but that implementation has an obvious race:
          public boolean add(E e) {
              add(size(), e);

        Activity

        Hide
        martin Martin Buchholz added a comment -
        Here's a repro in a jsr166 CVS workspace, showing off the awesome DetectRaces test.

         $ cvs update -D 2016-10-01 src/main/java/util/concurrent/CopyOnWriteArrayList.java && ant -Djsr166.tckTestClass=CopyOnWriteArrayListTest -Djsr166.runsPerTest=100 -Djsr166.methodFilter=DetectRaces tck; cvs update -A src/main/java/util/concurrent/CopyOnWriteArrayList.java
        P src/main/java/util/concurrent/CopyOnWriteArrayList.java
        Buildfile: /usr/local/google/home/martinrb/jsr166/sublist/build.xml

        configure-compiler:

        compile:
            [javac] Compiling 2 source files to /usr/local/google/home/martinrb/jsr166/sublist/build/classes/java.base

        jar:
              [jar] Building jar: /usr/local/google/home/martinrb/jsr166/sublist/build/jsr166.jar

        tck:
             [java] EThere was 1 error:
             [java] 1) testDetectRaces(Collection8Test)java.util.concurrent.ExecutionException: java.util.ConcurrentModificationException
             [java] at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:93)
             [java] at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:176)
             [java] at Collection8Test.testDetectRaces(Collection8Test.java:598)
             [java] at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
             [java] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
             [java] at JSR166TestCase.runTest(JSR166TestCase.java:303)
             [java] at JSR166TestCase.runBare(JSR166TestCase.java:294)
             [java] at JSR166TestCase.main(JSR166TestCase.java:367)
             [java] at CopyOnWriteArrayListTest.main(CopyOnWriteArrayListTest.java:25)
             [java] Caused by: java.util.ConcurrentModificationException
             [java] at java.base/java.util.concurrent.CopyOnWriteArrayList$COWSubList.spliterator(CopyOnWriteArrayList.java:1455)
             [java] at java.base/java.util.Collection.stream(Collection.java:581)
             [java] at Collection8Test.lambda$testDetectRaces$52(Collection8Test.java:552)
             [java] at Collection8Test.lambda$testDetectRaces$62(Collection8Test.java:585)
             [java] at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:485)
             [java] at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:235)
             [java] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1132)
             [java] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:606)
             [java] at java.base/java.lang.Thread.run(Thread.java:844)
             [java] Time: 1.398
             [java]
             [java] FAILURES!!!
        Show
        martin Martin Buchholz added a comment - Here's a repro in a jsr166 CVS workspace, showing off the awesome DetectRaces test.  $ cvs update -D 2016-10-01 src/main/java/util/concurrent/CopyOnWriteArrayList.java && ant -Djsr166.tckTestClass=CopyOnWriteArrayListTest -Djsr166.runsPerTest=100 -Djsr166.methodFilter=DetectRaces tck; cvs update -A src/main/java/util/concurrent/CopyOnWriteArrayList.java P src/main/java/util/concurrent/CopyOnWriteArrayList.java Buildfile: /usr/local/google/home/martinrb/jsr166/sublist/build.xml configure-compiler: compile:     [javac] Compiling 2 source files to /usr/local/google/home/martinrb/jsr166/sublist/build/classes/java.base jar:       [jar] Building jar: /usr/local/google/home/martinrb/jsr166/sublist/build/jsr166.jar tck:      [java] EThere was 1 error:      [java] 1) testDetectRaces(Collection8Test)java.util.concurrent.ExecutionException: java.util.ConcurrentModificationException      [java] at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:93)      [java] at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:176)      [java] at Collection8Test.testDetectRaces(Collection8Test.java:598)      [java] at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)      [java] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)      [java] at JSR166TestCase.runTest(JSR166TestCase.java:303)      [java] at JSR166TestCase.runBare(JSR166TestCase.java:294)      [java] at JSR166TestCase.main(JSR166TestCase.java:367)      [java] at CopyOnWriteArrayListTest.main(CopyOnWriteArrayListTest.java:25)      [java] Caused by: java.util.ConcurrentModificationException      [java] at java.base/java.util.concurrent.CopyOnWriteArrayList$COWSubList.spliterator(CopyOnWriteArrayList.java:1455)      [java] at java.base/java.util.Collection.stream(Collection.java:581)      [java] at Collection8Test.lambda$testDetectRaces$52(Collection8Test.java:552)      [java] at Collection8Test.lambda$testDetectRaces$62(Collection8Test.java:585)      [java] at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:485)      [java] at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:235)      [java] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1132)      [java] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:606)      [java] at java.base/java.lang.Thread.run(Thread.java:844)      [java] Time: 1.398      [java]      [java] FAILURES!!!
        Hide
        hgupdate HG Updates added a comment -
        URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/a15610e000ba
        User: martin
        Date: 2016-11-29 08:30:02 +0000
        Show
        hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/a15610e000ba User: martin Date: 2016-11-29 08:30:02 +0000
        Hide
        hgupdate HG Updates added a comment -
        URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/a15610e000ba
        User: lana
        Date: 2016-12-07 19:33:44 +0000
        Show
        hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/a15610e000ba User: lana Date: 2016-12-07 19:33:44 +0000

          People

          • Assignee:
            martin Martin Buchholz
            Reporter:
            martin Martin Buchholz
          • Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: