Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部分请求方法会造成无限递归 #1591

Closed
shuxuefu opened this issue May 29, 2020 · 6 comments
Closed

部分请求方法会造成无限递归 #1591

shuxuefu opened this issue May 29, 2020 · 6 comments
Milestone

Comments

@shuxuefu
Copy link

简要描述

在开发过程中发现 BaseWxMpServiceImpl.execute()调用 executeInternal方法 异常内 如果出现指定code会刷新token后调用execute()
而execute()方法终止条件为 为方法变量 int retryTimes = 0; 那么会如果一直出现指定code会造成无限递归

模块版本情况

  • WxJava 模块名: weixin-java-mp
  • WxJava 版本号:3.7.0

详细描述

public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    int retryTimes = 0;

    while(true) {
        try {
            return this.executeInternal(executor, uri, data);
        } catch (WxErrorException var10) {
            if (retryTimes + 1 > this.maxRetryTimes) {
                log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
                throw new RuntimeException("微信服务端异常,超出重试次数");
            }

            WxError error = var10.getError();
            if (error.getErrorCode() != -1) {
                throw var10;
            }

            int sleepMillis = this.retrySleepMillis * (1 << retryTimes);

            try {
                log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
                Thread.sleep((long)sleepMillis);
            } catch (InterruptedException var9) {
                throw new RuntimeException(var9);
            }

            if (retryTimes++ >= this.maxRetryTimes) {
                log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
                throw new RuntimeException("微信服务端异常,超出重试次数");
            }
        }
    }
}

protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    E dataForLog = DataUtils.handleDataWithSecret(data);
    if (uri.contains("access_token=")) {
        throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
    } else {
        String accessToken = this.getAccessToken(false);
        String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;

        try {
            T result = executor.execute(uriWithAccessToken, data, WxType.MP);
            log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", new Object[]{uriWithAccessToken, dataForLog, result});
            return result;
        } catch (WxErrorException var9) {
            WxError error = var9.getError();
            if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
                this.getWxMpConfigStorage().expireAccessToken();
                if (this.getWxMpConfigStorage().autoRefreshToken()) {
                    return this.execute(executor, uri, data);
                }
            }

            if (error.getErrorCode() != 0) {
                log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", new Object[]{uriWithAccessToken, dataForLog, error});
                throw new WxErrorException(error, var9);
            } else {
                return null;
            }
        } catch (IOException var10) {
            log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", new Object[]{uriWithAccessToken, dataForLog, var10.getMessage()});
            throw new WxErrorException(WxError.builder().errorMsg(var10.getMessage()).build(), var10);
        }
    }
}
@binarywang
Copy link
Member

你是实际项目运行过程中发现有此问题吗?还只是猜测?

@lyh92

This comment has been minimized.

@binarywang

This comment has been minimized.

@nengjie
Copy link

nengjie commented Jun 20, 2020

我们实际项目里面确实在这里出现了无限递归,最后导致微信的调用次数超过限制,我们最后通过链路追踪只能看到不听的递归,而递归只有可能是微信那边返回的错误码就是这里的递归条件,建议这个地方可以完善一下

@ghostg00
Copy link

我也遇到了 这个问题 假如获取的token 一直是失效的就导致 这种情况 但其实一般是想尝试一定次数 就直接异常了 这样不会占用服务资源

@binarywang
Copy link
Member

看来出现此问题可能跟具体的配置有关系,如果配置没问题,就不会导致token一直无效,也就不会导致循环递归调用。
目前准备先增加代码把相关错误日志打印出来,以方便使用分析错误原因,从而修复配置。

@binarywang binarywang changed the title BaseWxMpServiceImpl 请求方法会造成无限递归 部分请求方法会造成无限递归 Aug 23, 2020
@binarywang binarywang added this to the 3.9.0 milestone Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants