# INFO: 127.0.0.1:54321 - 'GET /sync-endpoint HTTP/1.1' 200 OK (took 5.02s, blocking loop detected)

- **ID:** `python/fastapi-event-loop-blocked`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A synchronous blocking call (e.g. time.sleep, requests, heavy CPU) was executed directly inside an async def endpoint, blocking the event loop and stalling all concurrent requests.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Offload blocking work to a thread:

import anyio
@app.get('/sync-endpoint')
async def handler():
    result = await anyio.to_thread.run_sync(blocking_fn)
   ```
2. **** (92% success)
   ```
   Define the route as def (not async def) so FastAPI runs it in a threadpool:

@app.get('/sync-endpoint')
def handler():
    return blocking_fn()
   ```
3. **** (90% success)
   ```
   Replace requests with httpx.AsyncClient and time.sleep with asyncio.sleep.
   ```

## Dead Ends

- **** — Each worker is still blocked; throughput per worker stays at one request. (70% fail)
- **** — Does not fix the blocking call inside the existing one. (90% fail)
- **** — Unrelated to loop blocking. (95% fail)
