Repository navigation
Description
OpenRouterProvider::model_pricing can deadlock: it holds a read guard on
models_cache and then calls fetch_models(), which awaits a write guard on
that same lock.
In crates/jcode-provider-openrouter-runtime/src/lib.rs:
async fn model_pricing(&self, model_id: &str) -> Option<ModelPricing> {
let cache = self.models_cache.read().await; // read guard acquired
if cache.fetched
&& let Some(model) = cache.models.iter().find(|m| m.id == model_id)
{
return Some(model.pricing.clone());
}
// ... disk cache attempt ...
if let Ok(models) = self.fetch_models().await // still holding `cache`
&& let Some(model) = models.iter().find(|m| m.id == model_id)
{
return Some(model.pricing.clone());
}
None
}cache is still alive at the fetch_models().await, because it is never
dropped and the early returns above are the only paths that release it. And
fetch_models does:
pub async fn fetch_models(&self) -> Result<Vec<ModelInfo>> {
// ...
let cache = self.models_cache.read().await;
// ...
let mut cache = self.models_cache.write().await; // waits foreverWith tokio::sync::RwLock, a pending writer blocks, so the task waits on a
lock it is itself holding a reader for.
When it triggers
Only on a cold catalog: cache.fetched false and no usable disk cache entry.
Both early-return paths release the guard, which is why this does not reproduce
once the catalog is warm.
It needs model_catalog = true, which is why I suspect it has gone unreported.
Possible fix
Dropping the guard before the fetch is enough — release it explicitly, or scope
the cache read so it ends before fetch_models() is awaited.
Notes
I have this change and a regression test running in a fork if a reference is
useful. Filing as an issue rather than a PR per CONTRIBUTING, since the
mechanism is visible from the source.
Activity
docs: report the remaining two patches upstream as issues
1jehuang commentedon Jul 29, 2026
Confirmed. The read guard is live at the
fetch_models().await,fetch_modelstakes a write guard on the samemodels_cache, and a pending tokio writer blocks, so the task waits on a lock it holds a reader for. Your note that only a cold catalog reproduces it, because both early-return paths release the guard first, is exactly right.Fixed in #656 by scoping the cache read to a block so the guard drops before the fetch.
One thing I want to be straight about: I did not add a regression test for this. A deadlock test means driving a cold cache through the pricing path with a timeout, and doing it in a way that actually fails before the fix rather than passing vacuously. You mentioned having a mutation-verified test in a fork. If you are willing to share that branch, it is worth more than anything I would write blind here.
--- — Jcode agent (automated triage), on behalf of @1jehuang
test(openrouter): reproduce the #649 model_pricing deadlock
1jehuang commentedon Jul 29, 2026
Update: I said earlier I had not written a regression test for this. I have now, and it is mutation-verified: reverting the guard-scoping fix makes it hang until the 10s timeout, and it passes with the fix. Commit
6d2ac7198on the PR branch.Sharing the construction detail, because my first attempt was a false pass and I think it is the same trap your fork's test would need to avoid. Two conditions have to hold together:
models_cache.fetchedis false, somodel_pricing's first early return (which does drop the guard) never fires.model_pricing's disk-cache branch fall through without returning, while makingfetch_models()take its disk-cache path, which is the only branch infetch_modelsthat acquires a write guard.With no disk cache at all,
fetch_models()goes straight to the HTTP call and never contends for the lock, so the test passes whether or not the bug is present. My first version did exactly that and looked green, which is worth flagging since "cold catalog" intuitively suggests "no cache anywhere".I would still like to see your version if you are willing to share, particularly if it covers a path mine misses.
--- — Jcode agent (automated triage), on behalf of @1jehuang
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
test(openrouter): reproduce the 1jehuang#649 model_pricing deadlock
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
test(openrouter): reproduce the 1jehuang#649 model_pricing deadlock
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
test(openrouter): reproduce the 1jehuang#649 model_pricing deadlock
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
test(openrouter): reproduce the 1jehuang#649 model_pricing deadlock
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
test(openrouter): reproduce the 1jehuang#649 model_pricing deadlock
fix: five triage fixes for Gemini schemas, macOS stdin, reload, MCP m…
test(openrouter): reproduce the 1jehuang#649 model_pricing deadlock