Create the tmp file ahead of time. Is this causing the trouble?