Fix for OSError: [Errno 9] Bad file descriptor error, freebsd related