TimeoutError
asyncio.exceptions.TimeoutError
Stack trace
asyncio.exceptions.TimeoutError: The RunPod serverless handler exceeded the allowed execution time limit and was terminated.
Why it happens
RunPod serverless functions have a maximum execution time limit configured by the platform. If your handler runs longer than this limit, the platform forcibly terminates it, raising a TimeoutError. This often happens due to long-running synchronous code, blocking calls, or inefficient resource usage.
Detection
Monitor function execution duration metrics and catch asyncio.exceptions.TimeoutError in your handler logs to detect when timeouts occur before they crash your service.
Causes & fixes
Handler function contains blocking or long-running synchronous code that exceeds the timeout limit.
Refactor the handler to use asynchronous calls and optimize logic to complete within the allowed time.
External API calls or I/O operations in the handler are slow or unresponsive, causing delays.
Add timeouts and retries to external calls, and consider caching or prefetching data to reduce latency.
The configured timeout for the RunPod serverless function is too low for the workload.
Increase the timeout setting in the RunPod function configuration to allow more execution time.
Code: broken vs fixed
import runpod
def handler(event):
# Blocking call causing timeout
import time
time.sleep(120) # This will cause a timeout error
return {'status': 'done'}
runpod.serverless.start(handler) # This line triggers the timeout error import os
import asyncio
import runpod
os.environ['RUNPOD_API_KEY'] = os.environ.get('RUNPOD_API_KEY') # Use env var for API key
async def handler(event):
# Use async sleep instead of blocking sleep
await asyncio.sleep(1) # Reduced sleep to avoid timeout
return {'status': 'done'}
runpod.serverless.start(handler) # Fixed: async handler and no blocking calls Workaround
Wrap long-running code in asyncio.wait_for with a timeout shorter than the platform limit and catch TimeoutError to handle partial results or retry logic.
Prevention
Design serverless handlers to be asynchronous, avoid blocking calls, and monitor execution time metrics to keep within RunPod's timeout limits.