Prevent file descriptor leaking on some platforms, fixes #20