You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/04/20 09:54:30 UTC

[dubbo] branch 3.0 updated: [3.0] Clear ThreadLocal before injvm invoke (#9946)

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

albumenj 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 f5e9d03663 [3.0] Clear ThreadLocal before injvm invoke (#9946)
f5e9d03663 is described below

commit f5e9d036633a9aec8d5310011b3d6a37d8e81745
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Wed Apr 20 17:53:53 2022 +0800

    [3.0] Clear ThreadLocal before injvm invoke (#9946)
---
 .../common/threadlocal/InternalThreadLocalMap.java   | 20 ++++++++++++++++++++
 .../dubbo/rpc/protocol/injvm/InjvmInvoker.java       | 10 +++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java
index e683b28512..3f3dfe4141 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java
@@ -55,6 +55,26 @@ public final class InternalThreadLocalMap {
         return slowGet();
     }
 
+    public static InternalThreadLocalMap getAndRemove() {
+        try {
+            Thread thread = Thread.currentThread();
+            if (thread instanceof InternalThread) {
+                return ((InternalThread) thread).threadLocalMap();
+            }
+            return slowThreadLocalMap.get();
+        } finally {
+            remove();
+        }
+    }
+
+    public static void set(InternalThreadLocalMap internalThreadLocalMap) {
+        Thread thread = Thread.currentThread();
+        if (thread instanceof InternalThread) {
+            ((InternalThread) thread).setThreadLocalMap(internalThreadLocalMap);
+        }
+        slowThreadLocalMap.set(internalThreadLocalMap);
+    }
+
     public static void remove() {
         Thread thread = Thread.currentThread();
         if (thread instanceof InternalThread) {
diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
index 1b99521c8d..2d4f734c88 100644
--- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
+++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.protocol.injvm;
 
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.threadlocal.InternalThreadLocalMap;
 import org.apache.dubbo.common.threadpool.manager.ExecutorRepository;
 import org.apache.dubbo.common.utils.ArrayUtils;
 import org.apache.dubbo.common.utils.ReflectUtils;
@@ -129,7 +130,14 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
             result.setExecutor(executor);
             return result;
         } else {
-            Result result = invoker.invoke(copiedInvocation);
+            Result result;
+            // clear thread local before child invocation, prevent context pollution
+            InternalThreadLocalMap originTL = InternalThreadLocalMap.getAndRemove();
+            try {
+                result = invoker.invoke(copiedInvocation);
+            } finally {
+                InternalThreadLocalMap.set(originTL);
+            }
             if (result.hasException()) {
                 AsyncRpcResult rpcResult = AsyncRpcResult.newDefaultAsyncResult(result.getException(), copiedInvocation);
                 rpcResult.setObjectAttachments(new HashMap<>(result.getObjectAttachments()));