Skip to content

Fix RefCell panic in cloud model token counting (#54188) (cherry-pick to preview)#54191

Merged
zed-zippy[bot] merged 1 commit intov0.233.xfrom
cherry-pick-v0.233.x-76aab0c3
Apr 17, 2026
Merged

Fix RefCell panic in cloud model token counting (#54188) (cherry-pick to preview)#54191
zed-zippy[bot] merged 1 commit intov0.233.xfrom
cherry-pick-v0.233.x-76aab0c3

Conversation

@zed-zippy
Copy link
Copy Markdown
Contributor

@zed-zippy zed-zippy Bot commented Apr 17, 2026

Cherry-pick of #54188 to preview


Fixes #54140

When RulesLibrary::count_tokens calls
CloudLanguageModel::count_tokens for Google cloud models, it does so
inside a cx.update closure, which holds a mutable borrow on the global
AppCell. The Google provider branch then called
token_provider.auth_context(&cx.to_async()), which created a new
AsyncApp handle and tried to take a shared borrow on the same
RefCell — causing a "RefCell already mutably borrowed" panic.

This only affects Google models because they are the only provider that
counts tokens server-side via an HTTP request (requiring
authentication). The other providers (Anthropic, OpenAI, xAI) count
tokens locally using tiktoken, so they never call auth_context during
count_tokens.

The fix makes CloudLlmTokenProvider::auth_context generic over impl AppContext instead of requiring &AsyncApp. This allows the
count_tokens call site to pass &App directly (which reads entities
without re-borrowing the RefCell), while all other call sites that
already pass &AsyncApp (e.g. stream_completion, refresh_models)
continue to work unchanged.

Release Notes:

  • Fixed a crash ("RefCell already mutably borrowed") that could occur
    when counting tokens with Google cloud language models.

Fixes #54140

When `RulesLibrary::count_tokens` calls
`CloudLanguageModel::count_tokens` for Google cloud models, it does so
inside a `cx.update` closure, which holds a mutable borrow on the global
`AppCell`. The Google provider branch then called
`token_provider.auth_context(&cx.to_async())`, which created a new
`AsyncApp` handle and tried to take a shared borrow on the same
`RefCell` — causing a "RefCell already mutably borrowed" panic.

This only affects Google models because they are the only provider that
counts tokens server-side via an HTTP request (requiring
authentication). The other providers (Anthropic, OpenAI, xAI) count
tokens locally using tiktoken, so they never call `auth_context` during
`count_tokens`.

The fix makes `CloudLlmTokenProvider::auth_context` generic over `impl
AppContext` instead of requiring `&AsyncApp`. This allows the
`count_tokens` call site to pass `&App` directly (which reads entities
without re-borrowing the `RefCell`), while all other call sites that
already pass `&AsyncApp` (e.g. `stream_completion`, `refresh_models`)
continue to work unchanged.

Release Notes:

- Fixed a crash ("RefCell already mutably borrowed") that could occur
when counting tokens with Google cloud language models.
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 17, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator Bot requested review from a team, maxbrunsfeld, reflectronic and rtfeldman and removed request for a team April 17, 2026 15:04
@zed-community-bot zed-community-bot Bot added the bot Pull requests authored by a bot label Apr 17, 2026
@zed-zippy zed-zippy Bot merged commit 0e90169 into v0.233.x Apr 17, 2026
42 checks passed
@zed-zippy zed-zippy Bot deleted the cherry-pick-v0.233.x-76aab0c3 branch April 17, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot Pull requests authored by a bot cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant