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 on flag config change java 9 #954

Merged
merged 1 commit into from
Sep 17, 2024

Conversation

toddbaert
Copy link
Member

@toddbaert toddbaert commented Sep 16, 2024

We had a static hashmap that could possibly be modified by multiple threads since computeIfAbsent mutates the map.

This is specifically more visible in Java 9 due to some new additional checks. This is easily solved by making this static member a local variable.

Assert.assertEquals(expectedChangedFlags.keySet().stream().collect(Collectors.toList()),
storageStateDTOS.take().getChangedFlagsKeys());
// flags changed from initial VALID_SIMPLE flag, as a set because we don't care about order
Assert.assertEquals(expectedChangedFlags.keySet().stream().collect(Collectors.toSet()),
Copy link
Member Author

@toddbaert toddbaert Sep 16, 2024

Choose a reason for hiding this comment

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

This assert would inconsistently fail because of ordering inconsistencies.

@@ -98,9 +99,9 @@ public void changedFlags() throws Exception {
Map<String, FeatureFlag> expectedChangedFlags =
FlagParser.parseString(getFlagsFromResource(VALID_LONG),true);
Copy link
Member Author

Choose a reason for hiding this comment

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

This call would intermittently fail in Java 9+ due to concurrent modification exceptions.

private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Map<String, Pattern> PATTERN_MAP = new HashMap<>();
Copy link
Member Author

Choose a reason for hiding this comment

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

This field presents a concurrency issue, since multiple threads can possible parse at the same time, and computeIfAbsent can throw an concurrent-modification-exception in Java 9+

@toddbaert toddbaert changed the title fix: concurrency error on flag configs java 9 fix: ConcurrentModificationException on flag configs java 9 Sep 16, 2024
@toddbaert toddbaert changed the title fix: ConcurrentModificationException on flag configs java 9 fix: ConcurrentModificationException on flag config change java 9 Sep 16, 2024
@toddbaert toddbaert merged commit f74fe5f into main Sep 17, 2024
7 checks passed
@toddbaert toddbaert deleted the fix/concurrency-java-9 branch September 17, 2024 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants