# RuntimeError: Working outside of request context.

- **ID:** `python/flask-working-outside-request-context`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Accessing request, session, or g globals from a background thread, Celery task, or CLI command without an active Flask request context.

## Version Compatibility

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

## Workarounds

1. **** (85% success)
   ```
   with app.test_request_context('/path', method='POST'):\n    do_work()
   ```
2. **** (95% success)
   ```
   def task(user_id, payload): ...\nuser_id = request.headers.get('X-User-Id')\ntask.delay(user_id, request.json)
   ```

## Dead Ends

- **** — app_context does not push a request context; request, session, and g still raise RuntimeError. (70% fail)
- **** — This only affects exception propagation, not context availability. (90% fail)
