python / python/cpython

shutil's move function fails to handle files opened by another process

未關閉
#120,882 0 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

OS-windows type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

Problem:

When attempting to move a file using shutil.move from one location to another, if the file is opened by another process (if file is opened in other python script using open function), shutil.move raises an error (WinError 32: The process cannot access the file because it is being used by another process). Despite this error, the file is still copied to the destination directory, which is unexpected behavior.

Details:

The shutil.move function is designed to handle moving files or directories between locations. However, it does not check if the file is currently open by another process before attempting to move it. This results in an error being raised, but the file is still copied to destination regardless of the error condition.

Solution:

To address this issue, I modified the shutil.move function to include an additional check for OSError with errno.EACCES (Permission Denied) in the exception handling block. This ensures that if the file cannot be moved due to being opened by another process, the operation is aborted cleanly without copying the file to maintain the integrity of the move function as well as file system.

Here is the modified part of the shutil.move function

    except OSError as exc:
        if exc.errno == errno.EACCES:
            raise  # Propagate the PermissionError
     # Existing code

Proposal:

I propose adding this error handling improvement to the shutil.move function in Python's standard library. This enhancement will make file operations more reliable in scenarios where files may be concurrently accessed by multiple processes

Steps to Reproduce:

  • Open a file (test.pdf) in any python script using open function in read mode.
  • Attempt to move the file using shutil.move to another directory.
  • Observe the WinError 32 exception and incorrect copying behavior.
  • Close the file (To avoid memory leakage).

NOTE: Avoid to use context manager here for opening file to find the error.
You can also run the following script to get the error.

import shutil

src = "C://Users//Desktop//Downloads//test.pdf" # Provide full file path of your system.
dst_dir = "D://Files" # Provide destination directory path.
f = open(src, "r")
try:
    shutil.move(src, dst_dir)
except Exception as e:
    print(e)
finally:
    f.close()

Expected Behavior:

When shutil.move encounters a situation where the source file is open in another process (WinError 32), it should raise an exception and abort the operation without performing any partial file copying to the destination.

Impact:

This issue affects users trying to use shutil.move to relocate files that are concurrently accessed by other processes. The proposed solution aims to improve the robustness and expected behavior of the function in such scenarios.

CPython versions tested on:

3.10

Operating systems tested on:

Windows

Linked PRs
  • gh-120883

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 shutil.move 入口開始,使用被另一個程序保持開啟狀態的來源檔案重現回報的 Windows 行為。確認該操作會引發預期的例外,且不會留下複製出的目的地檔案,並在開始之前檢查相關聯的 PR gh-120883,因為該 issue 已經有相關工作。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
operating-systems
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。