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.
This commit is contained in:
Svyatoslav Scherbina
2023-11-14 18:26:24 +01:00
committed by Space Team
parent cf849835ba
commit 6ce33e80a5
@@ -43,7 +43,12 @@ internal object SafeEnvVars : Iterable<NameAndSafeValue> {
internal class SafeProperties : Iterable<NameAndSafeValue> {
// Properties are mutable. So, need to capture the current values.
private val properties: Map<String, String> = TreeMap<String, String>().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()
}
}