GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 部分请求方法会造成无限递归 · Issue #1591 · binarywang/WxJava

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

New issue
Copy link
New issue
Copy link
Closed
Closed
部分请求方法会造成无限递归#1591
Copy link
Labels
公众号
Milestone
3.9.0

Description

@shuxuefu
shuxuefu
opened on May 29, 2020
Issue body actions

简要描述

在开发过程中发现 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);
        }
    }
}

Activity

  1. binarywang commented on May 29, 2020

    @binarywang
    binarywang
    on May 29, 2020
    Owner
    More actions

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

  2. lyh92 commented on Jun 11, 2020

    @lyh92
    lyh92
    on Jun 11, 2020 · Hidden as off-topic
    show commentMore actions
  3. binarywang commented on Jun 11, 2020

    @binarywang
    binarywang
    on Jun 11, 2020 · Hidden as off-topic
    show commentMore actions
  4. nengjie commented on Jun 20, 2020

    @nengjie
    nengjie
    on Jun 20, 2020
    More actions

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

  5. ghostg00 commented on Jun 24, 2020

    @ghostg00
    ghostg00
    on Jun 24, 2020
    More actions

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

  6. binarywang
    added
    公众号
    on Jul 5, 2020
  7. binarywang commented on Aug 23, 2020

    @binarywang
    binarywang
    on Aug 23, 2020
    Owner
    More actions

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

  8. binarywang
    changed the title [-]BaseWxMpServiceImpl 请求方法会造成无限递归[/-] [+]部分请求方法会造成无限递归[/+] on Aug 23, 2020
  9. binarywang
    added this to the 3.9.0 milestone on Aug 23, 2020
  10. binarywang
    added a commit that references this issue on Aug 23, 2020

    🎨 #1591 给部分请求方法导致无限递归调用的代码增加日志,方便排查问题

    33b13b1
  11. binarywang
    closed this as completedon Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    公众号

    Projects

    No projects

      Milestone

      • 3.9.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.