The generated tempfile is sometimes deleted before uploading

Recent refactoring (805e857) can lead to situations where the Tempfile is garbage collected and thus the file deleted before uploading to the VM. We have to make sure that the Tempfile stays in the scope long enough.