ensure output file handle is closed.