Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Add guards around structures to prevent racing. #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

antihax
Copy link

@antihax antihax commented Mar 17, 2017

@wing328 From what I can tell, there are races around the data structures here when hit concurrently.

Please review and make sure my use of Collections.synchronizedList and synchronized are correct. I have not written anything in Java for some time.

I have these changes running locally with go test on a loop and beefed up the concurrency significantly in the go tests. Running 30 minutes with no failures now. This should resolve swagger-codegen #5102.

static List<Category> categories = new ArrayList<Category>();

static List<Pet> pets = Collections.synchronizedList(new ArrayList<Pet>());
static List<Category> categories = Collections.synchronizedList(new ArrayList<Category>());
Copy link

Choose a reason for hiding this comment

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

I guess you don't need to synchronize this, as it is not used anywhere outside the 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.

Removed. Thanks.


public class StoreData {
static List<Order> orders = new ArrayList<Order>();
static List<Order> orders = Collections.synchronizedList(new ArrayList<Order>());
Copy link

Choose a reason for hiding this comment

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

If you always access this in a synchronized block, there is no need for the additional synchronizedList.

Copy link
Author

Choose a reason for hiding this comment

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

Removed. Thanks,

@@ -68,11 +69,13 @@ public Pet getPetById(long petId) {

Copy link

@ePaul ePaul Mar 17, 2017

Choose a reason for hiding this comment

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

So you in almost all methods synchronize on pets, just not in getPetById (some lines up from here). Intentionally?
I'm not sure if this here could create some issue ...

The iterator is not synchronized, but checks for concurrent modifications (in an unsynchronized way).
So if any other request gets in between with deletion/addition, this could throw an exception.

On the other hand, if you synchronize everything anyways, you could get rid of the synchronizedList.

Copy link
Author

Choose a reason for hiding this comment

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

I missed the iterator around it. I also removed the synchronizedLists since all the operations are within sync {}.

@wing328
Copy link

wing328 commented Mar 17, 2017

@antihax thanks for the PR. Can you submit it against my repo instead: https://github.com/wing328/swagger-samples ? That way we can more easily test and deploy.

@antihax
Copy link
Author

antihax commented Mar 17, 2017

updated based on review. I will see if i can figure out to submit to your repo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants