anthropics / anthropics/anthropic-sdk-go
requestconfig: prevent per-attempt timeout timer leaks during retries
- 主要语言
- Go
- 星标
- 1.2k
- 派生
- 213
- 平均合并
- 1 天 12 小时
- 30 天内合并 PR
- 11
描述
## Environment
**Go SDK**: v1.62.0
## Bug
RequestConfig.Execute creates a timeout for each retry attempt.
The problem is that when we retry, the timeout from the previous attempt is not cancelled. The cancel variable gets replaced with the new attempt's cancel function.
There is also a defer inside the loop, but all the deferred functions use the same cancel variable. When the function finishes, they all end up calling the cancel function from the last attempt.
This means the earlier timers are never cancelled and stay alive until their timeout expires.
For example, with 4 attempts:
```
Attempt 1 → timer A
Attempt 2 → timer B
Attempt 3 → timer C
Attempt 4 → timer D
```
```
Execute returns → D is cancelled
A, B, C remain alive
```
## Impact
With the default `MaxRetries=2`, a request that uses all its retries can leave 2 timers running unnecessarily.
**The requests still work as expected.** But if there are lots of retries, especially from 429 or 5xx responses, these timers can build up and use extra memory and runtime resources.
## Expected vs actual
**Expected:**
- Cancel the current timeout before starting the next retry.
- Cancel the final timeout when Execute returns.
**Actual:**
- The previous timeout is never cancelled.
- Only the last timeout gets cancelled.
- Earlier timers stay alive until they expire.
## Suggested fix
Cancel the previous timeout at the start of each retry and keep one defer outside the loop for the final timeout.
贡献指南
调研方向
Start at RequestConfig.Execute and trace how the timeout and cancel function are created for each retry attempt. Verify the retry flow and existing tests, then confirm that each prior attempt's timeout is cancelled before the next attempt and that the final timeout is cancelled when Execute returns.
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- go
- 领域
- api
- Issue 类型
- 缺陷
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 冷清
- 描述清晰度
- 描述清楚
- 新手友好度
- 74/100