Skip to content

fix(upload): stop Bunny uploads from silently restarting from zero - #159

Merged
yusufipk merged 2 commits into
masterfrom
fix/bunny-chunked-upload
Dec 4, 2026
Merged

yusufipk merged 2 commits into
masterfrom
fix/bunny-chunked-upload

Conversation

@yusufipk

@yusufipk yusufipk commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Large Bunny uploads could reach a high percentage and then start over from 0% with no error, and a short Wi-Fi drop failed them outright.

What was tested. I ran live tests against a Bunny Stream test library using headless Chromium, the shipped tus-js-client 4.3.1 and this PR's lib/client/bunny-tus.ts. A CONNECT proxy cut the connection, and a Playwright route injected a 502. The test file was a 325 MiB 1080p video.

Scenario Before After
Connection reset at 60% resumed at 58.5% resumed at 58%
Reset at 60%, then Bunny answers the resume HEAD with 502 POST 201, restarted at 0% HEAD retried, resumed at 58%
10 s offline at 50% failed, no retry (seen with the PR's first commit) retried, completed
Reset after the last byte 423, then HEAD 200 at 100% same (3 runs)
Clean upload 73 s 53 s (one run each)

Bunny encoded the chunked, resumed uploads at 1080p. All test videos were deleted afterwards.

Why.

  • After an error, tus HEADs the upload to find the offset. On any answer other than 2xx or 423, _resumeUpload drops the URL and POSTs a new upload to endpoint. Bunny accepts a second upload for the same video, even one that is already encoded, so a passing 5xx makes tus quietly send the whole file again. I have not caught Bunny returning such a HEAD in production. The test injected it, so that link is still an assumption.
  • tus's default onShouldRetry gives up whenever navigator.onLine is false.
  • In one earlier run, the browser resent the final PATCH on its own after the connection dropped. Bunny had already finished the upload, answered 404, and tus failed an upload that was complete. Later runs went through 423 instead, so this depends on timing. Its fix is covered by a unit test only.

Change (lib/client/bunny-tus.ts, wired into all three browser call sites):

  • pinToCreatedUpload nulls the endpoint once the upload URL exists. A failed resume HEAD is then retried against the same upload (5xx) or reported (4xx). It never re-sends the file into a new upload.
  • A custom onShouldRetry retries everything except 4xx. 409, 423 and a 404 on PATCH stay retryable, and it keeps retrying while offline. The retry delays still cap attempts at 7 per stall, about 2.5 minutes.
  • 50 MiB chunks and Bunny's documented retry delays. Chunking costs no throughput against Bunny.
  • Signed-in uploads share one 6h lifetime for the TUS signature, the upload grant and the quota hold, which were 1h, 1h and 2h before. Bunny checks AuthorizationExpire on every HEAD and PATCH, so a retry past the hour failed. Guest uploads keep a 1h token and a 30 min hold. An upload abandoned without the client's DELETE now holds its quota for up to 6h instead of 2h. Expired holds are already ignored by the quota sums.

Validation. bun run check passes. The new tests/unit/lib/client/bunny-tus.test.ts passes, and so do the token and version hook tests.

tus-js-client defaulted to one PATCH for the whole file, and Bunny resumes
only from the last chunk it acknowledged, so any dropped connection in a
multi-GB upload, even with the bar at 100%, restarted it from zero. All three
browser call sites now share BUNNY_TUS_OPTIONS with 50 MiB chunks and Bunny's
documented retry delays.

The TUS signature, the signed upload grant and the quota hold were 1h, 1h and
2h. Bunny checks AuthorizationExpire on every HEAD and PATCH, so a retry past
the hour failed with a 400, and an upload that finished past the hour was
refused when saved. Signed-in uploads now get one shared 6h lifetime; guest
uploads keep their 1h token and 30 min hold.
After a dropped connection tus-js-client asks Bunny how far the upload got.
Any answer but 2xx or 423, a passing 502 included, made it create a new
upload and send the whole file again, and Bunny accepts a second upload for
the same video, so the bar fell back to 0% with no error. Reproduced against
a real Bunny library: HEAD 502, then POST 201 and the upload restarted at 0%.
pinToCreatedUpload drops the endpoint once the upload URL exists, so that
answer is retried against the same upload instead.

The default retry rule also gave up while navigator.onLine was false, so ten
seconds offline failed the upload outright. And when the connection dropped
after the last byte, the browser could resend the final PATCH after Bunny had
already finished, get a 404 and fail an upload that was complete. Both are now
retried; the retry's HEAD resumes, or ends the upload as a success.
@yusufipk yusufipk changed the title fix(upload): send Bunny uploads in chunks and outlive slow uploads fix(upload): stop Bunny uploads from silently restarting from zero Dec 4, 2026
@yusufipk
yusufipk merged commit b276b22 into master Dec 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant