# RuntimeError: Cannot add middleware after an application has started

- **ID:** `python/starlette-staticfiles-mount-order`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

app.add_middleware / mount called inside a route handler or after the ASGI app has begun serving requests.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   Move app.add_middleware(...) and app.mount(...) to module top-level before uvicorn.run(app).
   ```
2. **** (80% success)
   ```
   If dynamic, build a fresh Starlette app per config and swap it in the ASGI server.
   ```

## Dead Ends

- **** — Timing issue is runtime, not import order. (70% fail)
- **** — Middleware silently not applied; behavior unchanged. (80% fail)
