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 3 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
24 changes: 23 additions & 1 deletion 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

from contextlib import contextmanager
Expand Down Expand Up @@ -32,7 +33,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 @@ -91,6 +92,27 @@ def safe_rmtree(path: str | Path) -> None:
shutil.rmtree(path, onerror=_on_rm_error)


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:
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.
safe_rmtree(path)
eblis marked this conversation as resolved.
Show resolved Hide resolved


def readme_content_type(path: str | Path) -> str:
suffix = Path(path).suffix
if suffix == ".rst":
Expand Down
32 changes: 32 additions & 0 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from __future__ import annotations

import os
import tempfile

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

import pytest

from poetry.core.utils.helpers import combine_unicode
from poetry.core.utils.helpers import normalize_version
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


if TYPE_CHECKING:
from pytest_mock import MockerFixture


@pytest.mark.parametrize(
"version,normalized_version",
[
Expand Down Expand Up @@ -178,3 +185,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