Fix file descriptor leak

Originally reported by dexX7