Repository navigation
fix(e2e): fix stale lock recovery and improve lock debugging #1612
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
Changes from 3 commits
9dda8aa
3683bdf
58e6938
1b0e866
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,16 @@ func acquireOrg(ctx context.Context, client forge.Client, token, runID string, p | |
| logf("[org-pool] Trying to acquire %s...", org) | ||
| acquired, err := tryCreateLock(ctx, client, org, runID, logf) | ||
| if err != nil { | ||
| logf("[org-pool] Error trying %s: %v", org, err) | ||
| logf("[org-pool] Error trying %s: %v — will check for stale lock", org, err) | ||
| // tryCreateLock may return an error when the repo exists but the | ||
| // error message doesn't match isRepoAlreadyExists (e.g. GitHub | ||
| // returns "422 Validation Failed" without "already exists" in | ||
| // the top-level message). Still attempt stale lock recovery. | ||
| if token != "" { | ||
| if reclaimed := tryReclaimStaleLock(ctx, client, token, org, runID, logf); reclaimed { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Stale lock recovery called on any error type — wastes API quota (4/5 agents flagged, strongest consensus) When Same pattern repeats at line 122. Suggestion — gate on error type so only 422s trigger stale recovery: var apiErr *github.APIError
if token != "" && errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusUnprocessableEntity {
if reclaimed := tryReclaimStaleLock(ctx, client, token, org, runID, logf); reclaimed {
return org, nil
}
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, gated on 422 via |
||
| return org, nil | ||
| } | ||
| } | ||
| continue | ||
| } | ||
| if acquired { | ||
|
|
@@ -107,7 +116,12 @@ func acquireOrg(ctx context.Context, client forge.Client, token, runID string, p | |
| for _, org := range shuffled { | ||
| acquired, err := tryCreateLock(ctx, client, org, runID, logf) | ||
| if err != nil { | ||
| logf("[org-pool] Error trying %s: %v", org, err) | ||
| logf("[org-pool] Error trying %s: %v — will check for stale lock", org, err) | ||
| if token != "" { | ||
| if reclaimed := tryReclaimStaleLock(ctx, client, token, org, runID, logf); reclaimed { | ||
| return org, nil | ||
| } | ||
| } | ||
| continue | ||
| } | ||
| if acquired { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,10 +58,17 @@ type APIErrorDetail struct { | |
| Resource string `json:"resource"` | ||
| Field string `json:"field"` | ||
| Code string `json:"code"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| func (e *APIError) Error() string { | ||
| return fmt.Sprintf("github api: %d %s", e.StatusCode, e.Message) | ||
| s := fmt.Sprintf("github api: %d %s", e.StatusCode, e.Message) | ||
| for _, d := range e.Errors { | ||
| if d.Message != "" { | ||
| s += fmt.Sprintf(" (%s)", d.Message) | ||
| } | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // Unwrap returns forge.ErrNotFound for 404 errors, enabling errors.Is checks. | ||
|
|
@@ -110,19 +117,31 @@ func (c *LiveClient) do(ctx context.Context, method, path string, body any) (*ht | |
| return nil, fmt.Errorf("http %s %s: %w", method, path, err) | ||
| } | ||
|
|
||
| if !isRetryable(resp) { | ||
| retryable, respBody := isRetryable(resp) | ||
| if !retryable { | ||
| if respBody != nil { | ||
| // We read the body to check for secondary rate limit; | ||
| // replace it so callers can still read it. | ||
| resp.Body.Close() | ||
| resp.Body = io.NopCloser(bytes.NewReader(respBody)) | ||
| } | ||
| return resp, nil | ||
| } | ||
|
|
||
| // Drain and close the body before retrying. | ||
| io.Copy(io.Discard, resp.Body) | ||
| // Body already read or drained by isRetryable. | ||
| resp.Body.Close() | ||
|
|
||
| delay := retryDelay(resp, attempt) | ||
| retryAfter := resp.Header.Get("Retry-After") | ||
|
|
||
| if attempt == maxRetries-1 { | ||
| return nil, &APIError{StatusCode: resp.StatusCode, Message: "rate limited after retries"} | ||
| msg := fmt.Sprintf("rate limited after %d retries on %s %s (last delay: %s", maxRetries, method, path, delay) | ||
| if retryAfter != "" { | ||
| msg += fmt.Sprintf(", Retry-After: %s", retryAfter) | ||
| } | ||
| msg += ")" | ||
| return nil, &APIError{StatusCode: resp.StatusCode, Message: msg} | ||
| } | ||
|
|
||
| delay := retryDelay(resp, attempt) | ||
| select { | ||
| case <-time.After(delay): | ||
| case <-ctx.Done(): | ||
|
|
@@ -135,19 +154,35 @@ func (c *LiveClient) do(ctx context.Context, method, path string, body any) (*ht | |
| } | ||
|
|
||
| // isRetryable returns true for responses that should trigger a retry. | ||
| // GitHub uses 429 for primary rate limits and 403 with Retry-After for | ||
| // secondary rate limits. A plain 403 (e.g., permission denied) is not retried. | ||
| func isRetryable(resp *http.Response) bool { | ||
| // GitHub uses 429 for primary rate limits and 403 for secondary rate limits. | ||
| // Secondary rate limits may include a Retry-After header, or may only be | ||
| // identifiable by the response body containing "secondary rate limit". | ||
| func isRetryable(resp *http.Response) (bool, []byte) { | ||
| if resp.StatusCode == http.StatusTooManyRequests { | ||
| return true | ||
| io.Copy(io.Discard, resp.Body) | ||
| return true, nil | ||
| } | ||
| // GitHub secondary rate limit: 403 + Retry-After header. | ||
| if resp.StatusCode == http.StatusForbidden && resp.Header.Get("Retry-After") != "" { | ||
| return true | ||
| if resp.StatusCode == http.StatusForbidden { | ||
| if resp.Header.Get("Retry-After") != "" { | ||
| io.Copy(io.Discard, resp.Body) | ||
| return true, nil | ||
| } | ||
| // Check body for secondary rate limit without Retry-After header. | ||
| data, _ := io.ReadAll(resp.Body) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Unbounded Two issues on this line:
Suggestion: data, readErr := io.ReadAll(io.LimitReader(resp.Body, 1<<16)) // 64KB max
if readErr != nil {
return false, nil
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 1b0e866. LimitReader at 64KB and return |
||
| if strings.Contains(string(data), "secondary rate limit") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Case-sensitive substring match is fragile
Suggestion: if strings.Contains(strings.ToLower(string(data)), "secondary rate limit") {
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Folded into the same fix — |
||
| return true, nil | ||
| } | ||
| // Not a rate limit — return the body so the caller can still use it. | ||
| return false, data | ||
| } | ||
| return false | ||
| return false, nil | ||
| } | ||
|
|
||
| // secondaryRateLimitBackoff is the minimum backoff for secondary rate limits | ||
| // when no Retry-After header is present. GitHub's secondary rate limits | ||
| // typically require waiting at least 60 seconds. | ||
| var secondaryRateLimitBackoff = 60 * time.Second | ||
|
|
||
| // retryDelay calculates how long to wait before retrying. | ||
| // It uses the Retry-After header if present, otherwise exponential backoff. | ||
| func retryDelay(resp *http.Response, attempt int) time.Duration { | ||
|
|
@@ -156,6 +191,10 @@ func retryDelay(resp *http.Response, attempt int) time.Duration { | |
| return time.Duration(secs) * time.Second | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM]
Suggestion — cap at a reasonable max: if secs, err := strconv.Atoi(ra); err == nil && secs > 0 && secs <= 300 {
return time.Duration(secs) * time.Second
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capped at 300s in 1b0e866. |
||
| } | ||
| } | ||
| // For secondary rate limits (403), use a longer backoff. | ||
| if resp.StatusCode == http.StatusForbidden { | ||
| return secondaryRateLimitBackoff + time.Duration(math.Pow(2, float64(attempt)))*time.Second | ||
| } | ||
| // Exponential backoff: 1s, 2s, 4s | ||
| return time.Duration(math.Pow(2, float64(attempt))) * time.Second | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MEDIUM] Inconsistent
DeleteRepoerror handling vsacquireLock(4/5 agents flagged)Here in
tryReclaimStaleLock, aDeleteRepofailure causes immediatereturn false, abandoning the org. But inacquireLock(lines 84-87), the same failure is logged andtryCreateLockstill runs. This inconsistency means:DeleteReporeturns 404 here, andtryReclaimStaleLockgives up even thoughtryCreateLockwould succeed.acquireLock, the same scenario correctly falls through totryCreateLock.Suggestion — check if the error is a 404 (repo already gone) and proceed in that case:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch —
forge.IsNotFoundalready exists so this was easy. 1b0e866 now lets 404 fall through to tryCreateLock.