# RuntimeError: Lifespan protocol error: received 'lifespan.shutdown' before 'lifespan.startup.complete'

- **ID:** `python/starlette-asgi-lifespan-error`
- **Domain:** python
- **Category:** system_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The ASGI server sent a shutdown signal before the startup completed, often due to an error in the startup handler.

## Version Compatibility

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

## Workarounds

1. **Fix the error in the startup handler** (90% success)
   ```
   @app.on_event('startup')
async def startup():
    try:
        # initialization code
    except Exception as e:
        print(f"Startup error: {e}")
   ```
2. **Use try-except in lifespan context manager** (85% success)
   ```
   @asynccontextmanager
async def lifespan(app):
    try:
        await startup()
        yield
    finally:
        await shutdown()
   ```

## Dead Ends

- **Ignoring the error and restarting the server** — Error will recur if the startup handler still fails. (70% fail)
- **Removing the lifespan handler entirely** — Removes startup/shutdown functionality, but may hide important initialization. (60% fail)
