Skip to content

RpcStatus中beginCount函数实现的一处疑问 #6621

@maslke

Description

@maslke
  • I have searched the issues of this repository and believe that this is not a duplicate.
  • I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

  • Dubbo version: 2.7.8
  • Operating System version: Windows 10
  • Java version: 1.8

Steps to reproduce this issue

  1. RpcStatus的beginCount的实现代码如下。
 /**
     * @param url
     */
    public static boolean beginCount(URL url, String methodName, int max) {
        max = (max <= 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 > max) {
                return false;
            }
            if (methodStatus.active.compareAndSet(i, i + 1)) {
                break;
            }
        }
        appStatus.active.incrementAndGet();
        return true;
    }

在以上代码的for循环中,存在着i + 1操作

 for (int i; ; ) {
            i = methodStatus.active.get();
            if (i + 1 > max) {
                return false;
            }
            if (methodStatus.active.compareAndSet(i, i + 1)) {
                break;
            }
        }

问题如下:
当 i == Integer.MAX_VALUE的时候,i + 1会发生溢出吧?如果发生了溢出,导致的后果是 i + 1->溢出-> 大于max不成立 ->cas设置成功->成功调用。
发生这种情况的条件比较极端,前提是i的值为Integer.MAX_VALUE。在循环的上面有对值的判断,但在并发的情况下,此判断会失效。

if (methodStatus.active.get() == Integer.MAX_VALUE) {
            return false;
        }

这算是bug吗?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions