You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2026/07/25 09:06:55 UTC

(groovy) branch master updated: Harden parallel concurrency tests against common-pool contention

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ca665dad0 Harden parallel concurrency tests against common-pool contention
0ca665dad0 is described below

commit 0ca665dad0d4070bfa218d9b5577ec423c8b2181
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jul 25 18:01:08 2026 +0900

    Harden parallel concurrency tests against common-pool contention
    
    @Parallel and eachParallel concurrency checks used CyclicBarrier on
    ForkJoinPool.commonPool(). Barrier.await is unmanaged blocking: under
    pool contention a single free worker can hit the barrier and never run
    the second party, yielding TimeoutException (wrapped as
    UndeclaredThrowableException via the SAM Consumer proxy).
    
    - Isolate the barrier rendezvous with ParallelScope.withPool(parties)
    - Cover eachParallel with the same concurrency proof
    - Document that blocking parallel bodies need a dedicated or I/O pool
---
 src/main/java/groovy/transform/Parallel.java       |  5 +++
 .../runtime/ParallelCollectionExtensions.java      |  8 +++++
 .../concurrent/ParallelAnnotationTest.groovy       | 42 ++++++++++++----------
 .../concurrent/ParallelCollectionTest.groovy       | 26 ++++++++++++++
 4 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/src/main/java/groovy/transform/Parallel.java b/src/main/java/groovy/transform/Parallel.java
index 991e778f58..da85aeb538 100644
--- a/src/main/java/groovy/transform/Parallel.java
+++ b/src/main/java/groovy/transform/Parallel.java
@@ -40,6 +40,11 @@ import java.lang.annotation.RetentionPolicy;
  * (e.g. {@code collectParallel}, {@code eachParallel}) directly may
  * offer a better debugging experience, as {@code @Parallel} performs
  * internal variable renaming in its generated code.
+ * <p>
+ * Blocking loop bodies should run inside
+ * {@link groovy.concurrent.ParallelScope#withPool(int, java.util.function.Function)
+ * ParallelScope.withPool(n)} (or an I/O pool); see
+ * {@link org.codehaus.groovy.runtime.ParallelCollectionExtensions}.
  *
  * @since 6.0.0
  * @see org.codehaus.groovy.transform.ParallelASTTransformation
diff --git a/src/main/java/org/codehaus/groovy/runtime/ParallelCollectionExtensions.java b/src/main/java/org/codehaus/groovy/runtime/ParallelCollectionExtensions.java
index 3ffd7d8752..26cbc1f1fb 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ParallelCollectionExtensions.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ParallelCollectionExtensions.java
@@ -47,6 +47,14 @@ import java.util.stream.IntStream;
  * the stream operations run on the current pool's {@link ForkJoinPool},
  * not the common pool.
  * <p>
+ * <b>Blocking work:</b> closures that block (I/O, locks, barriers, timed waits)
+ * hold ForkJoin worker threads. Prefer a dedicated
+ * {@link groovy.concurrent.ParallelScope#withPool(int, java.util.function.Function)
+ * ParallelScope.withPool(n)} with {@code n} large enough for the expected
+ * concurrent blockers, or an I/O-oriented pool ({@link Pool#io()},
+ * {@link Pool#virtual()}) for blocking workloads. Relying on the shared
+ * common pool for blocking bodies can stall under contention.
+ * <p>
  * Inspired by GPars' {@code GParsPoolUtil} category methods.
  *
  * @since 6.0.0
diff --git a/src/test/groovy/groovy/concurrent/ParallelAnnotationTest.groovy b/src/test/groovy/groovy/concurrent/ParallelAnnotationTest.groovy
index 346a7150ab..b4f66e8230 100644
--- a/src/test/groovy/groovy/concurrent/ParallelAnnotationTest.groovy
+++ b/src/test/groovy/groovy/concurrent/ParallelAnnotationTest.groovy
@@ -20,10 +20,7 @@ package groovy.concurrent
 
 import org.junit.jupiter.api.Test
 
-import java.util.concurrent.ForkJoinPool
-
 import static groovy.test.GroovyAssert.assertScript
-import static org.junit.jupiter.api.Assumptions.assumeTrue
 
 final class ParallelAnnotationTest {
 
@@ -124,35 +121,44 @@ final class ParallelAnnotationTest {
 
     @Test
     void testParallelForRunsConcurrently() {
-        // Single-core containers (common in CI) set the fork-join common
-        // pool parallelism to 1. A concurrency assertion is meaningless in
-        // that environment — skip.
-        int parallelism = ForkJoinPool.commonPool().parallelism
-        assumeTrue(parallelism >= 2,
-                "common pool parallelism is $parallelism — skipping concurrency check")
-
         // Force deterministic concurrent execution via a barrier: each
         // iteration must rendezvous with another before proceeding. If the
         // @Parallel loop runs on a single thread, the first await() will
         // time out — eliminating the split-heuristic flakiness of simply
         // counting thread names over a small data set.
+        //
+        // Use ParallelScope.withPool(parties) rather than the common pool:
+        // CyclicBarrier.await is unmanaged blocking, so a contended
+        // ForkJoinPool.commonPool() (other tests / background FJP work)
+        // can leave only one free worker. That worker hits the barrier and
+        // never runs the second party → TimeoutException wrapped as
+        // UndeclaredThrowableException through the SAM Consumer proxy.
+        // A dedicated pool of size == parties isolates the check and works
+        // even on single-core hosts (two worker threads time-share; blocking
+        // on the barrier frees the core for the other party).
         assertScript '''
             import groovy.transform.Parallel
+            import groovy.concurrent.ParallelScope
             import java.util.concurrent.CopyOnWriteArraySet
             import java.util.concurrent.CyclicBarrier
             import java.util.concurrent.TimeUnit
 
             int parties = 2
-            def threadNames = new CopyOnWriteArraySet()
-            def barrier = new CyclicBarrier(parties)
+            ParallelScope.withPool(parties) { scope ->
+                def threadNames = new CopyOnWriteArraySet()
+                def barrier = new CyclicBarrier(parties)
 
-            @Parallel
-            for (item in 1..parties) {
-                threadNames << Thread.currentThread().name
-                barrier.await(5, TimeUnit.SECONDS)
-            }
+                // Real product shape: `@Parallel for (i in 1..n)` on IntRange.
+                // Pool isolation (above) is what makes the barrier rendezvous safe;
+                // RandomAccess on IntRange improves parallelStream split quality.
+                @Parallel
+                for (item in 1..parties) {
+                    threadNames << Thread.currentThread().name
+                    barrier.await(5, TimeUnit.SECONDS)
+                }
 
-            assert threadNames.size() == parties
+                assert threadNames.size() == parties
+            }
         '''
     }
 }
diff --git a/src/test/groovy/groovy/concurrent/ParallelCollectionTest.groovy b/src/test/groovy/groovy/concurrent/ParallelCollectionTest.groovy
index 827a32a34c..fb80e2c243 100644
--- a/src/test/groovy/groovy/concurrent/ParallelCollectionTest.groovy
+++ b/src/test/groovy/groovy/concurrent/ParallelCollectionTest.groovy
@@ -39,6 +39,32 @@ final class ParallelCollectionTest {
         '''
     }
 
+    @Test
+    void testEachParallelRunsConcurrently() {
+        // Same rationale as ParallelAnnotationTest.testParallelForRunsConcurrently:
+        // dedicated pool isolates the barrier rendezvous from commonPool contention.
+        assertScript '''
+            import groovy.concurrent.ParallelScope
+            import java.util.concurrent.CopyOnWriteArraySet
+            import java.util.concurrent.CyclicBarrier
+            import java.util.concurrent.TimeUnit
+
+            int parties = 2
+            ParallelScope.withPool(parties) { scope ->
+                def threadNames = new CopyOnWriteArraySet()
+                def barrier = new CyclicBarrier(parties)
+
+                // IntRange path: same collection shape as `@Parallel for (i in 1..n)`
+                (1..parties).eachParallel { item ->
+                    threadNames << Thread.currentThread().name
+                    barrier.await(5, TimeUnit.SECONDS)
+                }
+
+                assert threadNames.size() == parties
+            }
+        '''
+    }
+
     @Test
     void testCollectParallel() {
         assertScript '''