Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConcurrentModificationException #6732 #6746

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

package io.opentelemetry.api.internal;

import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/**
Expand All @@ -33,10 +37,21 @@ private ConfigUtil() {}
*/
public static String getString(String key, String defaultValue) {
String normalizedKey = normalizePropertyKey(key);
Set<Map.Entry<String, String>> properties =
new HashSet<>(System.getProperties().entrySet())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If system properties are modified I'd guess you could still get a concurrent modification error from the hash set constructor. Perhaps this class should create normalized copies of system properties and env in static initializer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I modified the commit a bit in order to access the immutable set of property names that Properties provides. Let me know what you think! :)

.stream()
.filter(
entry -> entry.getKey() instanceof String && entry.getValue() instanceof String)
.map(
entry ->
new AbstractMap.SimpleEntry<>(
(String) entry.getKey(), (String) entry.getValue()))
.collect(Collectors.<Map.Entry<String, String>>toSet());

String systemProperty =
neugartf marked this conversation as resolved.
Show resolved Hide resolved
System.getProperties().entrySet().stream()
.filter(entry -> normalizedKey.equals(normalizePropertyKey(entry.getKey().toString())))
.map(entry -> entry.getValue().toString())
properties.stream()
.filter(entry -> normalizedKey.equals(normalizePropertyKey(entry.getKey())))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
if (systemProperty != null) {
Expand Down
Loading