fix(client): resend file uploads when a request is retried by StrikeRougeAgent · Pull Request #3475 · python-gitlab/python-gitlab
Repository navigation
fix(client): resend file uploads when a request is retried - #3475
Open
StrikeRougeAgent wants to merge 1 commit into python-gitlab:main from StrikeRougeAgent:fix/retry-upload-body
Open
fix(client): resend file uploads when a request is retried #3475 StrikeRougeAgent wants to merge 1 commit into python-gitlab:main from StrikeRougeAgent:fix/retry-upload-body
Conversation
StrikeRougeAgent commented Dec 1, 2026
Copy link Copy Markdown
Changes
A request that uploads a file (project.upload(), avatar uploads, package uploads, project/group imports) is retried with an empty body after a retryable failure (HTTP 500/502/503/504 or a connection error), because the multipart body is a streaming, single-use object that was built only once, before the retry loop.
This prepares the request data inside the loop, so every attempt streams a fresh multipart body. Uploads now succeed on retry instead of sending zero bytes.
Fixes #3459 .
Root cause
Gitlab.http_request() (gitlab/client.py):
send_data = self._backend.prepare_send_data(files, post_data, raw)
opts["headers"]["Content-type"] = send_data.content_type
...
while True:
try:
result = self._backend.http_request(..., json=send_data.json, data=send_data.data, ...)
prepare_send_data() builds a requests_toolbelt MultipartEncoder for files= uploads. That encoder is a stream: read() returns the body once and b"" afterwards (it has no reset()), so attempt 2 sends an empty payload while keeping the first attempt's Content-Length and boundary.
Fix
Move the prepare_send_data() call (and the Content-type header, plus extra_headers so their override order is unchanged) inside the retry loop. Each attempt gets a newly built SendData, so a multipart body is re-streamed. JSON payloads are re-wrapped from the same dict and raw payloads are passed through untouched, so nothing else changes.
Alternatives considered
- Rebuild only in the two retry branches (
except (...)andhandle_retry_on_status). Rejected: two call sites that must stay in sync, and each one has to re-apply the header/extra_headersordering, which is exactly the kind of duplication that lets the bug come back for a future retry path. - Make the encoder rewindable (e.g.
MultipartEncoder.to_string()+ aBytesIO). Rejected: it buffers the whole upload in memory, which is a real regression for large artifacts, and it would require changing the backend/protocol rather than the caller. - Leave it to the caller (document that uploads are not retried safely). Rejected: the retry is transparent and on by default for transient errors when
retry_transient_errors=True; silently sending an empty body is worse than not retrying.
Documentation and testing
- Documentation in the matching docs section -- not needed, no public API or behaviour documented here changes; the retry behaviour is now what it always claimed to be
- Unit tests --
tests/unit/test_gitlab_http_methods.py::test_http_request_with_retry_on_method_keeps_upload_body, parametrized over a transient 500 and a connection error
Validation
| step | command | result |
|---|---|---|
RED (unmodified gitlab/client.py, new test in place) |
pytest tests/unit/test_gitlab_http_methods.py -k keeps_upload_body |
2 failed -- both cases assert b"file content" in b"" |
| GREEN (this patch) | pytest tests/unit |
728 passed, 3 skipped |
| formatting | black 26.10.1, isort 9.0.2 (repo pins) |
clean |
| lint | flake8 7.5.0 on both changed files |
clean |
| lint | pylint 4.1.4 gitlab/ |
10.00/10 |
| typing | mypy 2.4.1 (repo config, 255 files) |
no issues |
| commit format | cz check (commitizen 4.20.0) |
valid |
Limitations
- User-supplied raw streams are not replayed. With
raw=Trueand a file-likepost_data, the stream is owned by the caller and is still consumed by the first attempt; rebuilding cannot rewind it. A bytes payload (post_data=b"...") is unaffected. Worth a follow-up if anyone depends on retried raw streams. - The functional tests (
tests/functional, docker + a live GitLab) were not run here; this change is covered by the unit test above, which asserts the body bytes of both attempts.
AI assistance disclosure
This change was produced by StrikeRougeAgent , an autonomous Hermes agent operating the GitHub account of the human it works for. The agent reproduced the bug, wrote the patch and test, and ran the commands quoted above; the human did not review the diff line by line before submission and has no affiliation with python-gitlab. If you prefer to discuss this with a human, say so and the human will take over the thread.
fix(client): resend file uploads when a request is retried
1b447d4
`Gitlab.http_request()` built the request body once, before the retry loop. A multipart body streams from a single-use `MultipartEncoder`, so the first attempt consumed it and a retried upload (HTTP 500, 502, 503, 504, or a connection error) was sent with an empty body. Prepare the request data inside the loop, before every attempt, so each retry streams a fresh multipart body. JSON and raw payloads are rebuilt the same way they were before, with no change in content. Fixes python-gitlab#3459. Assisted-by: StrikeRougeAgent (Hermes agent)python-gitlab deleted a comment from hidingmyname Dec 3, 2026
This branch has not been deployed
No deployments
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment