{
  "id": "rust/tokio-runtime-enter-error",
  "signature": "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 by another runtime.",
  "signature_zh": "无法在运行时内启动运行时。这是因为某个函数（如 `block_on`）试图阻塞当前线程，而该线程正被另一个运行时使用。",
  "regex": "Cannot start a runtime from within a runtime",
  "domain": "rust",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Attempting to create or enter a Tokio runtime (e.g., via `block_on`, `runtime.block_on`, or `#[tokio::main]`) while already inside an active Tokio runtime context on the same thread.",
  "root_cause_type": "generic",
  "root_cause_zh": "在同一个线程上已经处于活跃的 Tokio 运行时上下文中时，尝试创建或进入另一个 Tokio 运行时（例如通过 `block_on`、`runtime.block_on` 或 `#[tokio::main]`）。",
  "versions": [
    {
      "version": "tokio 1.35.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tokio 1.36.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tokio 1.37.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.75.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "If the spawned thread also tries to create a runtime, it will succeed on a new thread, but the original thread still has the nested runtime issue; also, sharing data across threads may require `Send + Sync`.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This still attempts to enter a runtime from within another runtime on the same thread; the error persists.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This can lead to multiple runtime instances and still cause the same error if any runtime is entered while another is active; it also complicates code structure.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use `tokio::task::spawn_blocking` to run blocking code on a separate thread pool without creating a new runtime: `tokio::task::spawn_blocking(move || { /* blocking work */ }).await`",
      "success_rate": 0.9,
      "how": "Use `tokio::task::spawn_blocking` to run blocking code on a separate thread pool without creating a new runtime: `tokio::task::spawn_blocking(move || { /* blocking work */ }).await`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Pass the runtime handle around using `tokio::runtime::Handle::current()` and use `handle.block_on()` only from non-async contexts; avoid calling `block_on` inside async code.",
      "success_rate": 0.85,
      "how": "Pass the runtime handle around using `tokio::runtime::Handle::current()` and use `handle.block_on()` only from non-async contexts; avoid calling `block_on` inside async code.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Restructure the code to use a single runtime: if you need to run blocking code, use `tokio::task::spawn_blocking`; if you need to call async code from sync code, use `tokio::runtime::Runtime::block_on` only at the top-level entry point.",
      "success_rate": 0.95,
      "how": "Restructure the code to use a single runtime: if you need to run blocking code, use `tokio::task::spawn_blocking`; if you need to call async code from sync code, use `tokio::runtime::Runtime::block_on` only at the top-level entry point.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use `tokio::task::spawn_blocking` to run blocking code on a separate thread pool without creating a new runtime: `tokio::task::spawn_blocking(move || { /* blocking work */ }).await`",
    "Pass the runtime handle around using `tokio::runtime::Handle::current()` and use `handle.block_on()` only from non-async contexts; avoid calling `block_on` inside async code.",
    "Restructure the code to use a single runtime: if you need to run blocking code, use `tokio::task::spawn_blocking`; if you need to call async code from sync code, use `tokio::runtime::Runtime::block_on` only at the top-level entry point."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#method.block_on",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2024-02-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}