Skip to content

Commit fb5e2f0

Browse files
authored
Merge pull request #1824 from dandi/bf-zarr-upload2
BF: Seek file data to 0 before every retry in request(), not just for retryable status codes
2 parents 34af06f + d7d5dd4 commit fb5e2f0

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

dandi/dandiapi.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ def request(
212212

213213
lgr.debug("%s %s", method.upper(), url)
214214

215+
def _rewind_data(retry_state: tenacity.RetryCallState) -> None:
216+
# After a failed attempt (ConnectionError mid-upload, HTTPError,
217+
# etc.), the file pointer may be at an arbitrary position. Seek
218+
# back to 0 so the next attempt sends the complete body.
219+
# See https://github.com/dandi/dandi-cli/issues/1821
220+
if data is not None and hasattr(data, "seek"):
221+
data.seek(0)
222+
215223
try:
216224
for i, attempt in enumerate(
217225
tenacity.Retrying(
@@ -225,6 +233,7 @@ def request(
225233
),
226234
stop=tenacity.stop_after_attempt(REQUEST_RETRIES),
227235
reraise=True,
236+
before_sleep=_rewind_data,
228237
)
229238
):
230239
with attempt:
@@ -249,8 +258,6 @@ def request(
249258
url,
250259
result.text,
251260
)
252-
if data is not None and hasattr(data, "seek"):
253-
data.seek(0)
254261
if retry_after := get_retry_after(result):
255262
lgr.debug(
256263
"Sleeping for %d seconds as instructed in response "

0 commit comments

Comments
 (0)