{
  "id": "python/starlette-staticfiles-mount-order",
  "signature": "RuntimeError: Cannot add middleware after an application has started",
  "signature_zh": "RuntimeError: 应用启动后无法添加中间件",
  "regex": "RuntimeError:\\ Cannot\\ add\\ middleware\\ after\\ an\\ application\\ has\\ started",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "app.add_middleware / mount called inside a route handler or after the ASGI app has begun serving requests.",
  "root_cause_type": "generic",
  "root_cause_zh": "在路由处理程序内部或 ASGI 应用开始处理请求之后调用了 app.add_middleware / mount。",
  "versions": [
    {
      "version": "0.100+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "0.110+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Timing issue is runtime, not import order.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Middleware silently not applied; behavior unchanged.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Move app.add_middleware(...) and app.mount(...) to module top-level before uvicorn.run(app).",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "If dynamic, build a fresh Starlette app per config and swap it in the ASGI server.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}