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: race condition when keys are added or removed while the object is being traversed #1062

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions parse/src/main/java/com/parse/ParseObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,9 @@ public Date getCreatedAt() {
/**
* Returns a set view of the keys contained in this object. This does not include createdAt,
* updatedAt, authData, or objectId. It does include things like username and ACL.
*
* <p>Note that while the returned set is unmodifiable, it is in fact not thread-safe, and
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
* creating a copy is recommended before iterating over it.
*/
public Set<String> keySet() {
synchronized (mutex) {
Expand Down
11 changes: 10 additions & 1 deletion parse/src/main/java/com/parse/ParseTraverser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
*/
package com.parse;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -94,7 +96,14 @@ private void traverseInternal(
} else if (root instanceof ParseObject) {
if (traverseParseObjects) {
ParseObject object = (ParseObject) root;
for (String key : object.keySet()) {
// Because the object's keySet is not thread safe, because the underlying Map isn't,
// we need to create a copy before iterating over the object's keys to avoid
// ConcurrentModificationExceptions
Set<String> keySet;
synchronized (object.mutex) {
keySet = new HashSet<>(object.keySet());
}
for (String key : keySet) {
traverseInternal(object.get(key), true, seen);
}
}
Expand Down