Skip to content

Replace shutil.move with shutil.copy2, to avoid a permission error on Windows#11280

Merged
ptormene merged 3 commits intomasterfrom
safemove
Mar 11, 2026
Merged

Replace shutil.move with shutil.copy2, to avoid a permission error on Windows#11280
ptormene merged 3 commits intomasterfrom
safemove

Conversation

@ptormene
Copy link
Member

@ptormene ptormene commented Mar 6, 2026

This should prevent errors like:

Traceback (most recent call last):
  File "C:\Users\iklab\AppData\Roaming\Python\Python311\site-packages\openquake\server\views.py", line 1483, in submit_job
    job_ini = store(request_files, ini, job.calc_id)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\iklab\AppData\Roaming\Python\Python311\site-packages\openquake\server\views.py", line 224, in store
    shutil.move(input_file.temporary_file_path(), new_path)
  File "C:\Users\iklab\AppData\Local\Programs\OpenQuake Engine\python3\Lib\shutil.py", line 846, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\iklab\\AppData\\Local\\Temp\\_o1kbw_5.upload'

We checked with @vot4anto on a Windows machine that this actually fixes the issue.

Changing shutil.move to shutil.copy2, Django deletes the temporary file when the request ends, instead of attempting to delete it immediately when it is still in use by the Django process.

@ptormene ptormene added this to the Engine 3.26.0 milestone Mar 6, 2026
@ptormene ptormene requested review from micheles and vot4anto March 6, 2026 09:10
@ptormene ptormene self-assigned this Mar 6, 2026
@ptormene ptormene added the bug label Mar 6, 2026
@ptormene ptormene marked this pull request as ready for review March 11, 2026 15:01
Copy link
Contributor

@vot4anto vot4anto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ptormene ptormene merged commit 76d879d into master Mar 11, 2026
@ptormene ptormene deleted the safemove branch March 11, 2026 15:34
@ptormene ptormene changed the title Replace shutil.move with a safe_move utility that on Windows should wait until the file handler is released Replace shutil.move with shutil.copy2, to avoid a permission error on Windows Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants