Skip to content

[Bug] Retry re-selects the same channel and never reaches higher-priority channels when session affinity is enabled #8063

Closed

Description

@ayakabot

Read This First (Do Not Remove This Section)

  • Docs: https://docs.newapi.ai/
  • Usage questions first: https://deepwiki.com/QuantumNous/new-api
  • Issues about forwarding behavior after enabling pass-through mode are not accepted; pass-through mode forwards requests directly, so please verify upstream behavior yourself.
  • Technical support requests such as coding plans or reverse-engineering channels are not accepted as issues.
  • Warning: issues with this template removed, section headings deleted, or content cleared may be closed directly. Repeated abusive submissions may result in a block.

Your current newapi version

v1.0.0-rc.47

Submission Checks

  • Non-duplicate issue: I have searched existing Issues and confirmed there are no similar issues.
  • Read this first: I have fully read the section above, reviewed the docs at https://docs.newapi.ai/ and the project README, and asked AI first, confirming this is not a usage, configuration, or integration question.
  • Template intact: I have not removed any guidance or section headings from this template and will complete it as requested.
  • Maintainer time: I understand that maintainers have limited time and issues that do not follow this template may be ignored or closed directly.

Issue Description

With channel affinity (session affinity) enabled and retry count set to 1, a retry can re-select the channel that just failed, and it can never reach channels whose priority is higher than the affinity channel.

Channel setup used for the reproduction (all channels serve the same model, same group):

channel priority weight
24 4 3
25 3 1
26 4 3
27 3 1
28 4 6
29 3 1

Observed behavior:

request -> session affinity picks 27 (priority 3)
        -> 27 fails
        -> retry #1 -> picks 27 again
        -> fails -> retries exhausted -> return error

retry: 27->27 in the log. Channels 24 / 26 / 28 (priority 4) are never tried.

Root cause (checked against main):

(a) Retry is used as an index into the priority list instead of being an attempt counter.

controller/relay.go runs the attempt loop with retryParam.GetRetry() starting at 0, and getChannel() calls service.CacheGetRandomSatisfiedChannel(retryParam), which forwards param.GetRetry() as the retry argument of model.GetRandomSatisfiedChannel(group, model, retry, filters).

In model/channel_cache.go:

sort.Sort(sort.Reverse(sort.IntSlice(sortedUniquePriorities)))
if retry >= len(uniquePriorities) {
    retry = len(uniquePriorities) - 1
}
targetPriority := int64(sortedUniquePriorities[retry])

So retry=0 means "priority 4" and retry=1 means "priority 3". Attempt 0 is consumed by session affinity, which by design overrides priority and lands on 27 (priority 3). With RetryTimes=1 the only retry is retry=1, i.e. the same priority tier the failed channel belongs to, and a weighted draw among 25 / 27 / 29 (1:1:1) can hand back 27 again. Priority-4 channels only exist at retry=0, which is already spent, so they are unreachable for the whole request.

The clamp retry = len(uniquePriorities) - 1 also means that raising the retry count does not walk the ladder back up: every retry beyond the number of tiers just re-rolls the lowest priority level.

(b) Channels already tried in this request are not excluded.

service.AppendUsedChannel only appends to the use_channel context key, which is read once at the end of controller/relay.go to print the retry: a->b log line. The selection path has no skip list, so a channel that already failed in this request remains a candidate for the next attempt.

Steps to Reproduce

  1. Create the six channels above, same model, same group.
  2. Enable channel affinity and set retry count to 1.
  3. Send a request whose affinity resolves to channel 27.
  4. Make channel 27 fail (invalid key or upstream 5xx).
  5. Check the retry log: retry: 27->27, and no attempt on 24 / 26 / 28.

Expected Result

A retry should pick a channel that has not been tried yet, and it should consider the full priority ladder from the top rather than being pinned to priority[sortedPriorities[retry]]:

request -> affinity 27 -> 27 fails
        -> retry #1 -> highest priority tier (4): 24 / 26 / 28, weighted 3:3:6
        -> only after all eligible channels are exhausted -> return error

Concretely: (1) retry selection should not be limited to a single priority tier derived from the retry counter, and (2) channels present in use_channel should be removed from the candidate set.

Related Screenshots

N/A

Activity

  1. added
    bug
    on Oct 27, 2026
  2. new-api-reviewer-bot commented on Oct 27, 2026

    @new-api-reviewer-bot

    [Bot Review]

    Thank you for submitting this bug report and providing detailed reproduction steps. We will look into the reported channel retry behavior.

  3. seefs001 commented on Oct 28, 2026

    @seefs001
    Collaborator

    #8078 修复了命中渠道亲和性后,首次重试跳过最高优先级的问题。亲和性渠道失败后,将从最高优先级开始普通选路,重试次数上限保持不变。

    重试时允许再次选择已尝试的渠道是现有设计,目前没有变更计划。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions