<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have searched the <a href="https://github.com/apache/incubator-dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have checked the <a href="https://github.com/apache/incubator-dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: 2.7.0-SNAPSHOT</li>
</ul>
<p dir="auto">场景描述：<br>
新版异步方式，会在ConsumerContextFilter中设置 remoteAddress</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="RpcContext.getContext()
                .setInvoker(invoker)
                .setInvocation(invocation)
                .setLocalAddress(NetUtils.getLocalHost(), 0)
                .setRemoteAddress(invoker.getUrl().getHost(),
                        invoker.getUrl().getPort());"><pre class="notranslate"><span class="pl-smi">RpcContext</span>.<span class="pl-en">getContext</span>()
                .<span class="pl-en">setInvoker</span>(<span class="pl-s1">invoker</span>)
                .<span class="pl-en">setInvocation</span>(<span class="pl-s1">invocation</span>)
                .<span class="pl-en">setLocalAddress</span>(<span class="pl-smi">NetUtils</span>.<span class="pl-en">getLocalHost</span>(), <span class="pl-c1">0</span>)
                .<span class="pl-en">setRemoteAddress</span>(<span class="pl-s1">invoker</span>.<span class="pl-en">getUrl</span>().<span class="pl-en">getHost</span>(),
                        <span class="pl-s1">invoker</span>.<span class="pl-en">getUrl</span>().<span class="pl-en">getPort</span>());</pre></div>
<p dir="auto">且会在 ExceptionFilter 的 doPostProcess中使用</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="logger.error(&quot;Got unchecked and undeclared exception which called by &quot; + RpcContext.getContext().getRemoteHost()
                        + &quot;. service: &quot; + invoker.getInterface().getName() + &quot;, method: &quot; + invocation.getMethodName()
                        + &quot;, exception: &quot; + exception.getClass().getName() + &quot;: &quot; + exception.getMessage(), exception);"><pre class="notranslate"><span class="pl-s1">logger</span>.<span class="pl-en">error</span>(<span class="pl-s">"Got unchecked and undeclared exception which called by "</span> + <span class="pl-smi">RpcContext</span>.<span class="pl-en">getContext</span>().<span class="pl-en">getRemoteHost</span>()
                        + <span class="pl-s">". service: "</span> + <span class="pl-s1">invoker</span>.<span class="pl-en">getInterface</span>().<span class="pl-en">getName</span>() + <span class="pl-s">", method: "</span> + <span class="pl-s1">invocation</span>.<span class="pl-en">getMethodName</span>()
                        + <span class="pl-s">", exception: "</span> + <span class="pl-s1">exception</span>.<span class="pl-en">getClass</span>().<span class="pl-en">getName</span>() + <span class="pl-s">": "</span> + <span class="pl-s1">exception</span>.<span class="pl-en">getMessage</span>(), <span class="pl-s1">exception</span>);</pre></div>
<p dir="auto">那么问题来了。<br>
当连续的两个异步调用 A/B 在同一个线程先后执行的时候，A 的结果尚未返回，B 就开始执行，此时 A与 B 的 RpcContext 是同一个对象，就会导致 B 的 RpcContext 覆盖掉 A 的设置，导致 A 结果返回后获取到的 RpcContext 不再是 A 设置的内容，导致数据不一致。</p>
<p dir="auto">提供一种问题解决思路，仅供参考：</p>
<ol dir="auto">
<li>RpcContext 提供拷贝函数，对 attachments, values 进行深拷贝，其他属性浅拷贝；</li>
<li>AsyncRpcResult中 storedContext以及 storedServerContext 调用RpcContext.getContext().copy()完成赋值；</li>
</ol>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://github.com/apache/dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/apache/dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: 2.7.x</li>
<li>Operating System version: xxx</li>
<li>Java version: 1.8</li>
</ul>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/6637227/70201717-6788d800-1752-11ea-8b7a-32685857a147.png"><img src="https://user-images.githubusercontent.com/6637227/70201717-6788d800-1752-11ea-8b7a-32685857a147.png" alt="image" style="max-width: 100%;"></a></p>