diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java index 46496208c54..a58055e5155 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java @@ -325,9 +325,10 @@ public String getParameter(String key) { } public String getMethodParameter(String method, String key, String defaultValue) { + // set consumerMethodParams firstly to avoid NPE at race condition. if (methodParams == null) { - methodParams = URL.toMethodParameters(params); consumerMethodParams = URL.toMethodParameters(consumerParams); + methodParams = URL.toMethodParameters(params); } String value = getMethodParameter(method, key, consumerMethodParams); @@ -359,12 +360,14 @@ public boolean hasMethodParameter(String method, String key) { } public boolean hasMethodParameter(String method) { + // set consumerMethodParams firstly to NPE at race condition. if (methodParams == null) { - methodParams = URL.toMethodParameters(params); consumerMethodParams = URL.toMethodParameters(consumerParams); + methodParams = URL.toMethodParameters(params); } - return consumerMethodParams.containsKey(method) || methodParams.containsKey(method); + return (CollectionUtils.isNotEmptyMap(consumerMethodParams) && consumerMethodParams.containsKey(method)) + || (CollectionUtils.isNotEmptyMap(methodParams) && methodParams.containsKey(method)); } public String toDescString() {