开启地域容灾后,网络错误变成 AttributeError,且不会重试

Open Beginner friendly
#236 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start in tencentcloud/common/abstract_client.py at AbstractClient._call_with_region_breaker, followed by _call_once, call, call_json and _call_and_deserialize to see how the returned value reaches _check_status. Reproduce with an endpoint pointing at a closed local port and StandardRetryer(max_attempts=3, backoff_fn=lambda n: 0), comparing disable_region_breaker True and False. Done means ClientNetworkError propagates in both modes, attempts equal max_attempts, and the breaker records the failure; add the unit test mocking send_request.

Written by the indexing model from the issue text.

Description

现象

按 README「地域容灾」一节打开地域容灾(client_profile.disable_region_breaker = False)后,只要请求本身失败(连接被拒、超时、DNS 失败等,ApiRequest.send_request 抛出 ClientNetworkError),调用方拿到的不是 TencentCloudSDKException,而是:

AttributeError: 'NoneType' object has no attribute 'status_code'

同时配置的 retryer(如 StandardRetryer)也不会重试,因为它只看得到 AttributeError。

原因

AbstractClient._call_with_region_breaker 在 except TencentCloudSDKException 里记录完熔断器的成功/失败之后没有重新抛出,函数返回 None;call / call_json / _call_and_deserialize 里的 _call_once 接着对这个 None 调 _check_status,于是变成 AttributeError。关闭地域容灾(默认)时走的是另一条分支,网络错误会正常以 ClientNetworkError 抛出并被重试。

怎么触发

地域容灾本来就是为主域名请求失败的场景准备的,所以正好是在它该起作用的时候出问题:

cp = ClientProfile(disable_region_breaker=False,
                   retryer=StandardRetryer(max_attempts=3))
client = CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou", profile=cp)
try:
    client.call_json("DescribeRegions", {})
except TencentCloudSDKException as e:   # 接不到
    ...
复现

master(91bcb61,release 3.1.211),macOS,Python 3.9。把 endpoint 指向一个不监听的本地端口(连接被拒),StandardRetryer(max_attempts=3, backoff_fn=lambda n: 0),统计实际发出的请求次数,分别关/开地域容灾:

disable_region_breaker=True  attempts=3 -> TencentCloudSDKException: code:ClientNetworkError ...
disable_region_breaker=False attempts=1 -> AttributeError: 'NoneType' object has no attribute 'status_code'
建议

在 except 分支里记录完熔断器状态后 raise,让异常照常往上走。2024 年的 #183 提过同样的一行改动,后来被作者自己关掉了,PR 上没有评审记录;当前 master 上问题还在。我这边加了一个单测(mock send_request 抛 ClientNetworkError,关/开地域容灾两种情况都断言抛出 ClientNetworkError、重试次数等于 max_attempts、开启时熔断器记了相应的失败次数),可以提一个 PR。

Dominant language
Python
Stars
717
Forks
254
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TencentCloud/tencentcloud-sdk-python

All issues in TencentCloud/tencentcloud-sdk-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.