Repository navigation
[ci] Pass --skip-existing when uploading wheels to PyPI/TestPyPI - #67254
Conversation
upload_wheels_to_pypi() runs one twine call per file in sorted() order with check=True, so a single rejected file aborts the loop and strands every wheel after it. A retry then fails on the first file it already uploaded, so the upload can never complete without manual intervention. The 2.58.1 release hit this: the TestPyPI upload was rejected partway through (project size limit), leaving 9 of 24 wheels unpublished -- all of cp314, cp313-win and every ray_cpp wheel, since 'ray_cpp' sorts after 'ray-'. Retrying the job 400'd on the first file every time, and the wheels had to be uploaded by hand. --skip-existing makes the step idempotent: already-published files are skipped instead of failing, so a partial upload is fixed by re-running the job. Note it only covers duplicate-file rejections; other 4xx responses still fail the step, which is the intended behavior. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: elliot-barn <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request adds the --skip-existing flag to the twine upload command in pypi_lib.py and updates the corresponding test to prevent upload failures when a wheel already exists on PyPI. The reviewer suggests a further improvement to upload all wheels in a single twine invocation rather than individually, which would reduce process startup and connection overhead.
| # A file that is already on the index must not fail the whole | ||
| # upload: twine uploads one file per call and `check=True` aborts | ||
| # the loop, so a single rejected file strands every wheel after it | ||
| # in sort order and a retry then dies on the first file it already | ||
| # uploaded. This was hit on the 2.58.1 release, where 9 of 24 | ||
| # wheels never reached TestPyPI and the retry could not recover. | ||
| "--skip-existing", |
There was a problem hiding this comment.
Efficiency & Reliability Improvement: Upload all wheels in a single twine invocation
Currently, the script loops over each wheel and invokes twine upload individually. This has a few drawbacks:
- Process Startup Overhead: Spawning a new Python subprocess for each of the 24+ wheels adds significant overhead (around 10-20 seconds total).
- Connection Overhead: Each individual
twineinvocation must establish a new TLS connection to PyPI. - Connection Reuse: By passing all wheel paths to a single
twine uploadcommand,twinecan reuse a singlerequests.Session(with HTTP keep-alive), making the upload process much faster and more resilient to transient network/handshake failures.
Since we are already passing --skip-existing, a single invocation with all files will behave identically on retries (skipping already uploaded files).
Here is how the refactored upload_wheels_to_pypi function and its test could look:
def upload_wheels_to_pypi(pypi_env: str, directory_path: str) -> None:
directory_path = os.path.join(bazel_workspace_dir, directory_path)
pypi_url = _get_pypi_url(pypi_env)
pypi_token = _get_pypi_token(pypi_env)
wheels = sorted(os.listdir(directory_path))
if not wheels:
return
wheel_paths = [os.path.join(directory_path, wheel) for wheel in wheels]
cmd = [
sys.executable,
"-m",
"twine",
"upload",
"--skip-existing",
"--repository-url",
pypi_url,
"--username",
"__token__",
] + wheel_paths
_call_subprocess(cmd, add_env={"TWINE_PASSWORD": pypi_token})And the corresponding test update:
def test_upload_wheels_to_pypi(mock_subprocess, mock_get_pypi_url, mock_get_pypi_token):
...
upload_wheels_to_pypi(pypi_env, tmp_dir)
mock_get_pypi_token.assert_called_once_with(pypi_env)
mock_get_pypi_url.assert_called_once_with(pypi_env)
mock_subprocess.assert_called_once()
call_args = mock_subprocess.call_args
command = call_args[0][0]
assert command[:-len(wheels)] == [
sys.executable,
"-m",
"twine",
"upload",
"--skip-existing",
"--repository-url",
"test_pypi_url",
"--username",
"__token__",
]
assert command[-len(wheels):] == wheel_paths…to PyPI/TestPyPI (#67254) (#67319) Cherry-pick of #67254 (master commit 8bbfe80) onto `releases/2.60.0`. ## Why `upload_wheels_to_pypi()` in `ci/ray_ci/automation/pypi_lib.py` runs `twine upload` once per file with `check=True`. One rejected file aborts the rest of the upload. Every retry then fails on the first file, which is already on the index. This stranded 9 of 24 wheels in the 2.58.1 TestPyPI upload. #67254 adds `--skip-existing`, so a re-run skips the files already uploaded and finishes the rest. ## Cherry-pick details Applied cleanly with `git cherry-pick -x`. The change is identical to the master commit: the same 2 files, 8 insertions, and the same `git patch-id --stable`. No prerequisite commits are missing. Both files on this branch were identical to #67254's parent, so after the pick they match master at 8bbfe80. Not a duplicate: this branch doesn't contain #67254, and no PR against `releases/2.60.0` references it. ## Testing | Command | Result | |---|---| | `pre-commit run --files ci/ray_ci/automation/pypi_lib.py ci/ray_ci/automation/test_pypi_lib.py` | Passed (ruff, black, docstyle, import order, and the other applicable hooks) | I didn't run `//ci/ray_ci/automation:test_pypi_lib` locally. It's tagged `ci_unit`, so CI's `:coral: reef: ci+release tooling tests` job runs it on this PR. AI assistance was used to prepare this cherry-pick. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: elliot-barn <[email protected]> Signed-off-by: Sai Miduthuri <[email protected]> Co-authored-by: Elliot Barnwell <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
Why
upload_wheels_to_pypi()shells out totwine uploadonce per file, insorted()order, withcheck=True. A single rejected file therefore aborts the loop and strands every wheel after it — and because a retry starts over at file #1, which it already uploaded successfully, the retry fails immediately too. The step cannot recover on its own.This bit the 2.58.1 release. The TestPyPI upload was rejected partway through (the project had hit its size limit), leaving 9 of 24 wheels unpublished: all four
cp314wheels,cp313-win_amd64, and all fourray_cppwheels (ray_cppsorts afterray-, so none of them were attempted). Every retry of the job 400'd on the first file, and the missing wheels had to be uploaded manually.What
Adds
--skip-existingto the twine invocation, making the step idempotent: files already on the index are skipped rather than failing the run, so a partial upload is repaired by simply re-running the job.This only affects duplicate file rejections — twine still fails the step on other errors (bad auth, size limits, malformed metadata), which is the behavior we want.
Tests
ci/ray_ci/automation/test_pypi_lib.pyasserts the exact twine argv; updated accordingly. All 9 tests pass locally.🤖 Generated with Claude Code