fix: retry upload after package registration instead of raising#10801
Merged
radoering merged 2 commits intopython-poetry:mainfrom Apr 3, 2026
Merged
fix: retry upload after package registration instead of raising#10801radoering merged 2 commits intopython-poetry:mainfrom
radoering merged 2 commits intopython-poetry:mainfrom
Conversation
Reviewer's GuideAdds a guarded retry of file upload after successful package registration when encountering specific 400 responses, and introduces a test to verify the upload-register-upload flow. Sequence diagram for guarded retry upload after package registrationsequenceDiagram
actor User
participant Uploader
participant Server
User->>Uploader: publish_package(file)
Uploader->>Server: POST upload_file(file, registered=false)
Server-->>Uploader: 400 Bad Request
alt status 400 and contains was ever registered and registered is false
Uploader->>Server: POST register_package()
Server-->>Uploader: 200 OK
Uploader->>Server: POST upload_file(file, registered=true)
alt second upload succeeds
Server-->>Uploader: 200 OK
Uploader-->>User: publish succeeded
else second upload still 400
Server-->>Uploader: 400 Bad Request
Uploader->>Uploader: resp.raise_for_status()
Uploader-->>User: error raised
end
else status 400 and registered is true
Uploader->>Uploader: resp.raise_for_status()
Uploader-->>User: error raised
else other status handling
Uploader->>Uploader: handle other responses
end
Class diagram for updated uploader retry logicclassDiagram
class Uploader {
+upload_file(file)
-_upload_file(session, url, file, dry_run, skip_existing, registered)
-_register(session, url)
-_is_file_exists_error(resp) bool
}
class Session {
}
class Response {
+status_code int
+text str
+raise_for_status()
}
Uploader --> Session : uses
Uploader --> Response : receives from server
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- The retry logic in
_upload_fileuses recursion with a boolean flag; consider replacing this with an explicit, non-recursive retry loop (e.g., a small retry counter) to make the control flow clearer and easier to extend if additional retry cases are added. - The
registeredflag name and behavior could be clarified (e.g.,has_retried_after_registeror similar) to make it immediately obvious that it is guarding a single retry attempt rather than simply reflecting registration state.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The retry logic in `_upload_file` uses recursion with a boolean flag; consider replacing this with an explicit, non-recursive retry loop (e.g., a small retry counter) to make the control flow clearer and easier to extend if additional retry cases are added.
- The `registered` flag name and behavior could be clarified (e.g., `has_retried_after_register` or similar) to make it immediately obvious that it is guarding a single retry attempt rather than simply reflecting registration state.
## Individual Comments
### Comment 1
<location path="src/poetry/publishing/uploader.py" line_range="260-264" />
<code_context>
- self._register(session, url)
+ if not registered:
+ self._register(session, url)
+ return self._upload_file(
+ session, url, file, dry_run, skip_existing,
+ registered=True,
+ )
</code_context>
<issue_to_address>
**suggestion:** Forwarding all parameters positionally in the recursive call is somewhat brittle.
The retry call currently passes `dry_run` and `skip_existing` positionally. If `_upload_file`’s signature changes (new params or reordered ones), this can silently break behavior. Prefer keyword arguments for these flags and `registered` (e.g. `dry_run=dry_run`, `skip_existing=skip_existing`, `registered=True`) to make the intent explicit and avoid argument ordering issues.
```suggestion
self._register(session, url)
return self._upload_file(
session=session,
url=url,
file=file,
dry_run=dry_run,
skip_existing=skip_existing,
registered=True,
)
```
</issue_to_address>
### Comment 2
<location path="tests/publishing/test_uploader.py" line_range="233-234" />
<code_context>
+) -> None:
+ """After registering a package, the upload must be retried.
+
+ The server returns 400 "was ever registered" on the first upload,
+ then 200 on the registration and the retried upload.
+ """
+ http.post("https://foo.com", status=400, body="No package was ever registered")
</code_context>
<issue_to_address>
**suggestion (testing):** Add a complementary test for repeated 400s after registration to exercise the recursion guard
The current test only covers the case where a 400 leads to registration and then a successful retry. Please add a second test that mocks responses as: 400 (first upload), 200 (register), 400 (retried upload). That test should verify that `upload()` terminates (no further retries/registration attempts) and surfaces the second 400 (e.g., via an exception) instead of attempting to register again.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
3358d38 to
d411d67
Compare
radoering
approved these changes
Apr 2, 2026
After _register() succeeded, resp.raise_for_status() raised on the original 400 response, so the upload was never retried. Now retries the upload once after registration, with a guard against infinite recursion if the server keeps returning 400. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2af8dcf to
cae2409
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After _register() succeeded, resp.raise_for_status() raised on the original 400 response, so the upload was never retried. Now retries the upload once after registration, with a guard against infinite recursion if the server keeps returning 400.
Another from copilot. Borderline bug or enhancement, but the retry surely makes sense.
Summary by Sourcery
Ensure package uploads are retried once after successful registration when the server initially responds with a 400 indicating the package was never registered.
Bug Fixes:
Tests: