bug with os.rename in 2.4.1?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Apr 29 05:00:34 CEST 2009


On Tue, 28 Apr 2009 14:30:02 -0500, Nick Craig-Wood wrote:

> t123 <tom.lukes at gmail.com> wrote:
>>  It's running on solaris 9.  Here is some of the code.  It's actually
>>  at the beginning of the job.  The files are ftp'd over.  The first
>>  thing that happens is that the files get renamed before any processing
>>  of the file.  And when it fails, it always fails at the first file,
>>  comm.dat.  What I can't understand is why the inconsistent behavior.
>> 
>>  try:
>>          if os.path.exists(paths.xferin_dir+'/COMM.DAT'):
>>            os.rename(paths.xferin_dir+'/COMM.DAT',paths.xferin_dir+'/
> 
> This code is inherently racy... What if two copies of your code started
> simultaneously?  They might both run the os.path.exists but only one
> will succeed in the os.rename.
> 
> You could write instead
> 
>     try:
>         os.rename(paths.xferin_dir+'/COMM.DAT',paths.xferin_dir+'/
COMM.DAT'+'.0')
>     except OSError:
>         pass
> 
> Which isn't racy.


The race condition is still there. The only difference is that in the 
first case it fails noisily, with an exception, and in the second it 
fails quietly and does nothing.



-- 
Steven



More information about the Python-list mailing list