AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Bug] 401 DefinitiveAuthenticationFailure increments retry.failures toward MAX_ACCOUNT_SWITCHES, failing requests before reaching healthy accounts

Open photobrand opened this issue 1 week ago • 0 comments

发生了什么 / What happened

Summary

When multiple accounts expire (HTTP 401, protocol code 16, e.g., ~6h cookie expiration), each newly discovered 401 account consumes one slot of MAX_ACCOUNT_SWITCHES. Once MAX_ACCOUNT_SWITCHES is exhausted on expired accounts, a single transient error (such as 503) on the next account aborts the entire request—even when healthy ready accounts are still available in the pool.

Observed Log Evidence (sanitized)

Pool had 10+ accounts, MAX_ACCOUNT_SWITCHES=5, ROUTING_STRATEGY=round-robin. Two accounts (account_g, account_h) were healthy and serving 200 OK concurrently (14:06:57 and 14:07:25). Yet request req_652 failed with 503:

  • 14:07:01 account_a -> 401 code 16 (failures=1, marked auth_required)
  • 14:07:04 account_b -> 401 code 16 (failures=2, marked auth_required)
  • 14:07:08 account_c -> 401 code 16 (failures=3, marked auth_required)
  • 14:07:11 account_d -> 401 code 16 (failures=4, marked auth_required)
  • 14:07:13 account_e -> 401 code 16 (failures=5, marked auth_required)
  • 14:07:18 account_f -> 503 code 14 (failures=6 > 5 -> request failed with 503, never trying account_g or account_h).

With the default MAX_ACCOUNT_SWITCHES=3, just 3 expired accounts in the pool cause requests to fail before reaching healthy accounts.

Root Cause Pointer

In internal/app/runtime.go (settleAttempt):

if retryable && !quotaFailure && !buildRejected && !missingInlineMedia && !recoverWorker && !coolingDown {
    retry.failures++
    retryable = int64(retry.failures) <= service.maxAccountSwitches.Load()
}

quotaFailure (429) and coolingDown (ErrAccountCoolingDown) are excluded from retry.failures, but DefinitiveAuthenticationFailure(err) (401) is counted as a switch failure even though ConfirmAuthenticationFailure immediately removes that stale account from the ready pool.

Suggested Fix

Exclude aistudio.DefinitiveAuthenticationFailure(err) from incrementing retry.failures in settleAttempt (the outer loop is still bounded by retry.maxAttempts = len(EnabledAccounts()), so each stale account is tried and moved to auth_required at most once without burning the healthy-account retry budget).

版本 / Version

No response

photobrand avatar Nov 21 '26 11:11 photobrand