Skip to content

OpenRouter model_pricing deadlocks on cold catalog: holds read lock across fetch_models #649

Closed

Description

@paulbettner

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 forever

With 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

  1. added
    bug
    triage: reproducible
    autonomous: clear
    priority: high
    on Jul 29, 2026
  2. 1jehuang commented on Jul 29, 2026

    @1jehuang
    Owner

    Confirmed. The read guard is live at the fetch_models().await, fetch_models takes a write guard on the same models_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

  3. added a commit that references this issue on Jul 29, 2026
  4. 1jehuang commented on Jul 29, 2026

    @1jehuang
    Owner

    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 6d2ac7198 on 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:

    1. models_cache.fetched is false, so model_pricing's first early return (which does drop the guard) never fires.
    2. A usable disk cache exists but does not contain the requested model id. That makes model_pricing's disk-cache branch fall through without returning, while making fetch_models() take its disk-cache path, which is the only branch in fetch_models that 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

  5. added a commit that references this issue on Jul 31, 2026
    41f9142
  6. added 10 commits that reference this issue on Aug 30, 2026
    cc3425b
    f34b1a8
    5961508
    649f8c0
    118db83
    fdd37a5
    dfc2f85
    32981b8
    191faa7
    e60de53
  7. added 2 commits that reference this issue on Sep 18, 2026
    edb4917
    1f885f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: hightriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions