{
  "id": "python/flask-app-context-in-thread",
  "signature": "RuntimeError: Working outside of application context. (raised in threading.Thread target)",
  "signature_zh": "RuntimeError: 在应用上下文之外工作（在线程目标函数中抛出）。",
  "regex": "RuntimeError:\\ Working\\ outside\\ of\\ application\\ context\\.\\ \\(raised\\ in\\ threading\\.Thread\\ target\\)",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A background thread spawned from a request tries to access current_app or db.session, but Flask's context is thread-local and not inherited.",
  "root_cause_type": "generic",
  "root_cause_zh": "从请求中启动的后台线程尝试访问 current_app 或 db.session，但 Flask 的上下文是线程局部的，不会继承。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "current_app is a LocalProxy; accessing it in the thread still raises.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Unrelated to context propagation.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "def worker(app, payload):\n    with app.app_context():\n        ...\nthreading.Thread(target=worker, args=(current_app._get_current_object(), payload)).start()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use Celery with a Flask app factory and `task_context` pattern to inject app context.",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-12-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}