Skip to content

fix(serve): mlxcel serve aborts with a nested-runtime panic when auto-downloading a missing model #463

Closed

Description

@pjb7687

Summary

mlxcel serve -m <repo-id> panics with "Cannot start a runtime from within a runtime" when the model is not present locally and has to be downloaded. mlxcel run <repo-id> downloads and runs the same model fine. As a result the "serve a model by repo-id and let it auto-download" first-run flow is unusable.

Background

The download helper download_repo builds its own Tokio runtime and calls block_on on it. That is valid from a synchronous caller (mlxcel run) but Tokio forbids it when the current thread is already driving a runtime, which is exactly the case for mlxcel serve (the HTTP server runs under #[tokio::main]). So the same download path works from run and aborts from serve.

Proposed Solution

Resolve and download a missing model in the synchronous CLI layer BEFORE entering the async server runtime (mirroring what mlxcel run already does), so the blocking download never runs on a runtime thread. Alternatively (or additionally) make the sync download entry runtime-aware: detect an existing runtime via tokio::runtime::Handle::try_current() and offload the blocking work to a dedicated thread / spawn_blocking instead of Runtime::new().block_on, so no future caller can reintroduce the panic. Hoisting the download out of the runtime is cleaner and keeps the progress UX identical to run.

Implementation Notes

  • Panic origin: download_repo creates a fresh runtime and blocks on it at src/downloader/mod.rs:730 (tokio::runtime::Runtime::new() then rt.block_on(...)).
  • mlxcel serve dispatch: Commands::Serve(args) => commands::run_serve(args) at src/main.rs:1681; hoist the missing-model download ahead of the async runtime here.
  • mlxcel-server (src/bin/mlx_server.rs, also #[tokio::main]) calls download_repo near line 1135 while worker tasks are "parked"; verify it does not hit the same nested-runtime abort and fix both paths consistently.
  • Consider having download_repo detect an active runtime and either offload or return a clear error rather than aborting the process.

Acceptance Criteria

  • mlxcel serve -m <repo-id> with a not-yet-downloaded model downloads it and starts serving, with the same progress output as mlxcel run.
  • mlxcel-server -m <repo-id> with a missing model auto-downloads without a nested-runtime abort.
  • The download helper no longer aborts when invoked from within a Tokio runtime (by hoisting the download out, or by an in-runtime-safe offload path).
  • Regression coverage for the serve auto-download path (or a runtime-aware guard on the download entry).

Original Suggestion

Title: fix: mlxcel serve panics when it comes to downloading a model

Summary

mlxcel serve panics when it comes to downloading a model

Reproduction steps

mlxcel serve -m mlx-community/gemma-4-31B-it-qat-OptiQ-4bit --draft-model mlx-community/gemma-4-31B-it-assistant-bf16 --draft-kind mtp --port 12121
  [mlxcel] model 'mlx-community/gemma-4-31B-it-qat-OptiQ-4bit' not found locally; downloading into the mlxcel store...
  [mlxcel download] repo=mlx-community/gemma-4-31B-it-qat-OptiQ-4bit revision=main dest=/Users/parkj/.cache/mlxcel/models/mlx-community/gemma-4-31B-it-qat-OptiQ-4bit
  [mlxcel download] 13 files queued (filtered from 15 total siblings)

  thread 'main' (5651514) panicked at /Users/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.52.3/src/runtime/scheduler/multi_thread/mod.rs:91:9:
  Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous
   tasks.
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  ./run.sh: line 3: 19301 Abort trap: 6           mlxcel serve -m mlx-community/gemma-4-31B-it-qat-OptiQ-4bit --draft-model mlx-community/gemma-4-31B-it-assistant-bf16 --draft-kind mtp
  --port 12121

Expected behavior

It has to properly download the given model. Interestingly, mlxcel run <model name> works well, but only mlxcel serve panics at downloading a model.

Actual behavior

it panics

mlxcel version

v0.3.2

Install method

Homebrew (lablup/tap)

Platform

macOS (Apple Silicon)

Model checkpoint

No response

Relevant logs


Additional context

No response

Pre-submission checks

  • I searched existing issues and did not find a duplicate
  • This is not a security vulnerability (those go to SECURITY.md instead)

Activity

  1. added
    status:ready
    type:bug
    on Jun 28, 2026
  2. added
    priority:high
    area:cli
    on Jul 2, 2026
  3. changed the title [-]fix: mlxcel serve panics when it comes to downloading a model[/-] [+]fix: avoid nested Tokio runtime panic when serve auto-downloads a repo ID[/+] on Jul 2, 2026
  4. added
    area:core
    on Jul 2, 2026
  5. changed the title [-]fix: avoid nested Tokio runtime panic when serve auto-downloads a repo ID[/-] [+]fix(serve): mlxcel serve aborts with a nested-runtime panic when auto-downloading a missing model[/+] on Jul 2, 2026
  6. added
    status:done
    and removed
    status:ready
    on Sep 6, 2026
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

    area:cliarea:corepriority:highstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions