ComicCrawler icon indicating copy to clipboard operation
ComicCrawler copied to clipboard

os.replace is not atomic on Windows

Open eight04 opened this issue 8 years ago • 1 comments

https://github.com/eight04/ComicCrawler/blob/7ad079af4c8190837afa505cffe3b98cdce888e0/comiccrawler/io.py#L59-L60 https://docs.python.org/3/library/os.html#os.rename

I think we should change the procedure to:

  1. Rename the old file.
  2. Write new file.
  3. Delete the old file.

So it doesn't depend on os.replace.

eight04 avatar Apr 09 '18 06:04 eight04

Actually, the current strategy is same as other libraries.

_write_atomic from std importlab uses tempfile + os.replace: https://github.com/python/cpython/blob/9e2be60634914f23db2ae5624e4acc9335bf5fea/Lib/importlib/_bootstrap_external.py#L105-L124

os.replace in cpython uses MoveFileExW: https://github.com/python/cpython/blob/48e8c82fc63d2ddcddce8aa637a892839b551619/Modules/posixmodule.c#L4056

_replace_atomic from atomicwrites uses MoveFileExW: https://github.com/untitaker/python-atomicwrites/blob/a01f2ee2be5d86a066422a652705ee9394c472e5/atomicwrites/init.py#L68-L72

Let's see if there are more data loss reports.

eight04 avatar Apr 15 '18 22:04 eight04