Skip to content

Commit

Permalink
fix apache#10078 potential ConcurrentModificationException on async s…
Browse files Browse the repository at this point in the history
…cenarios.
  • Loading branch information
chickenlj committed Jun 10, 2022
1 parent db15513 commit 86e7733
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

Expand Down Expand Up @@ -112,7 +113,7 @@ public RpcInvocation(Invocation invocation) {
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
this(invocation.getTargetServiceUniqueName(), invocation.getServiceModel(), invocation.getMethodName(), invocation.getServiceName(),
invocation.getProtocolServiceKey(), invocation.getParameterTypes(), invocation.getArguments(),
new HashMap<>(invocation.getObjectAttachments()), invocation.getInvoker(), invocation.getAttributes(),
new ConcurrentHashMap<>(invocation.getObjectAttachments()), invocation.getInvoker(), invocation.getAttributes(),
invocation instanceof RpcInvocation ? ((RpcInvocation) invocation).getInvokeMode() : null);
if (invoker != null) {
URL url = invoker.getUrl();
Expand Down Expand Up @@ -240,7 +241,7 @@ public RpcInvocation(String targetServiceUniqueName, ServiceModel serviceModel,
this.protocolServiceKey = protocolServiceKey;
this.parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
this.arguments = arguments == null ? new Object[0] : arguments;
this.attachments = attachments == null ? new HashMap<>() : attachments;
this.attachments = attachments == null ? new ConcurrentHashMap<>() : attachments;
this.attributes = attributes == null ? Collections.synchronizedMap(new HashMap<>()) : attributes;
this.invoker = invoker;
initParameterDesc();
Expand Down Expand Up @@ -381,7 +382,7 @@ public Map<String, Object> getObjectAttachments() {
}

public void setObjectAttachments(Map<String, Object> attachments) {
this.attachments = attachments == null ? new HashMap<>() : attachments;
this.attachments = attachments == null ? new ConcurrentHashMap<>() : attachments;
}

@Override
Expand All @@ -397,7 +398,7 @@ public Map<String, String> getAttachments() {

@Deprecated
public void setAttachments(Map<String, String> attachments) {
this.attachments = attachments == null ? new HashMap<>() : new HashMap<>(attachments);
this.attachments = attachments == null ? new ConcurrentHashMap<>() : new ConcurrentHashMap<>(attachments);
}

@Override
Expand All @@ -408,7 +409,7 @@ public void setAttachment(String key, Object value) {
@Override
public void setObjectAttachment(String key, Object value) {
if (attachments == null) {
attachments = new HashMap<>();
attachments = new ConcurrentHashMap<>();
}
attachments.put(key, value);
}
Expand All @@ -426,7 +427,7 @@ public void setAttachmentIfAbsent(String key, Object value) {
@Override
public void setObjectAttachmentIfAbsent(String key, Object value) {
if (attachments == null) {
attachments = new HashMap<>();
attachments = new ConcurrentHashMap<>();
}
if (!attachments.containsKey(key)) {
attachments.put(key, value);
Expand All @@ -439,7 +440,7 @@ public void addAttachments(Map<String, String> attachments) {
return;
}
if (this.attachments == null) {
this.attachments = new HashMap<>();
this.attachments = new ConcurrentHashMap<>();
}
this.attachments.putAll(attachments);
}
Expand All @@ -449,7 +450,7 @@ public void addObjectAttachments(Map<String, Object> attachments) {
return;
}
if (this.attachments == null) {
this.attachments = new HashMap<>();
this.attachments = new ConcurrentHashMap<>();
}
this.attachments.putAll(attachments);
}
Expand Down

0 comments on commit 86e7733

Please sign in to comment.