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

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 2 commits into from
Nov 1, 2023

Conversation

tgregg
Copy link
Contributor

@tgregg tgregg commented Nov 1, 2023

Issue #, if available:
Closes #629

Description of changes:

The build_field_map() method assigns an empty map to the _fields_map field, and then populates it. This can result in a race condition where the fields map is active (i.e. not null) before it is actually populated. This change ensures that the field map, if required, is initialized and populated before the struct is made read-only, making it available for concurrent access from multiple threads.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…structs with more than 5 members to be non-deterministic.
@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);
Copy link
Contributor

Choose a reason for hiding this comment

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

This name reads like a Boolean accessor.

@@ -261,6 +266,30 @@ public void modifyAfterCloneDoesNotChangeOriginal() {
assertNotEquals(original, clone);
}

@Test
public void readOnlyIonStructMultithreadedTest() {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion—It would be nice to have a more descriptive name.

@@ -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() {

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 to IonContainerLite?

Copy link
Contributor Author

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.

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 to IonStructLite

@tgregg tgregg changed the base branch from v1.10.5-base to master November 1, 2023 22:20
Copy link

codecov bot commented Nov 1, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Files Coverage Δ
src/com/amazon/ion/impl/lite/IonStructLite.java 86.66% <100.00%> (+0.13%) ⬆️

... and 1 file with indirect coverage changes

📢 Thoughts on this report? Let us know!.

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.

Unexpected behavior for cloned read-only IonStructs when shared across multiple threads
5 participants