You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2022/02/23 02:06:31 UTC

[dubbo] branch 3.0 updated: fix, check before remove on embedded rpc call (#9704)

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new f2ccdd3  fix, check before remove on embedded rpc call (#9704)
f2ccdd3 is described below

commit f2ccdd322f2d4445104e11f383780ac48719630f
Author: ken.lj <ke...@gmail.com>
AuthorDate: Wed Feb 23 10:06:17 2022 +0800

    fix, check before remove on embedded rpc call (#9704)
---
 .../main/java/org/apache/dubbo/rpc/RpcContext.java | 26 ++++++++++++++++++----
 .../org/apache/dubbo/rpc/filter/ContextFilter.java |  7 +++++-
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
index 1305be1..67b24a3 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
@@ -107,6 +107,8 @@ public class RpcContext {
         CANCELLATION_CONTEXT.set(oldContext);
     }
 
+    private boolean remove = true;
+
     protected RpcContext() {
     }
 
@@ -156,6 +158,14 @@ public class RpcContext {
         return SERVER_ATTACHMENT.get();
     }
 
+    public boolean canRemove() {
+        return remove;
+    }
+
+    public void clearAfterEachInvoke(boolean remove) {
+        this.remove = remove;
+    }
+
     /**
      * Using to pass environment parameters in the whole invocation. For example, `remotingApplicationName`,
      * `remoteAddress`, etc. {@link RpcServiceContext}
@@ -171,19 +181,27 @@ public class RpcContext {
     }
 
     public static void removeClientAttachment() {
-        CLIENT_ATTACHMENT.remove();
+        if (CLIENT_ATTACHMENT.get().canRemove()) {
+            CLIENT_ATTACHMENT.remove();
+        }
     }
 
     public static void removeServerAttachment() {
-        SERVER_ATTACHMENT.remove();
+        if (SERVER_ATTACHMENT.get().canRemove()) {
+            SERVER_ATTACHMENT.remove();
+        }
     }
 
     /**
      * customized for internal use.
      */
     public static void removeContext() {
-        CLIENT_ATTACHMENT.remove();
-        SERVER_ATTACHMENT.remove();
+        if (CLIENT_ATTACHMENT.get().canRemove()) {
+            CLIENT_ATTACHMENT.remove();
+        }
+        if (SERVER_ATTACHMENT.get().canRemove()) {
+            SERVER_ATTACHMENT.remove();
+        }
         SERVER_LOCAL.remove();
         SERVICE_CONTEXT.remove();
         CANCELLATION_CONTEXT.remove();
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java
index 0ef30af..8ea367a 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java
@@ -126,7 +126,12 @@ public class ContextFilter implements Filter, Filter.Listener {
             ((RpcInvocation) invocation).setInvoker(invoker);
         }
 
-        return invoker.invoke(invocation);
+        try {
+            context.clearAfterEachInvoke(false);
+            return invoker.invoke(invocation);
+        } finally {
+            context.clearAfterEachInvoke(true);
+        }
     }
 
     @Override