Fix file descriptor leak