dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

Dead cycle: Exception when scan the timeout invocation of remotin.

Open xhkyyy opened this issue 8 years ago • 4 comments

## 问题: RemotingInvocationTimeoutScan(DefaultFuture.java) 会无限循环的一直抛出: Exception when scan the timeout invocation of remoting.

## 造成的原因: 1.当使用 Guava 的 Lists 构建里一个 list 对象:

List<Integer> list = com.google.common.collect.Lists.transform(..., new Function<..., Integer>() {
                    @Override
                    public Integer apply(... obj) {
                        //某些条件下会 throw NullPointException
                    }
                });

注: (1) apply方法可能抛出 NullPointException。 O__O 我们的代码逻辑不严谨,没有检查空指针,已经修复。

(2) apply方法不会立即执行。

2.将上面的 list 对象作为参数,调用我的 dubbo Service。

myService.myMethod( list );

3.触发这个问题的前提是,当 myService 出现 dubbo 超时:

RemotingInvocationTimeoutScan(DefaultFuture.java) 会一直轮询检查 Future 超时。

源代码(RemotingInvocationTimeoutScan):

public void run() {
            while (true) {
                try {
                    for (DefaultFuture future : FUTURES.values()) {
                        if (future == null || future.isDone()) {
                            continue;
                        }
                        if (System.currentTimeMillis() - future.getStartTimestamp() > future.getTimeout()) {
                            // create exception response.
                            Response timeoutResponse = new Response(future.getId());
                            // set timeout status.
                            timeoutResponse.setStatus(future.isSent() ? Response.SERVER_TIMEOUT : Response.CLIENT_TIMEOUT);
                            timeoutResponse.setErrorMessage(future.getTimeoutMessage(true));
                            // handle response.
                            DefaultFuture.received(future.getChannel(), timeoutResponse);
                        }
                    }
                    Thread.sleep(30);
                } catch (Throwable e) {
                    logger.error("Exception when scan the timeout invocation of remoting.", e);
                }
            }
        }

在上面的代码中,执行到 future.getTimeoutMessage(true) 这一行的时候, 由于第 1 步中,list.apply 方法可能会抛出 NullPointException 异常, 所以,DefaultFuture.received( ... ) 也就一直得不到执行。从而以 30 毫秒间隔死循环一直抛下去。

getTimeoutMessage 的调用链应该是:getTimeoutMessage --> request(toString) --> RpcInvocation(toString) --> AbstractCollection(toString) --> list.apply

xhkyyy avatar Jan 08 '18 07:01 xhkyyy

Which dubbo version do you use? I think we have fixed this problem in the latest dubbo version 2.5.8/2.6.0

chickenlj avatar Jan 09 '18 07:01 chickenlj

RpcInvocation(toString)

private static String safeToString(Object data) {
        if (data == null) return null;
        String dataStr;
        try {
            dataStr = data.toString();
        } catch (Throwable e) {
            dataStr = "<Fail toString of " + data.getClass() + ", cause: " +
                    StringUtils.toString(e) + ">";
        }
        return dataStr;
    }

chickenlj avatar Jan 09 '18 07:01 chickenlj

@chickenlj I upgrade dubbo version to 2.6.0, and the problem has not reappeared so far. thank you!

xhkyyy avatar Jan 10 '18 02:01 xhkyyy

我看到这段代码2.5.X和2.6.X没有区别,怎么解决的?

swit1983 avatar Jul 30 '22 12:07 swit1983