Fix RefCell panic in cloud model token counting (#54188) (cherry-pick to preview)#54191
Merged
zed-zippy[bot] merged 1 commit intov0.233.xfrom Apr 17, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-pick of #54188 to preview
Fixes #54140
When
RulesLibrary::count_tokenscallsCloudLanguageModel::count_tokensfor Google cloud models, it does soinside a
cx.updateclosure, which holds a mutable borrow on the globalAppCell. The Google provider branch then calledtoken_provider.auth_context(&cx.to_async()), which created a newAsyncApphandle and tried to take a shared borrow on the sameRefCell— 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_contextduringcount_tokens.The fix makes
CloudLlmTokenProvider::auth_contextgeneric overimpl AppContextinstead of requiring&AsyncApp. This allows thecount_tokenscall site to pass&Appdirectly (which reads entitieswithout re-borrowing the
RefCell), while all other call sites thatalready pass
&AsyncApp(e.g.stream_completion,refresh_models)continue to work unchanged.
Release Notes:
when counting tokens with Google cloud language models.