{
  "id": "python/starlette-runtime-error-application-context",
  "signature": "RuntimeError: You cannot use the ASGI lifespan protocol with a sync application",
  "signature_zh": "运行时错误：无法在同步应用中使用 ASGI 生命周期协议",
  "regex": "RuntimeError:\\ You\\ cannot\\ use\\ the\\ ASGI\\ lifespan\\ protocol\\ with\\ a\\ sync\\ application",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Starlette 应用被配置为异步生命周期，但应用本身是同步函数",
  "root_cause_type": "generic",
  "root_cause_zh": "Starlette 应用被配置为异步生命周期，但应用本身是同步函数",
  "versions": [
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "移除 lifespan 参数但保留 async def 应用",
      "why_fails": "应用仍为异步，但缺少生命周期管理，可能导致资源泄漏",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "使用 sync_to_async 包装应用但未正确配置",
      "why_fails": "包装后仍需要异步上下文，根本问题未解决",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "将应用改为异步函数或使用同步生命周期",
      "success_rate": 0.9,
      "how": "app = Starlette(routes=routes)  # 使用同步应用\n或者\nasync def app(scope, receive, send): ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "使用 starlette.applications.Starlette 的 lifespan 参数",
      "success_rate": 0.95,
      "how": "async def lifespan(app):\n    yield\napp = Starlette(routes=routes, lifespan=lifespan)",
      "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.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}