<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.8</li>
<li>Operating System version: Windows 10</li>
<li>Java version: 1.8</li>
</ul>
<h3 dir="auto">Steps to reproduce this issue</h3>
<ol dir="auto">
<li>RpcStatus的beginCount的实现代码如下。</li>
</ol>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" /**
     * @param url
     */
    public static boolean beginCount(URL url, String methodName, int max) {
        max = (max &lt;= 0) ? Integer.MAX_VALUE : max;
        RpcStatus appStatus = getStatus(url);
        RpcStatus methodStatus = getStatus(url, methodName);
        if (methodStatus.active.get() == Integer.MAX_VALUE) {
            return false;
        }
        for (int i; ; ) {
            i = methodStatus.active.get();
            if (i + 1 &gt; max) {
                return false;
            }
            if (methodStatus.active.compareAndSet(i, i + 1)) {
                break;
            }
        }
        appStatus.active.incrementAndGet();
        return true;
    }"><pre class="notranslate"> <span class="pl-c">/**</span>
<span class="pl-c">     * @param url</span>
<span class="pl-c">     */</span>
    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-smi">boolean</span> <span class="pl-s1">beginCount</span>(<span class="pl-smi">URL</span> <span class="pl-s1">url</span>, <span class="pl-smi">String</span> <span class="pl-s1">methodName</span>, <span class="pl-smi">int</span> <span class="pl-s1">max</span>) {
        <span class="pl-s1">max</span> = (<span class="pl-s1">max</span> &lt;= <span class="pl-c1">0</span>) ? <span class="pl-smi">Integer</span>.<span class="pl-c1">MAX_VALUE</span> : <span class="pl-s1">max</span>;
        <span class="pl-smi">RpcStatus</span> <span class="pl-s1">appStatus</span> = <span class="pl-en">getStatus</span>(<span class="pl-s1">url</span>);
        <span class="pl-smi">RpcStatus</span> <span class="pl-s1">methodStatus</span> = <span class="pl-en">getStatus</span>(<span class="pl-s1">url</span>, <span class="pl-s1">methodName</span>);
        <span class="pl-k">if</span> (<span class="pl-s1">methodStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">get</span>() == <span class="pl-smi">Integer</span>.<span class="pl-c1">MAX_VALUE</span>) {
            <span class="pl-k">return</span> <span class="pl-c1">false</span>;
        }
        <span class="pl-k">for</span> (<span class="pl-smi">int</span> <span class="pl-s1">i</span>; ; ) {
            <span class="pl-s1">i</span> = <span class="pl-s1">methodStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">get</span>();
            <span class="pl-k">if</span> (<span class="pl-s1">i</span> + <span class="pl-c1">1</span> &gt; <span class="pl-s1">max</span>) {
                <span class="pl-k">return</span> <span class="pl-c1">false</span>;
            }
            <span class="pl-k">if</span> (<span class="pl-s1">methodStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">compareAndSet</span>(<span class="pl-s1">i</span>, <span class="pl-s1">i</span> + <span class="pl-c1">1</span>)) {
                <span class="pl-k">break</span>;
            }
        }
        <span class="pl-s1">appStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">incrementAndGet</span>();
        <span class="pl-k">return</span> <span class="pl-c1">true</span>;
    }</pre></div>
<p dir="auto">在以上代码的for循环中，存在着i + 1操作</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" for (int i; ; ) {
            i = methodStatus.active.get();
            if (i + 1 &gt; max) {
                return false;
            }
            if (methodStatus.active.compareAndSet(i, i + 1)) {
                break;
            }
        }"><pre class="notranslate"> <span class="pl-k">for</span> (<span class="pl-smi">int</span> <span class="pl-s1">i</span>; ; ) {
            <span class="pl-s1">i</span> = <span class="pl-s1">methodStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">get</span>();
            <span class="pl-k">if</span> (<span class="pl-s1">i</span> + <span class="pl-c1">1</span> &gt; <span class="pl-s1">max</span>) {
                <span class="pl-k">return</span> <span class="pl-c1">false</span>;
            }
            <span class="pl-k">if</span> (<span class="pl-s1">methodStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">compareAndSet</span>(<span class="pl-s1">i</span>, <span class="pl-s1">i</span> + <span class="pl-c1">1</span>)) {
                <span class="pl-k">break</span>;
            }
        }</pre></div>
<p dir="auto">问题如下：<br>
当 i == Integer.MAX_VALUE的时候，i + 1会发生溢出吧？如果发生了溢出，导致的后果是 i + 1-&gt;溢出-&gt; 大于max不成立 -&gt;cas设置成功-&gt;成功调用。<br>
发生这种情况的条件比较极端，前提是i的值为Integer.MAX_VALUE。在循环的上面有对值的判断，但在并发的情况下，此判断会失效。</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="if (methodStatus.active.get() == Integer.MAX_VALUE) {
            return false;
        }"><pre class="notranslate"><span class="pl-k">if</span> (<span class="pl-s1">methodStatus</span>.<span class="pl-s1">active</span>.<span class="pl-en">get</span>() == <span class="pl-smi">Integer</span>.<span class="pl-c1">MAX_VALUE</span>) {
            <span class="pl-k">return</span> <span class="pl-c1">false</span>;
        }</pre></div>
<p dir="auto">这算是bug吗？</p>