r/coolgithubprojects • u/sayboom • 7h ago
openfd β what's holding this mount or port, and how long until the process hits EMFILE
github.comhttps://github.com/saromanov/openfd
Three things kept annoying me: umount: target is busy, address already in use, and "I deleted the log but the disk is still full". In all three lsof gives you a pid and leaves the rest to you. On big boxes it can also take minutes and eat memory (Red Hat KB has a writeup about it OOMing).
So I wrote openfd. The subcommand I use most is why:
$ openfd why /mnt/data
/mnt/data (ext4, /dev/sdb1) is busy: 3 processes, 4 references
OWNER PID COMMAND HOW TARGET
nginx.service 812 nginx fd 7 /mnt/data/logs/access.log
user@1000/session-3 4410 bash cwd /mnt/data/project
docker:3f2a9c 9921 app mmap /mnt/data/lib/libfoo.so
fix:
systemctl restart nginx.service
cd out of /mnt/data/project
docker restart 3f2a9cIt checks fd, cwd, exe, mmap, locks and nested mounts, and works out which systemd unit, container or pod the pid belongs to. The hints are printed, not executed.
There's also leak: it watches fd growth over time, fits a least-squares line, gives an ETA to EMFILE, and groups what's growing β e.g. "+171 tcp β 10.0.0.5 CLOSE-WAIT" β with a guess at the cause. And --deleted, which sums the bytes held by unlinked-but-open files.
Scans stream, so memory doesn't scale with the number of fds. At 100k fds: openfd 0.71 s / 3 MiB, lsof 2.0 s / 42 MiB, lsfd 2.8 s / 140 MiB (hyperfine, same machine; methodology in BENCHMARKS.md, re-run in CI on every PR).
It's not a drop-in lsof replacement and doesn't try to be. It covers the common stuff; missing flags fail with an error instead of pretending. Linux only. Without root you only see your own processes.
Rust, no unsafe in the core, two setpriority calls in the CLI for --nice. Static binary, no network access.
cargo install openfd, brew install saromanov/tap/openfd, or binaries from Releases. MIT/Apache-2.0.
Curious which lsof invocation you'd miss. There's also a small good first issue if anyone wants to contribute: https://github.com/saromanov/openfd/issues/17