SnowOps Night Shift

Why does my systemd service fail with Too many open files when ulimit is high?

Every open file and socket counts against a process's open-file limit, and going over it fails with Errno 24, Too many open files. A service started by systemd gets its limit from LimitNOFILE in its unit or drop-ins, not from your shell, so ulimit -n in a terminal can be high while the service is capped. systemctl show -p LimitNOFILE and /proc/<pid>/limits show the real value. In this incident a hardening drop-in set the limit to 256 while a report opens 300 files.

What it looks like

  • Errors with Errno 24 or Too many open files.
  • The same code works when run by hand.
  • systemctl show reports a low LimitNOFILE.
  • A unit or drop-in changed shortly before.

Commands that find the cause

  1. journalctl -u orders-api -n 6 --no-pager

    The service's log, newest last. Each failed report is one line with the operating system's error and its number.

  2. systemctl show orders-api -p LimitNOFILE

    The open-file limit systemd gives the service. Every file and socket counts against it, and the service cannot raise it from inside.

  3. grep 'open files' /proc/<pid>/limits

    The open-file limit the kernel enforces on the running process, soft and hard. This is what the service really has, whatever any config file says.

  4. ulimit -n

    The open-file limit of your own shell. A service started by systemd does not inherit it, so a high number here says nothing about the service.

  5. ls /opt/orders/data | wc -l

    How many part files the report reads. The report opens them all at once.

What the evidence looked like

Recorded on a real system when this incident was captured for the game.

$ journalctl -u orders-api -n 6 --no-pager
Nov 29 20:29:29 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:29 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'

Root cause

A hardening drop-in set LimitNOFILE=256 while a report opens 300 files; raising it to 4096 fixed reports.

The fix

Raise the open-file limit in the hardening drop-in and restart the service:

sudo sed -i 's/LimitNOFILE=256/LimitNOFILE=4096/' /etc/systemd/system/orders-api.service.d/hardening.conf && sudo systemctl daemon-reload && sudo systemctl restart orders-api

How to prevent it

Set LimitNOFILE from what the service really opens at peak, keep hardening drop-ins under review with the service owners, and alert on open file counts.

More incident guides