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

upload: add connection exceptions to upload retry protocol #1031

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 43 additions & 0 deletions tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,49 @@ def test_upload_retry(tmpdir, default_repo, caplog):
]


def test_upload_retry_on_connection_error(tmpdir, default_repo, caplog):
default_repo.disable_progress_bar = True

default_repo.session = pretend.stub(
post=pretend.raiser(requests.exceptions.ConnectionError)
)

fakefile = tmpdir.join("fake.whl")
fakefile.write(".")

package = pretend.stub(
safe_name="fake",
metadata=pretend.stub(version="2.12.0"),
basefilename="fake.whl",
filename=str(fakefile),
metadata_dictionary=lambda: {"name": "fake"},
)

# Upload with default max_redirects of 5
default_repo.upload(package)

assert caplog.messages == [
(
'ConnectionError raised.\n'
f"Package upload appears to have failed. Retry {i} of 5."
)
for i in range(1, 6)
]

caplog.clear()

# Upload with custom max_redirects of 3
default_repo.upload(package, 3)

assert caplog.messages == [
(
'ConnectionError raised.\n'
f"Package upload appears to have failed. Retry {i} of 3."
)
for i in range(1, 4)
]


@pytest.mark.parametrize(
"package_meta,repository_url,release_urls",
[
Expand Down
28 changes: 19 additions & 9 deletions twine/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,31 @@ def upload(
) -> requests.Response:
number_of_redirects = 0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I rename to retries? redirects aren't really handled here.

while number_of_redirects < max_redirects:
resp = self._upload(package)

if resp.status_code == requests.codes.OK:
return resp
if 500 <= resp.status_code < 600:
try:
resp = self._upload(package)
except (
requests.exceptions.ConnectionError,
requests.exceptions.ConnectTimeout,
) as exc:
number_of_redirects += 1
logger.warning(
f'Received "{resp.status_code}: {resp.reason}"'
f"{exc.__class__.__name__} raised."
"\nPackage upload appears to have failed."
f" Retry {number_of_redirects} of {max_redirects}."
)
continue
else:
return resp

return resp
if resp.status_code == requests.codes.OK:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slighlty redundant, but can leave for clarity. Should add a continue to the retry if clause.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, as the _upload function calls on POST without redirects, we aren't really handling anywhere (they are simply returned)

return resp
if 500 <= resp.status_code < 600:
number_of_redirects += 1
logger.warning(
f'Received "{resp.status_code}: {resp.reason}"'
"\nPackage upload appears to have failed."
f" Retry {number_of_redirects} of {max_redirects}."
)
else:
return resp

def package_is_uploaded(
self, package: package_file.PackageFile, bypass_cache: bool = False
Expand Down