From 86e77336aab687c767cf5c0e92c0e12c43263347 Mon Sep 17 00:00:00 2001 From: chickenlj Date: Fri, 10 Jun 2022 15:36:40 +0800 Subject: [PATCH] fix #10078 potential ConcurrentModificationException on async scenarios. --- .../org/apache/dubbo/rpc/RpcInvocation.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index c31c0a80b68..ed9d1eecfd5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -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; @@ -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(); @@ -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(); @@ -381,7 +382,7 @@ public Map getObjectAttachments() { } public void setObjectAttachments(Map attachments) { - this.attachments = attachments == null ? new HashMap<>() : attachments; + this.attachments = attachments == null ? new ConcurrentHashMap<>() : attachments; } @Override @@ -397,7 +398,7 @@ public Map getAttachments() { @Deprecated public void setAttachments(Map attachments) { - this.attachments = attachments == null ? new HashMap<>() : new HashMap<>(attachments); + this.attachments = attachments == null ? new ConcurrentHashMap<>() : new ConcurrentHashMap<>(attachments); } @Override @@ -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); } @@ -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); @@ -439,7 +440,7 @@ public void addAttachments(Map attachments) { return; } if (this.attachments == null) { - this.attachments = new HashMap<>(); + this.attachments = new ConcurrentHashMap<>(); } this.attachments.putAll(attachments); } @@ -449,7 +450,7 @@ public void addObjectAttachments(Map attachments) { return; } if (this.attachments == null) { - this.attachments = new HashMap<>(); + this.attachments = new ConcurrentHashMap<>(); } this.attachments.putAll(attachments); }