You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "netliomax25-code (via GitHub)" <gi...@apache.org> on 2026/07/12 16:01:09 UTC
[PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
netliomax25-code opened a new pull request, #2698:
URL: https://github.com/apache/groovy/pull/2698
Repro: on one thread call metaClass.mixin(someCategory) while another thread dispatches a method on the same class, so findMixinMethod iterates mixinClasses while addMixinClass adds to it.
Cause: mixinClasses is a plain LinkedHashSet with no locking, while every other backing collection in ExpandoMetaClass is a ConcurrentHashMap held alongside the read/write lock. A structural add during iteration throws ConcurrentModificationException in the dispatching thread.
Fix: keep the mixins in a CopyOnWriteArraySet, which is safe for concurrent read-while-modify and preserves insertion order plus the boolean add() dedup that GROOVY-11775 depends on.
Verified with a new test that throws ConcurrentModificationException on the old set and passes with the change; the existing mixin and ExpandoMetaClass suites stay green.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert closed pull request #2698: fix concurrent modification of ExpandoMetaClass mixinClasses
URL: https://github.com/apache/groovy/pull/2698
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert commented on PR #2698:
URL: https://github.com/apache/groovy/pull/2698#issuecomment-4962821957
I created the following issue:
https://issues.apache.org/jira/browse/GROOVY-12163
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2698:
URL: https://github.com/apache/groovy/pull/2698#discussion_r3574174786
##########
src/test/groovy/groovy/lang/ExpandoMetaClassMixinConcurrencyTest.groovy:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.lang
+
+import org.codehaus.groovy.reflection.MixinInMetaClass
+import org.junit.jupiter.api.Test
+
+import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.CyclicBarrier
+import java.util.concurrent.atomic.AtomicBoolean
+
+import static org.junit.jupiter.api.Assertions.assertTrue
+
+final class ExpandoMetaClassMixinConcurrencyTest {
+
+ static class Target {}
+
+ // Adding a mixin (addMixinClass) mutates the backing mixin set while method
+ // dispatch iterates it (findMixinMethod). The set must tolerate that; a plain
+ // LinkedHashSet threw ConcurrentModificationException from the dispatching thread.
+ @Test
+ void mixinAddIsSafeAgainstConcurrentDispatch() {
+ def loader = new GroovyClassLoader()
+ // distinct category classes so every mixin is a fresh structural add
+ def categories = (0..<160).collect { i ->
+ loader.parseClass("class Cat_${i} { def catMethod_${i}() { 'x' } }", "Cat_${i}.groovy")
+ }
+
+ def emc = new ExpandoMetaClass(Target, false, true)
+ emc.initialize()
+
+ // pre-grow the set so each iteration spans a wide window
+ (0..<40).each { MixinInMetaClass.mixinClassesToMetaClass(emc, [categories[it]]) }
+
+ def errors = new CopyOnWriteArrayList<Throwable>()
+ def stop = new AtomicBoolean(false)
+ def emptyArgs = new Class[0]
+ def barrier = new CyclicBarrier(5)
+
+ def readers = (1..4).collect {
+ Thread.start {
+ try {
+ barrier.await()
Review Comment:
`barrier.await()` has no timeout, so if one party fails to reach the barrier the test can hang indefinitely in CI. Use a bounded await to fail fast instead of blocking forever.
##########
src/test/groovy/groovy/lang/ExpandoMetaClassMixinConcurrencyTest.groovy:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.lang
+
+import org.codehaus.groovy.reflection.MixinInMetaClass
+import org.junit.jupiter.api.Test
+
+import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.CyclicBarrier
+import java.util.concurrent.atomic.AtomicBoolean
+
+import static org.junit.jupiter.api.Assertions.assertTrue
+
+final class ExpandoMetaClassMixinConcurrencyTest {
+
+ static class Target {}
+
+ // Adding a mixin (addMixinClass) mutates the backing mixin set while method
+ // dispatch iterates it (findMixinMethod). The set must tolerate that; a plain
+ // LinkedHashSet threw ConcurrentModificationException from the dispatching thread.
+ @Test
+ void mixinAddIsSafeAgainstConcurrentDispatch() {
+ def loader = new GroovyClassLoader()
+ // distinct category classes so every mixin is a fresh structural add
+ def categories = (0..<160).collect { i ->
+ loader.parseClass("class Cat_${i} { def catMethod_${i}() { 'x' } }", "Cat_${i}.groovy")
+ }
+
+ def emc = new ExpandoMetaClass(Target, false, true)
+ emc.initialize()
+
+ // pre-grow the set so each iteration spans a wide window
+ (0..<40).each { MixinInMetaClass.mixinClassesToMetaClass(emc, [categories[it]]) }
+
+ def errors = new CopyOnWriteArrayList<Throwable>()
+ def stop = new AtomicBoolean(false)
+ def emptyArgs = new Class[0]
+ def barrier = new CyclicBarrier(5)
+
+ def readers = (1..4).collect {
+ Thread.start {
+ try {
+ barrier.await()
+ while (!stop.get()) {
+ emc.findMixinMethod('noSuchMixinMethod', emptyArgs)
+ }
+ } catch (Throwable t) {
+ errors << t
+ } finally {
+ stop.set(true)
+ }
+ }
+ }
+ def writer = Thread.start {
+ try {
+ barrier.await()
Review Comment:
`barrier.await()` has no timeout here; if any reader thread fails before reaching the barrier, this writer thread can block forever. Prefer a bounded await to prevent test suite hangs.
##########
src/test/groovy/groovy/lang/ExpandoMetaClassMixinConcurrencyTest.groovy:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.lang
+
+import org.codehaus.groovy.reflection.MixinInMetaClass
+import org.junit.jupiter.api.Test
+
+import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.CyclicBarrier
+import java.util.concurrent.atomic.AtomicBoolean
+
+import static org.junit.jupiter.api.Assertions.assertTrue
+
+final class ExpandoMetaClassMixinConcurrencyTest {
+
+ static class Target {}
+
+ // Adding a mixin (addMixinClass) mutates the backing mixin set while method
+ // dispatch iterates it (findMixinMethod). The set must tolerate that; a plain
+ // LinkedHashSet threw ConcurrentModificationException from the dispatching thread.
+ @Test
+ void mixinAddIsSafeAgainstConcurrentDispatch() {
+ def loader = new GroovyClassLoader()
+ // distinct category classes so every mixin is a fresh structural add
+ def categories = (0..<160).collect { i ->
+ loader.parseClass("class Cat_${i} { def catMethod_${i}() { 'x' } }", "Cat_${i}.groovy")
+ }
+
+ def emc = new ExpandoMetaClass(Target, false, true)
+ emc.initialize()
+
+ // pre-grow the set so each iteration spans a wide window
+ (0..<40).each { MixinInMetaClass.mixinClassesToMetaClass(emc, [categories[it]]) }
+
+ def errors = new CopyOnWriteArrayList<Throwable>()
+ def stop = new AtomicBoolean(false)
+ def emptyArgs = new Class[0]
+ def barrier = new CyclicBarrier(5)
+
+ def readers = (1..4).collect {
+ Thread.start {
+ try {
+ barrier.await()
+ while (!stop.get()) {
+ emc.findMixinMethod('noSuchMixinMethod', emptyArgs)
+ }
+ } catch (Throwable t) {
+ errors << t
+ } finally {
+ stop.set(true)
+ }
+ }
+ }
+ def writer = Thread.start {
+ try {
+ barrier.await()
+ for (int i = 40; i < categories.size() && !stop.get(); i++) {
+ MixinInMetaClass.mixinClassesToMetaClass(emc, [categories[i]])
+ }
+ } catch (Throwable t) {
+ errors << t
+ } finally {
+ stop.set(true)
+ }
+ }
+
+ writer.join()
+ stop.set(true)
+ readers*.join()
Review Comment:
The thread joins are unbounded (`join()` / `readers*.join()`), so if a thread gets stuck (e.g., at the barrier) the test can hang the entire build. Use join timeouts and interrupt any remaining live threads.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
Posted by "testlens-app[bot] (via GitHub)" <gi...@apache.org>.
testlens-app[bot] commented on PR #2698:
URL: https://github.com/apache/groovy/pull/2698#issuecomment-4952022385
## ✅ All tests passed ✅
🏷️ Commit: df5ef75a2cc194c2ad84448c03bd69427f47ac75
▶️ Tests: 82577 executed
⚪️ Checks: 23/23 completed
---
_Learn more about TestLens at [testlens.app](https://testlens.app)._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert commented on PR #2698:
URL: https://github.com/apache/groovy/pull/2698#issuecomment-4963079423
Superseded by #2708
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix concurrent modification of ExpandoMetaClass mixinClasses [groovy]
Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert commented on PR #2698:
URL: https://github.com/apache/groovy/pull/2698#issuecomment-4963938772
The superseded PR is merged. Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org