Permission denied when opening a file that was created concurrently by os.rename (Windows)
izbyshev at ispras.ru
Sun Apr 5 19:45:07 CEST 2015
I've hit a strange problem that I reduced to the following test case:
* Run several python processes in parallel that spin in the following
with open(fname, 'rb') as f:
* Then, run another process that creates a temporary file and then
renames it to the name than other processes are expecting
* Now, some of the reading processes occasionally fail with "Permission
I was able to reproduce it on two Windows 7 64-bit machines. It seems
when the file appears on the filesystem it is still unavailable to
reading, but I have no idea how it can happen. Both source and
destination files are in the same directory, and the destination doesn't
exist before calling os.rename. Everything I could find indicates that
os.rename should be atomic under this conditions even on Windows, so
nobody should be able to observe the destination in unaccessible state.
I know that I can workaround this problem by removing useless
os.path.isfile() check and wrapping open() with try-except, but I'd like
to know the root cause of the problem. Please share you thoughts.
The test case is attached, the main file is test.bat. Python is
expected to be in PATH. Stderr of readers is redirected to *.log. You
may need to run several times to hit the issue.
More information about the Python-list