# Exception in background task: AttributeError: 'NoneType' object has no attribute 'send_email'

- **ID:** `python/fastapi-background-task-exception`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

BackgroundTasks function references a request-scoped dependency (DB session, current_user) that is closed after the response is returned.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 0.100+ | active | — | — |
| 0.110+ | active | — | — |

## Workarounds

1. **** (92% success)
   ```
   def send(uid: int):
    with SessionLocal() as s:
        user = s.get(User, uid)
        ...
background_tasks.add_task(send, user.id)  # pass IDs, not objects
   ```
2. **** (88% success)
   ```
   Use a task queue (Celery/RQ) for long-running or dependency-heavy work.
   ```

## Dead Ends

- **** — Silences the error but the task never runs correctly. (70% fail)
- **** — Blocks the response; defeats the purpose of background tasks. (80% fail)
