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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove corrupt cache files and retry download 3 times #8806

Merged
merged 4 commits into from Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 23 additions & 14 deletions conans/client/downloaders/cached_file_downloader.py
Expand Up @@ -7,7 +7,8 @@

from conans.client.downloaders.file_downloader import check_checksum
from conans.errors import ConanException
from conans.util.files import mkdir, set_dirty, clean_dirty, is_dirty
from conans.util.log import logger
from conans.util.files import mkdir, set_dirty, clean_dirty, is_dirty, remove
from conans.util.locks import SimpleLock
from conans.util.sha import sha256 as sha256_sum

Expand Down Expand Up @@ -47,19 +48,27 @@ def download(self, url, file_path=None, md5=None, sha1=None, sha256=None, **kwar
if os.path.exists(cached_path):
os.remove(cached_path)
clean_dirty(cached_path)
if not os.path.exists(cached_path):
set_dirty(cached_path)
self._file_downloader.download(url=url, file_path=cached_path, md5=md5,
sha1=sha1, sha256=sha256, **kwargs)
clean_dirty(cached_path)
else:
# specific check for corrupted cached files, will raise, but do nothing more
# user can report it or "rm -rf cache_folder/path/to/file"
try:
check_checksum(cached_path, md5, sha1, sha256)
except ConanException as e:
raise ConanException("%s\nCached downloaded file corrupted: %s"
% (str(e), cached_path))

for attempt in range(3):
if not os.path.exists(cached_path):
set_dirty(cached_path)
self._file_downloader.download(url=url, file_path=cached_path, md5=md5,
sha1=sha1, sha256=sha256, **kwargs)
clean_dirty(cached_path)
blackliner marked this conversation as resolved.
Show resolved Hide resolved
else:
# specific check for corrupted cached files, will log an error
# and raise after the third failed download attempt
try:
check_checksum(cached_path, md5, sha1, sha256)
break
except ConanException as e:
if attempt == 2:
raise ConanException("%s\nCached downloaded file corrupted: %s"
% (str(e), cached_path))
else:
logger.error("Cached file corrupt, redownloading")
remove(cached_path)


if file_path is not None:
file_path = os.path.abspath(file_path)
Expand Down
5 changes: 2 additions & 3 deletions conans/test/integration/cache/download_cache_test.py
Expand Up @@ -93,9 +93,8 @@ def package(self):
continue
save(f, load(f) + "a")
client.run("remove * -f")
client.run("install mypkg/0.1@user/testing", assert_error=True)
self.assertIn("ERROR: md5 signature failed", client.out)
self.assertIn("Cached downloaded file corrupted", client.out)
client.run("install mypkg/0.1@user/testing")
self.assertIn("mypkg/0.1@user/testing: Downloaded package", client.out)

@pytest.mark.skipif(not get_env("TESTING_REVISIONS_ENABLED", False), reason="Only revisions")
def test_dirty_download(self):
Expand Down