From 6ce33e80a51e8cb66add22a30c28cd02e2f573f5 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Tue, 14 Nov 2023 18:26:24 +0100 Subject: [PATCH] Native: fix ConcurrentModificationException in test infra Synchronously copy system properties before iterating over them, because other thread sometimes tries to modify them in the meantime, which leads to ConcurrentModificationException otherwise. --- .../konan/test/blackbox/support/util/SafeEnvironment.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/util/SafeEnvironment.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/util/SafeEnvironment.kt index 1ef4fdf332f..651b9415e12 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/util/SafeEnvironment.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/util/SafeEnvironment.kt @@ -43,7 +43,12 @@ internal object SafeEnvVars : Iterable { internal class SafeProperties : Iterable { // Properties are mutable. So, need to capture the current values. private val properties: Map = TreeMap().apply { - System.getProperties().forEach { (name, value) -> + // Cloning properties to protect from ConcurrentModificationException + // if another threads modifies them while we iterate through: + val systemProperties = System.getProperties().clone() as Properties + // (clone() is synchronized). + + systemProperties.forEach { (name, value) -> this[name.toString()] = value.toString() } }