You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by ja...@apache.org on 2026/04/25 15:48:31 UTC
(grails-core) 02/02: fix: serialise GSP compilation under Groovy 6 to dodge ListHashMap race
This is an automated email from the ASF dual-hosted git repository.
jamesfredley pushed a commit to branch grails8-groovy6-canary
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit ddc7ea20c6f783a66a684603e298c85c3a461ce2
Author: James Fredley <ja...@users.noreply.github.com>
AuthorDate: Sat Apr 25 11:48:19 2026 -0400
fix: serialise GSP compilation under Groovy 6 to dodge ListHashMap race
Every CI job that compiled GSPs against Groovy 6.0.0-SNAPSHOT failed
with a Groovy compiler stack like:
General error during instruction selection: Index 3 out of bounds for length 3
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at org.codehaus.groovy.util.ListHashMap.toMap(ListHashMap.java:207)
at org.codehaus.groovy.util.ListHashMap.put(ListHashMap.java:146)
at java.base/java.util.Map.computeIfAbsent(Map.java:1067)
at org.codehaus.groovy.ast.NodeMetaDataHandler.getNodeMetaData(NodeMetaDataHandler.java:65)
at org.codehaus.groovy.ast.AnnotationNode.isTargetAllowed(AnnotationNode.java:168)
at org.codehaus.groovy.classgen.ExtendedVerifier.visitAnnotations(ExtendedVerifier.java:354)
at org.codehaus.groovy.classgen.ExtendedVerifier.visitConstructor(ExtendedVerifier.java:216)
...
at org.grails.web.pages.GroovyPageForkedCompiler.main(GroovyPageForkedCompiler.groovy:106)
`AnnotationNode.isTargetAllowed` was added in Groovy 6 (GROOVY-11838) to
honour the new default annotation targets and uses
`NodeMetaDataHandler.getNodeMetaData` (a `Map.computeIfAbsent` over an
internal `ListHashMap`) on shared `Annotation*` AST nodes. That cache
is touched concurrently by the Grails `GroovyPageCompiler` thread pool
(`Executors.newFixedThreadPool(availableProcessors() * 2)`) once shared
annotations like `@Inject`, `@CompileStatic`, etc. are seen by more
than one GSP compile at the same time, which is exactly the case for
test apps that pull in Spring/Grails compiled output. `ListHashMap` is
not designed for concurrent mutation, so the resize fails with an
`ArrayIndexOutOfBoundsException` and the entire GSP compile aborts.
Replace the unconditional `availableProcessors() * 2` thread pool with
a small `computeGspCompilerParallelism()` helper that:
* defaults to 1 worker on Groovy 6 (eliminates the race),
* defaults to `availableProcessors() * 2` on Groovy 5 and earlier
(preserves prior behaviour),
* honours `-Dgrails.gsp.compiler.parallelism=N` so callers can opt back
into parallel GSP compilation once Groovy 6 fixes the race (or
experimentally tune it down on Groovy 5).
Trade-off: a small wall-clock increase on Groovy 6 GSP compilation in
exchange for deterministic behaviour. The control knob is a single
system property, so this is easy to revert once the upstream Groovy
fix is available.
Verified locally:
./gradlew :grails-gsp-core:compileGroovy --rerun-tasks -> BUILD SUCCESSFUL
Assisted-by: claude-code:claude-opus-4-7
---
.../grails/gsp/compiler/GroovyPageCompiler.groovy | 60 +++++++++++++++++++++-
1 file changed, 58 insertions(+), 2 deletions(-)
diff --git a/grails-gsp/core/src/main/groovy/org/grails/gsp/compiler/GroovyPageCompiler.groovy b/grails-gsp/core/src/main/groovy/org/grails/gsp/compiler/GroovyPageCompiler.groovy
index 9ebc8e1c14..dba23e0d65 100644
--- a/grails-gsp/core/src/main/groovy/org/grails/gsp/compiler/GroovyPageCompiler.groovy
+++ b/grails-gsp/core/src/main/groovy/org/grails/gsp/compiler/GroovyPageCompiler.groovy
@@ -109,11 +109,25 @@ class GroovyPageCompiler {
}
compilerConfig.setTargetDirectory(targetDir)
compilerConfig.setSourceEncoding(encoding)
- ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2)
+ // GSP compilation parallelism is intentionally configurable via the
+ // grails.gsp.compiler.parallelism system property. The default is
+ // 1 (serial) under Groovy 6 because Groovy 6.0.0-SNAPSHOT contains
+ // a thread-safety bug in org.codehaus.groovy.util.ListHashMap that
+ // surfaces during AnnotationNode.isTargetAllowed -> NodeMetaDataHandler
+ // .getNodeMetaData -> Map.computeIfAbsent on shared annotation
+ // metadata (e.g. @Inject, @CompileStatic) when multiple GSPs are
+ // compiled concurrently. The symptom is "General error during
+ // instruction selection: Index N out of bounds for length N" with
+ // an ArrayIndexOutOfBoundsException in ListHashMap.toMap. Falling
+ // back to a single thread eliminates the race at a small cost in
+ // wall-clock time. Override with -Dgrails.gsp.compiler.parallelism=N
+ // (or 0 to use availableProcessors*2) once Groovy 6 fixes this.
+ int parallelism = computeGspCompilerParallelism()
+ ExecutorService threadPool = Executors.newFixedThreadPool(parallelism)
CompletionService completionService = new ExecutorCompletionService(threadPool)
List<Future<Map>> futures = []
try {
- Integer collationLevel = Runtime.getRuntime().availableProcessors() * 2
+ Integer collationLevel = parallelism
if (srcFiles.size() < collationLevel) {
collationLevel = 1
}
@@ -174,6 +188,48 @@ class GroovyPageCompiler {
return compileGSPRegistry
}
+ /**
+ * Resolves the worker-thread count for parallel GSP compilation.
+ *
+ * Honours -Dgrails.gsp.compiler.parallelism=N. A value of 0 (or any
+ * non-positive number) means "use availableProcessors() * 2" (the
+ * historical Grails default). When the property is unset we default
+ * to 1 on Groovy 6 (see the inline comment at the call site for why)
+ * and to availableProcessors() * 2 on Groovy 5 and earlier.
+ */
+ private static int computeGspCompilerParallelism() {
+ int cores = Runtime.getRuntime().availableProcessors()
+ int defaultParallelism = isGroovy6OrLater() ? 1 : cores * 2
+
+ String override = System.getProperty('grails.gsp.compiler.parallelism')
+ if (override == null || override.isEmpty()) {
+ return defaultParallelism
+ }
+ try {
+ int requested = Integer.parseInt(override.trim())
+ if (requested <= 0) {
+ return cores * 2
+ }
+ return requested
+ } catch (NumberFormatException ignore) {
+ return defaultParallelism
+ }
+ }
+
+ private static boolean isGroovy6OrLater() {
+ String version = groovy.lang.GroovySystem.getVersion()
+ if (version == null || version.isEmpty()) {
+ return false
+ }
+ try {
+ int dot = version.indexOf('.')
+ int major = Integer.parseInt(dot >= 0 ? version.substring(0, dot) : version)
+ return major >= 6
+ } catch (NumberFormatException ignore) {
+ return false
+ }
+ }
+
/**
* Compiles an individual GSP file
*