← Back to Blog
DevOps November 17, 2026

Diagnose File Descriptor Leaks Before They Kill Your Service

A Python API service has been running for 14 days. Users report intermittent 500 errors. The application logs show OSError: [Errno 24] Too many open files. You check df and there's plenty of disk space. The service didn't crash — it's slowly accumulating file descriptors on every request until it hits the per-process ulimit and starts rejecting connections. By the time you notice, the process is holding 65,000+ FDs and the only fix is a restart that causes a brief outage.

The 5-minute setup

Create a lightweight watchdog script that alerts (or auto-restarts) when a service's open file descriptor count crosses a threshold:

#!/usr/bin/env bash
# fd-watchdog.sh — monitor open FDs for a given process

SERVICE="myapp"
THRESHOLD=10000          # alert above this count
CRITICAL=50000           # log CRITICAL (near ulimit)
PID=$(pgrep -f "$SERVICE" | head -1)

if [[ -z "$PID" ]]; then
  echo "WARN: no process matching '$SERVICE' found" >&2
  exit 1
fi

FD_COUNT=$(ls /proc/"$PID"/fd 2>/dev/null | wc -l)
ULIMIT=$(cat /proc/"$PID"/limits | grep 'open files' | awk '{print $4}')

echo "$(date -u +%FT%TZ) pid=$PID fds=$FD_COUNT limit=$ULIMIT"

if (( FD_COUNT > CRITICAL )); then
  logger -t fd-watchdog -p user.err \
    "CRITICAL: PID $PID has $FD_COUNT FDs (limit $ULIMIT)"
  # Uncomment for auto-restart:
  # systemctl restart "$SERVICE"
elif (( FD_COUNT > THRESHOLD )); then
  logger -t fd-watchdog -p user.warning \
    "WARNING: PID $PID has $FD_COUNT FDs (limit $ULIMIT)"
fi

Drop it in /usr/local/bin/fd-watchdog.sh, make it executable, and run it via a systemd timer every 60 seconds:

# /etc/systemd/system/fd-watchdog.timer
[Unit]
Description=Run FD watchdog every 60s

[Timer]
OnBootSec=30s
OnUnitActiveSec=60s

[Install]
WantedBy=timers.target
# /etc/systemd/system/fd-watchdog.service
[Unit]
Description=File descriptor watchdog

[Service]
Type=oneshot
ExecStart=/usr/local/bin/fd-watchdog.sh
systemctl daemon-reload
systemctl enable --now fd-watchdog.timer

Why it works

Every open socket, pipe, log file, or database connection consumes a file descriptor. When application code opens resources without closing them (missing close(), unclosed HTTP connections, leaked temp files), the kernel's per-process FD table grows monotonically. /proc/<PID>/fd is a directory of symlinks — one entry per open FD — so counting its entries gives you an exact, instantaneous read of how many descriptors the process holds. By sampling this value on a timer and comparing against the process's own RLIMIT_NOFILE (read from /proc/<PID>/limits), you get a leading indicator that fires minutes to hours before the service actually fails.

Pro Tip

To identify what kind of FDs are leaking (sockets vs. files vs. pipes), run this against the suspect PID — it groups by type and shows the top 10 offenders:

lsof -p <PID> 2>/dev/null | awk '{print $5}' | sort | uniq -c | sort -rn | head -10

If you see thousands of IPv4 or IPv6 entries, the leak is likely unclosed HTTP connections or database connections. If it's REG (regular files), look for temp files or log handles. If it's FIFO or PIPE, suspect subprocesses that weren't reaped.