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

Windows: file operations don't always succeed as expected

Abierto
#114 4 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
JavaScript
Estrellas
214
Forks
12
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

### 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.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

Mire travis_build_script.py para la función try_remove_tree y los scripts de instalación que realizan operaciones de archivos. Comprenda las condiciones de carrera específicas de Windows con shutil.rmtree y os.remove. Pruebe el mecanismo de sondeo y considere aplicarlo a otras operaciones de archivos en la base de código.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
cli, tooling
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.