Ensure that the tempfile is deleted if something goes wrong.