Broadcast Errors // StreamRelay Lab
MediaMTX maxReaders & Linux Ephemeral Socket Exhaustion
Default systemd LimitNOFILE (1024) and conservative maxReaders caps reject new viewers around 400-800 concurrent connections even when bandwidth is available.
Measured MediaMTX & FFmpeg Telemetry
Each HLS or WebRTC viewer consumes file descriptors for TCP/TLS sockets, epoll handles, and segment reads. Hitting RLIMIT_NOFILE causes MediaMTX and Caddy/Nginx to drop new viewer handshakes with 502/503 errors.
| Subsystem / Signal | Measured Target | Operational Impact |
|---|---|---|
| Default Linux Soft NOFILE | 1,024 descriptors | Fails around 400-500 active viewers |
| Production LimitNOFILE | 1,048,576 descriptors | Supports 3,000-20,000+ viewers cleanly |
| MediaMTX maxReaders | 0 (unlimited) or 1.35x peak | Prevents accidental self-throttling |
Step-by-Step Triage & Config Remediation
- Raise LimitNOFILE in systemd: Set LimitNOFILE=1048576 in both mediamtx.service and your TLS reverse proxy unit.
- Increase somaxconn & ephemeral ports: Tune net.core.somaxconn=65535 and net.ipv4.ip_local_port_range=1024 65535.
- Size maxReaders to 1.35x target: Set maxReaders in mediamtx.yml above peak viewer projections (e.g., 4,050 for 3,000 viewers).
# Verify open file limit of running MediaMTX PID: cat /proc/$(pgrep mediamtx)/limits | grep "Max open files"
Diagnostic FAQ
Should MediaMTX terminate TLS directly or behind Caddy/Nginx?
For 500-3,000 viewers, Caddy or Nginx on localhost with HTTP/2 + KeepAlive handles Let’s Encrypt certificates and CORS headers cleanly.
Does maxReaders: 0 have a downside?
Setting 0 disables reader limits, which is fine if you monitor NIC saturation; setting 1.35x expected peak protects against runaway hotlinking.