Timeout Configuration

Robyn supports comprehensive timeout configuration to handle high-concurrency scenarios and prevent resource exhaustion like the "Too many open files" error.

Configuration Options

Method Parameters

Configure timeouts directly in the app.start() method:

from robyn import Robyn

app = Robyn(__file__)

@app.get("/")
async def hello(request):
    return "Hello, world!"

# Configure timeout settings
app.start(
    host="0.0.0.0", 
    port=8080,
    client_timeout=30,          # Max seconds to read request headers (seconds)
    keep_alive_timeout=20       # Keep-alive timeout (seconds)
)

Environment Variables

Override configuration using environment variables:

# Set timeout configurations
export ROBYN_CLIENT_TIMEOUT=45
export ROBYN_KEEP_ALIVE_TIMEOUT=30

# Start your application
python app.py

Configuration Parameters

ParameterDefaultDescriptionEnvironment Variable
client_timeout30Maximum time (seconds) to wait for the client to transmit the complete request headers. This does not limit handler execution time or overall request duration — it only guards against slow or stalled clients during the initial header-reading phase. Maps to actix-web's client_request_timeout.ROBYN_CLIENT_TIMEOUT
keep_alive_timeout20Time (seconds) to keep idle connections alive before closing themROBYN_KEEP_ALIVE_TIMEOUT

Usage Examples

Basic Configuration

# Minimal timeout configuration
app.start(client_timeout=30)

High-Traffic Production Setup

# Optimized for high-traffic scenarios
app.start(
    host="0.0.0.0",
    port=8080,
    client_timeout=60,      # More time for clients on slow networks to send headers
    keep_alive_timeout=15   # Shorter keep-alive for faster turnover
)

Development Setup

# Development-friendly settings
app.start(
    client_timeout=300,     # Generous header-read timeout for debugging
    keep_alive_timeout=60   # Longer keep-alive for testing
)

Load Testing Configuration

# Optimized for load testing with tools like wrk
app.start(
    client_timeout=10,      # Quick header-read timeout
    keep_alive_timeout=5    # Fast connection turnover
)

Environment Variable Priority

Environment variables take precedence over method parameters:

# This will use ROBYN_CLIENT_TIMEOUT=60 if set, otherwise 30
app.start(client_timeout=30)

Troubleshooting

"Too Many Open Files" Error

If you encounter file descriptor exhaustion:

  1. Increase system limits:

    ulimit -n 65536
    
  2. Optimize timeout settings:

    app.start(
        client_timeout=15,      # Shorter timeouts
        keep_alive_timeout=5    # Faster connection cleanup
    )
    
  3. Use environment variables for deployment:

    export ROBYN_CLIENT_TIMEOUT=15
    export ROBYN_KEEP_ALIVE_TIMEOUT=5
    

Performance Tuning

For high-throughput APIs:

  • Lower keep_alive_timeout (5-15s)
  • Moderate client_timeout (15-30s)

For clients on slow networks:

  • Higher client_timeout (60-120s) so slow clients have time to send headers
  • Standard keep_alive_timeout (20-30s)

Note: client_timeout only controls how long the server waits for request headers — it does not cap handler execution time. If you need to limit how long a handler can run, implement that logic within the handler itself (e.g. with asyncio.wait_for).

Best Practices

  1. Always set explicit timeouts in production
  2. Use environment variables for deployment-specific configuration
  3. Test timeout settings with realistic load patterns
  4. Start with conservative values and tune based on metrics

Migration Guide

From Previous Versions

If upgrading from earlier Robyn versions, the default behavior changes:

Before (infinite timeout):

# Previously: no timeout (could cause resource exhaustion)
app.start(host="0.0.0.0", port=8080)

After (sensible defaults):

# Now: automatic 30s client timeout, 20s keep-alive
app.start(host="0.0.0.0", port=8080)
# Equivalent to:
app.start(
    host="0.0.0.0", 
    port=8080,
    client_timeout=30,
    keep_alive_timeout=20
)