Fix leaking FD

See #1289