Skip to content

fix(quota): close refresh worker lifecycle gaps - #79

Merged
evenluo merged 2 commits into
mainfrom
codex/fix-quota-refresh-lifecycle
Aug 5, 2026
Merged

fix(quota): close refresh worker lifecycle gaps#79
evenluo merged 2 commits into
mainfrom
codex/fix-quota-refresh-lifecycle

Conversation

@evenluo

@evenluo evenluo commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

  • make quota refresh acceptance atomic with worker lifecycle shutdown
  • reject refreshes as refresh_unavailable from lifecycle cancellation onward and render a human-readable Live Capacity status
  • terminalize queued refresh tasks with a failed state and TTL, and align usage identity comments with the current domain vocabulary

Verification

  • make verify
  • go test -race -count=25 ./internal/quota for the shutdown lifecycle cases
  • final Standards review: no findings
  • final Spec review: no findings

Compatibility

  • no migration, schema, or dependency changes
  • manual Live Capacity refreshes during shutdown are now rejected instead of returning a task that cannot run

Issue closeout

Auto-close: none

@cursor

cursor Bot commented Jul 31, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eccd402047

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/quota/refresh.go
Comment on lines +211 to +215
if s.refreshWorkersClose || s.refreshWorkerCtx.Err() != nil {
s.refreshWorkerMu.Unlock()
return
return nil, refreshUnavailableCode
}
task, created := s.refreshTasks.enqueue(authIndex)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 将生命周期关闭与任务接收置于同一临界区

当应用关闭与刷新请求并发时,Err() 可能先返回 nil,随后生命周期 context 被取消,而任务仍在第 215 行入队并作为 Accepted 返回;worker 接管后只能观察到已取消的 context 并将任务标记失败。这仍会返回一个不可能执行 provider probe 的任务,违反本次在 CONTEXT.md 中新增的关闭期拒绝保证;需要先在同一把锁下将生命周期标记为 closing,再取消 context 和等待 worker,而不是仅采样 ctx.Err()

AGENTS.md reference: AGENTS.md:L36-L38

Useful? React with 👍 / 👎.

StopRefreshWorkers now marks closing and cancels the worker context
under the same lock, closing the TOCTOU window where a refresh task
could be accepted after ctx cancellation but before the close flag.
App shutdown stops workers before cancelling the background context.
@evenluo

evenluo commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

已处理 Codex review 反馈(P2: 生命周期关闭与任务接收临界区,internal/quota/refresh.go:211-215)。

修复(commit 5aa5953

  • refresh.goAttachRefreshWorkerLifecycle 内部用 context.WithCancel 派生 worker ctx 并保存 cancel;StopRefreshWorkers 在同一把锁下先标记 refreshWorkersClose 再取消 worker context,与 startRefreshTask 的接收检查互斥——关闭一旦开始,后续接收必然被拒绝,不再有「Accepted 但 ctx 已取消」的任务。
  • app.gostopBackgroundTasks 调整为先 StopRefreshWorkers(锁内取消 worker 子 ctx)再 backgroundCancel,避免 parent 取消传播重新引入锁外取消窗口。
  • 新增测试 TestStopRefreshWorkersCancelsWorkersWithoutExternalCancel:验证无需外部 cancel,StopRefreshWorkers 单独调用即可让 running worker 退出并标记 failed,且后续刷新被拒绝。

验证go build ./...go vet 通过;internal/... 全量测试通过;internal/quota-race 测试通过。

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 5aa5953e70

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@evenluo
evenluo merged commit 15ba221 into main Aug 5, 2026
1 check passed
@evenluo
evenluo deleted the codex/fix-quota-refresh-lifecycle branch August 5, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant