-
Notifications
You must be signed in to change notification settings - Fork 111
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
Fixes a bug that caused concurrent field access of cloned, read-only structs with more than 5 members to be non-deterministic. #630
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,14 @@ private void build_field_map() | |
_field_map.put(v._fieldName, ii); // this causes the map to have the largest index value stored | ||
} | ||
} | ||
|
||
@Override | ||
public void makeReadOnly() { | ||
// Eagerly initialize the fields map to prevent potential data races https://github.com/amazon-ion/ion-java/issues/629 | ||
fieldMapIsActive(_child_count); | ||
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 name reads like a Boolean accessor. |
||
super.makeReadOnly(); | ||
} | ||
|
||
private void add_field(String fieldName, int newFieldIdx) | ||
{ | ||
Integer idx = _field_map.get(fieldName); | ||
|
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 |
---|---|---|
|
@@ -20,6 +20,10 @@ | |
import com.amazon.ion.system.IonSystemBuilder; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static com.amazon.ion.impl._Private_Utils.newSymbolToken; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
@@ -28,6 +32,7 @@ | |
import static org.hamcrest.Matchers.sameInstance; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class CloneTest | ||
|
@@ -261,6 +266,30 @@ public void modifyAfterCloneDoesNotChangeOriginal() { | |
assertNotEquals(original, clone); | ||
} | ||
|
||
@Test | ||
public void readOnlyIonStructMultithreadedTest() { | ||
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. suggestion—It would be nice to have a more descriptive name. |
||
// See: https://github.com/amazon-ion/ion-java/issues/629 | ||
String ionStr = "{a:1,b:2,c:3,d:4,e:5,f:6}"; | ||
|
||
IonStruct ionValue = (IonStruct)system().singleValue(ionStr); | ||
ionValue.makeReadOnly(); | ||
|
||
for (int i=0; i<100; i++) { | ||
IonStruct clone = ionValue.clone(); | ||
clone.makeReadOnly(); | ||
|
||
List<CompletableFuture<Void>> waiting = new ArrayList<>(); | ||
for (int j = 0; j < 4; j++) { | ||
waiting.add(CompletableFuture.runAsync(() -> { | ||
for (int k = 0; k <= 100; k++) { | ||
assertNotNull(clone.get("a")); | ||
} | ||
})); | ||
} | ||
waiting.forEach(CompletableFuture::join); | ||
} | ||
} | ||
|
||
/** | ||
* @return the singleton IonSystem | ||
*/ | ||
|
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.
Is there a reason to only do this for
IonStructLite
as opposed toIonContainerLite
?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.
Do what, specifically? The field map is contained in
IonStructLite
.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 was wondering if eager initialization is needed for
IonContainerLite
as well, but it seems the issue is isolated toIonStructLite