Timeouts and Deadlines (Prevent Hanging)
On this page
Timeout vs Deadline
- timeout: limit for one operation
- deadline: total time budget across multiple steps
Budget the Whole Request
Pick an overall deadline and allocate sub-timeouts so downstream calls cannot consume the entire budget.
asyncio Timeout Example
import asyncio
async def call():
await asyncio.sleep(10)
async def main():
return await asyncio.wait_for(call(), timeout=0.5)
try:
asyncio.run(main())
except asyncio.TimeoutError:
pass
Operational Checklist
- Every external call has a timeout.
- Define a request/job deadline and enforce it.
- Expose timeout metrics and log timeout events with context.
Failure Modes
- Hung threads/tasks: missing timeouts cause resource exhaustion.
- Timeout mismatch: upstream times out first, leaving downstream still working.