Skip to content

Add file descriptor limits and buffered output for enclave logging #86

@AnthonyRonning

Description

@AnthonyRonning

Problem

Enclaves can experience file descriptor exhaustion under heavy load, and unbuffered output to vsock logging can cause delays or incomplete log delivery.

Solution

  1. Increase file descriptor limits to prevent exhaustion
  2. Add line buffering for socat output to ensure timely log delivery
  3. Monitor file descriptor usage to detect potential issues

Code Changes in entrypoint.sh

# 1. Increase file descriptor limits (add after setting APP_MODE)
# Increase file descriptor limits to prevent socket exhaustion
ulimit -n 65536
log "File descriptor limit set to: $(ulimit -n)"

# 2. Add buffering to socat output (replace the exec redirect line)
# Redirect all output to the logging script via VSOCK with buffering
# Use stdbuf to line-buffer output for timely delivery
exec > >(stdbuf -oL socat -u - VSOCK-CONNECT:3:8011) 2>&1

# 3. Add file descriptor monitoring (add before starting the main application)
# Start file descriptor monitoring in background
(
    while true; do
        sleep 300  # Check every 5 minutes
        FD_COUNT=$(ls /proc/*/fd 2>/dev/null | wc -l)
        log "File descriptors in use: $FD_COUNT"
        if [ "$FD_COUNT" -gt 50000 ]; then
            log "WARNING: High file descriptor usage: $FD_COUNT"
        fi
    done
) &

Benefits

  1. Prevents resource exhaustion: 65536 file descriptor limit provides ample headroom
  2. Reliable logging: Line buffering ensures logs are delivered promptly
  3. Early warning system: Monitoring alerts before hitting limits
  4. Better observability: Track resource usage over time

Testing

  • Verify limit with ulimit -n inside enclave
  • Monitor FD usage in logs every 5 minutes
  • Confirm logs appear immediately (not delayed/batched)
  • Test under load to ensure no "too many open files" errors

Impact

Critical for production stability - prevents service outages due to resource exhaustion and ensures complete log visibility for debugging issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions