# RuntimeError: Working outside of application context. (raised in threading.Thread target)

- **ID:** `python/flask-app-context-in-thread`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 2.x | active | — | — |
| 3.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   def worker(app, payload):
    with app.app_context():
        ...
threading.Thread(target=worker, args=(current_app._get_current_object(), payload)).start()
   ```
2. **** (90% success)
   ```
   Use Celery with a Flask app factory and `task_context` pattern to inject app context.
   ```

## Dead Ends

- **** — current_app is a LocalProxy; accessing it in the thread still raises. (90% fail)
- **** — Unrelated to context propagation. (95% fail)
