Skip to content

fix(scheduling): release acquired slot when account hydration fails - #8631

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
lishuangqiang:fix/slot-leak-on-hydrate-error
Nov 10, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
lishuangqiang:fix/slot-leak-on-hydrate-error

Conversation

@lishuangqiang

@lishuangqiang lishuangqiang commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

问题

newSelectionResult 是 Anthropic/Gemini 选号成功后的唯一出口。它在 hydrateSelectedAccount 失败时直接返回错误:

hydrated, err := s.hydrateSelectedAccount(ctx, account)
if err != nil {
    return nil, err
}

但调用方在此之前已经通过 tryAcquireAccountSlot 从 Redis 占用了并发槽位,而且只能从成功结果里拿到 ReleaseFunc。所以这里返回错误时,槽位不会释放,只能等 TTL(默认 15 分钟)回收。

触发条件

  1. 选号成功,Redis 里已经写入一个并发槽位。
  2. schedulerSnapshot.GetAccount 失败(Redis 抖动/超时),或者账号在选号后被删除,返回 nil。
  3. newSelectionResult 返回错误,result.ReleaseFunc 被丢弃。

结果:这个账号在最多 15 分钟内并发上限少 1。如果 hydrate 持续失败,账号会被逐步占满,表现为“账号明明空闲,但请求一直排队或 503”。

OpenAI 侧已经有对应处理(newAcquiredSelectionResult 在出错时调用 release()),Anthropic/Gemini 侧漏了这一步。

修复

hydrateSelectedAccount 失败时,如果 release != nil 就先释放槽位,再返回错误。

测试

新增 gateway_scheduling_slot_release_test.go:

  • hydrate 失败:断言 release 被调用一次;
  • hydrate 成功:断言不会提前 release,ReleaseFunc 正常交给调用方。

本地验证:

  • go test -tags=unit ./internal/service/ 通过
  • go build ./... 通过

Closes #8632

newSelectionResult is the only exit for acquired Anthropic/Gemini
selections. When hydrateSelectedAccount failed, the already acquired
Redis concurrency slot was dropped with the error because callers only
receive ReleaseFunc through a successful result.

Release it on the error path, matching the OpenAI path that already does
this via newAcquiredSelectionResult. Without the fix the slot stays
occupied until its TTL expires, so the account can look idle while
requests queue or fail.
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@lishuangqiang

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 10, 2026
@Wei-Shaw
Wei-Shaw merged commit b455228 into Wei-Shaw:main Nov 10, 2026
10 of 11 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 选号后账号 hydrate 失败会泄漏 Redis 并发槽位,账号最多 15 分钟少一个并发

2 participants