Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with deleting temporary folders on Windows #460

Merged
merged 18 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/poetry/core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import stat
import tempfile
import time
import unicodedata
import warnings

Expand Down Expand Up @@ -42,7 +43,7 @@ def normalize_version(version: str) -> str:
def temporary_directory(*args: Any, **kwargs: Any) -> Iterator[str]:
name = tempfile.mkdtemp(*args, **kwargs)
yield name
safe_rmtree(name)
robust_rmtree(name)
eblis marked this conversation as resolved.
Show resolved Hide resolved


def parse_requires(requires: str) -> list[str]:
Expand Down Expand Up @@ -90,10 +91,29 @@ def _on_rm_error(func: Any, path: str | Path, exc_info: Any) -> None:
func(path)


def safe_rmtree(path: str | Path) -> None:
if Path(path).is_symlink():
return os.unlink(str(path))

def robust_rmtree(path: str, max_timeout: float = 1) -> None:
eblis marked this conversation as resolved.
Show resolved Hide resolved
"""
Robustly tries to delete paths.
Retries several times if an OSError occurs.
If the final attempt fails, the Exception is propagated
to the caller.
"""
timeout = 0.001
while timeout < max_timeout:
try:
# both os.unlink and shutil.rmtree can throw exceptions on Windows
# if the files are in use when called
if Path(path).is_symlink():
os.unlink(str(path))
eblis marked this conversation as resolved.
Show resolved Hide resolved
else:
shutil.rmtree(path)
return # Only hits this on success
except OSError:
# Increase the timeout and try again
time.sleep(timeout)
timeout *= 2

# Final attempt, pass any Exceptions up to caller.
shutil.rmtree(path, onerror=_on_rm_error)


Expand Down
32 changes: 32 additions & 0 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from __future__ import annotations

import os
import tempfile

from pathlib import Path
from stat import S_IREAD
from typing import TYPE_CHECKING

import pytest


if TYPE_CHECKING:
from pytest_mock import MockerFixture

from poetry.core.utils.helpers import combine_unicode
from poetry.core.utils.helpers import parse_requires
from poetry.core.utils.helpers import readme_content_type
from poetry.core.utils.helpers import robust_rmtree
from poetry.core.utils.helpers import temporary_directory


Expand Down Expand Up @@ -118,3 +125,28 @@ def test_utils_helpers_readme_content_type(
readme: str | Path, content_type: str
) -> None:
assert readme_content_type(readme) == content_type


def test_robust_rmtree(mocker: MockerFixture) -> None:
mocked_rmtree = mocker.patch("shutil.rmtree")

# this should work after an initial exception
name = tempfile.mkdtemp()
mocked_rmtree.side_effect = [
OSError(
"Couldn't delete file yet, waiting for references to clear", "mocked path"
),
None,
]
robust_rmtree(name)

# this should give up after retrying multiple times
name = tempfile.mkdtemp()
eblis marked this conversation as resolved.
Show resolved Hide resolved
mocked_rmtree.side_effect = OSError(
"Couldn't delete file yet, this error won't go away after first attempt"
)
with pytest.raises(OSError):
robust_rmtree(name, max_timeout=0.04)

eblis marked this conversation as resolved.
Show resolved Hide resolved
# clear the side effect (breaks the tear-down otherwise)
mocked_rmtree.side_effect = None
eblis marked this conversation as resolved.
Show resolved Hide resolved