Don't attempt to close a closed IO object

Because the yield runs arbitrary application code it's possible the
underlying file descriptor is closed during the block. This prevents
that.