07th-mod / 07th-mod/python-patcher

Windows: file operations don't always succeed as expected

未关闭
#114 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
214
派生
12
PR 合并指标
30 天内没有已合并 PR

描述

### Problems

- shutil.rmtree or os.remove does not always ensure that files are fully removed after function returns
- remove may take multiple attempts to succeed
- mkdir(path, exist_ok=True) might have a race condition if it is called just after the folder is "removed"

### Related Issues
- https://github.com/07th-mod/python-patcher/issues/33#issuecomment-489390131 "07th Mod - Install failed due to error: [WinError 145] The directory is not empty"

### Description

I noticed that sometimes in `travis_build_script.py`, try_remove_tree() didn't always remove the file immediately (the file would exist for some time after the function was called. The function used to be defined like this:

```python
def try_remove_tree(path):
try:
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
except FileNotFoundError:
pass
```

but I changed it to this

```python
def try_remove_tree(path):
try:
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
except FileNotFoundError:
pass

# Make sure the folder is really removed
poll_cnt = 0
while os.path.exists(path):
print(f"Waiting for {path} to be removed ({poll_cnt})")
if poll_cnt < 5:
time.sleep(1)
else:
raise Exception(f"Folder {path} could not be removed!")

poll_cnt += 1
```

The `# Make sure the folder is really removed` did trigger on my machine (at least once), and after 1 second the file was "properly" removed.

If this behavior can happen on windows, we should also update the installer scripts, which use the same functions, to add this kind of functionality.

The installer scripts currently don't do multiple attempts when doing operations like extraction, deletion etc, so it may be worth checking for those sort of issues as well.

贡献指南

这个仓库没有索引到贡献指南

调研方向

查看 travis_build_script.py 中的 try_remove_tree 函数以及执行文件操作的安装脚本。理解 shutil.rmtree 和 os.remove 在 Windows 上特有的竞态条件。测试轮询机制,并考虑将其应用于代码库中的其他文件操作。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cli, tooling
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。