{
  "id": "python/fastapi-background-task-exception",
  "signature": "Exception in background task: AttributeError: 'NoneType' object has no attribute 'send_email'",
  "signature_zh": "后台任务异常：AttributeError: 'NoneType' 对象没有属性 'send_email'",
  "regex": "Exception\\ in\\ background\\ task:\\ AttributeError:\\ 'NoneType'\\ object\\ has\\ no\\ attribute\\ 'send_email'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "BackgroundTasks function references a request-scoped dependency (DB session, current_user) that is closed after the response is returned.",
  "root_cause_type": "generic",
  "root_cause_zh": "BackgroundTasks 函数引用了请求作用域的依赖（数据库会话、current_user），而这些依赖在响应返回后即被关闭。",
  "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": "Silences the error but the task never runs correctly.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Blocks the response; defeats the purpose of background tasks.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "def send(uid: int):\n    with SessionLocal() as s:\n        user = s.get(User, uid)\n        ...\nbackground_tasks.add_task(send, user.id)  # pass IDs, not objects",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "Use a task queue (Celery/RQ) for long-running or dependency-heavy work.",
      "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.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-12-04",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}