-
Notifications
You must be signed in to change notification settings - Fork 835
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 ConfigUtil#getString ConcurrentModificationException #6841
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab84470
Fix ConcurrentModificationException #6732
neugartf b115c5a
Move to stringPropertyNames in order to iterate on a stable list.
neugartf af48863
Improve stream handling of system property
neugartf 329c05d
Merge branch 'open-telemetry:main' into main
neugartf d736406
Cloning properties in order to safely iterate over them
neugartf 56ea3da
Add ConfigUtil#safeSystemProperties() for safe access to system prope…
jack-berg 08d09ea
remove comment
jack-berg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,15 @@ | |
package io.opentelemetry.api.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
|
@@ -56,4 +64,45 @@ void defaultIfnull() { | |
assertThat(ConfigUtil.defaultIfNull("val1", "val2")).isEqualTo("val1"); | ||
assertThat(ConfigUtil.defaultIfNull(null, "val2")).isEqualTo("val2"); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("ReturnValueIgnored") | ||
void systemPropertiesConcurrentAccess() throws ExecutionException, InterruptedException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test fails on java 8 if you call |
||
int threads = 4; | ||
ExecutorService executor = Executors.newFixedThreadPool(threads); | ||
try { | ||
int cycles = 1000; | ||
CountDownLatch latch = new CountDownLatch(1); | ||
List<Future<?>> futures = new ArrayList<>(); | ||
for (int i = 0; i < threads; i++) { | ||
futures.add( | ||
executor.submit( | ||
() -> { | ||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
|
||
for (int j = 0; j < cycles; j++) { | ||
String property = "prop " + j; | ||
System.setProperty(property, "a"); | ||
System.getProperties().remove(property); | ||
} | ||
})); | ||
} | ||
|
||
latch.countDown(); | ||
for (int i = 0; i < cycles; i++) { | ||
assertThatCode(() -> ConfigUtil.getString("x", "y")).doesNotThrowAnyException(); | ||
} | ||
|
||
for (Future<?> future : futures) { | ||
future.get(); | ||
} | ||
|
||
} finally { | ||
executor.shutdownNow(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloning seems to be the simplest way to accommodate various use cases. Anything other option that returns
Properties
ends up mimicking the logic ofclone()
anyway. Ifclone()
ends up being too expensive (I doubt it), we can cache a copy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with it
ConfigUtil.getString(..)
is already slow (too slow to be used on the hotpath) since it normalizes all the entry keys on each callThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clone
is only needed on jdk8 and android so we could skip it on other jdks if we so wish. Untested code followsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. I'd prefer to keep that in our back pocket in case we need it. As a general rule, I think we avoid runtime-specific code unless we have a strong reason (subjective definition of "strong").