anthropics / anthropics/anthropic-sdk-go
requestconfig: prevent per-attempt timeout timer leaks during retries
- Lingua principale
- Go
- Stelle
- 1.2k
- Fork
- 213
- Merge medio
- 1g 12h
- PR unite (30g)
- 11
Descrizione
## 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.
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.