Skip to content

Attach a large file over a slow connection: upload it in parts - #2971

Open
davidmckayv wants to merge 6 commits into
mainfrom
fix/attachment-upload-parts
Open

davidmckayv wants to merge 6 commits into
mainfrom
fix/attachment-upload-parts

Conversation

@davidmckayv

Copy link
Copy Markdown
Contributor

What and why

On a slow upload link, no file over a few megabytes could be attached. A person on OpenBot Cloud with an upload speed of 15 to 35 KB/s tried 21 times and every try failed. The whole file went up in one request, and the edge in front of OpenBot Cloud closes any request whose body takes more than 125 seconds to arrive. So the composer's "Try again" could never work: at that speed the same request is cut off the same way every time. OpenBot Cloud takes files up to 100 MB, and sending one of those inside 125 s needs about 6.7 Mbps of sustained upload.

The API (api/openapi/conversations.yaml, additions only):

  • createAttachmentUpload, uploadAttachmentPart, completeAttachmentUpload and cancelAttachmentUpload send a file in parts. The server chooses the part size; every part is that size except the last.
  • A missing part is refused under its own code, ATTACHMENT_PARTS_MISSING, with each part listed in problems. A file over the limit for its kind is refused with ATTACHMENT_TOO_LARGE (422).
  • The deployment answer gains attachmentUploadsInParts.
  • uploadConversationAttachment is unchanged, so released apps keep working, and an app uses parts only where the deployment offers them.

The app:

  • Where the deployment offers parts, a file over 1 MB goes up in parts, one part in flight at a time across the tab. Sending parts side by side would split a slow link and push every part past the limit.
  • Every request retries by itself while its failure may pass: no answer, 408, 429, any 5xx (Cloudflare's 520 to 524 among them), or a refusal OpenBot marks retryable. It honours Retry-After, makes up to 8 tries about 1 s doubling to 30 s apart, with jitter, and keeps the same Idempotency-Key on every try.
  • An upload that never gets through is cancelled on the server. Its refusal no longer asks the person to try again.
  • The file tile shows "Uploading…" with the percentage and a thin bar, and a picture's placeholder shows the same bar. Both use the existing Progress primitive and tokens; the tile sizes are unchanged.
  • Part uploads use XMLHttpRequest, through the app's one transport in lib/client.ts, because fetch reports no upload progress in any browser. The cookie, headers, refusals and reachability are the same as with fetch.
  • Both the decision to send in parts and the size screening of a picked file wait for the deployment answer. A file picked before that answer had loaded used to go in one request, or be refused against the 8MB default limit on a server that takes 100MB.

Design choice, and sources

Every leading product splits a large upload into parts with a step that puts them together. That way no single request has to outlive an edge timeout, and a part that fails is retried on its own:

Retries follow Google Cloud Storage's retry strategy: retry on 408, 429, 5xx and connection errors, with exponential backoff and jitter (https://docs.cloud.google.com/storage/docs/retry-strategy).

The progress display follows the published composer pattern: a percentage and a bar while the size is known, and no bar parked at 100% while the server confirms (https://getstream.io/chat/docs/sdk/react/features/messaging/sending-with-pending-uploads/).

Open-source options I considered first:

  • tus-js-client. Its last stable release, 4.3.1, is from January 2025, with only pre-releases since, so it fails the bar of a release in the last six months and nothing pre-release. A tus server would also need storage in PostgreSQL for OpenBot Desktop and a lock shared across several servers.
  • Presigned S3 multipart. It doesn't work where files stay in PostgreSQL.

So the parts follow OpenAI's and Slack's shape, as the account import already uploads its bundle. The retries use p-retry 9.0.0 and the single lane uses p-limit 7.3.3. Both are pinned and released within the last two months.

Tests

app/tests/upload-in-parts-browser.test.tsx drives the real message box in Chromium and WebKit against a server that answers as the spec says. Every request and answer is checked against the spec's zod schemas, and any request whose body took too long to arrive is answered 524, as Cloudflare answers. Chromium's upload is throttled.

  • Regression: with main's code, the file goes in one request, is answered 524 and is refused ("couldn't be uploaded. Try again."). With this change it arrives whole in parts, with progress shown.
  • Also covered:
    • a failed part retried on its own;
    • a lost part sent again after ATTACHMENT_PARTS_MISSING;
    • giving up after 8 tries and cancelling;
    • small files and older servers using one request;
    • removing a file while it uploads;
    • a file picked before the deployment answer loads, which goes in parts and is screened against the server's own limits.

End to end on the real product (a throwaway Linux box running OpenBot's stack with a local Intelligence and an S3-compatible object store, in Chromium and WebKit, wide and at phone width, light and dark). Uploads went through a proxy limited to 50 KB/s that cuts off any request body still arriving after 40 s, a scaled-down Cloudflare.

  • Before: a 10 MB file was refused after 40 s with "couldn't be uploaded. Try again.", in both browsers.
  • After: the 10 MB file went up in 10 parts in about 3.5 minutes, with its progress shown, and was sent with the message, in both browsers. 3 MB files did the same at phone width and in dark mode.
  • The end-to-end run found the two races above: a file picked before the deployment answer arrived. Both are fixed here, and each has its own test.

Changelog

Conversations.

  • A large file attaches over a slow connection. Where OpenBot offers it, a file over 1MB is sent in parts that each try again by themselves, and its tile shows how much has gone. Before, a file that took longer than the edge allows to send was refused however often it was tried.

A file attached in one request has to reach the server inside the edge's
request timeout. Over a slow upload link a file of a few megabytes can't,
however often it is tried. createAttachmentUpload, uploadAttachmentPart and
completeAttachmentUpload send it in parts of a size the server chooses, each
short enough to arrive, and cancelAttachmentUpload stops one. The deployment
says attachmentUploadsInParts where they are served. The one-request
uploadConversationAttachment is unchanged.
… again by itself

On a slow upload link no file over a few megabytes could be attached: the whole
file went up in one request, and the edge in front of OpenBot Cloud closes any
request whose body takes more than 125 seconds to arrive, so trying again only
failed again.

Where the deployment offers it (attachmentUploadsInParts), a file over 1MB now
goes up in parts of the size OpenBot chooses (createAttachmentUpload,
uploadAttachmentPart, completeAttachmentUpload), as OpenAI's Uploads, Slack's
external uploads, Microsoft Graph's upload sessions and Google's resumable
uploads do. One part is in flight at a time across the tab, so files sent
together don't split a slow link and push every part past the limit. A file of
1MB or less, and every file on an OpenBot that doesn't offer parts, still goes in
one request.

Every request (each part, the start, the finish and the one-request upload) is
tried again by itself while its failure may pass: no answer, 408, 429, a 5xx
(Cloudflare's 520 to 524 among them) or a refusal OpenBot marks retryable,
honouring Retry-After. Up to 8 tries, about 1s doubling to 30s apart with
jitter (p-retry), as Google's and AWS's storage clients retry. A part that is
sent again keeps its Idempotency-Key. A part the finish says is missing
(ATTACHMENT_PARTS_MISSING) is sent again once and the finish asked again with a
new key. An upload that never gets through is stopped on the server
(cancelAttachmentUpload), and the file is refused in words that don't ask the
person to try again.

The tile shows how far a file sent in parts has got: "Uploading…" with the
percentage and a thin bar, and the same bar along a picture's placeholder, read
from XMLHttpRequest's upload progress (fetch reports none in any browser). It
goes through lib/client.ts's one transport,
with the same cookie, headers, refusals and reachability as fetch. Removing a
file while it goes up in parts stops it, and nothing is said about it.

upload-in-parts-browser.test.tsx drives the real message box in Chromium and
WebKit against a server that answers as the spec says (checked with its zod
schemas) and answers 524 to any body that took too long to arrive, with
Chromium's network throttled: in one request the file is refused, in parts it
arrives whole.
completeAttachmentUpload refuses an upload with a part missing under
ATTACHMENT_PARTS_MISSING, listing each part in problems, so an app sends
exactly those again without reading the sentence, and tells it apart from a
full message.
…ent answer

A file picked the moment the message box appeared, before the page had read
the deployment answer, went in one request for good: whether to send it in
parts was read from what the screen drew, and that reads as no until the
answer arrives. On a slow connection that is exactly when the answer takes
longest, so every try of the file was cut off at the edge and it was refused.
Found on the real stack at phone width.

The upload now asks for the deployment answer itself when it sends a file
larger than one part: the one already held, or the one on its way. Only a
deployment that can't be read sends the file in one request. The conversation
composer, the new-conversation screen and the held files of a first message all
go through it, so nothing passes the flag around any more.

upload-in-parts-browser.test.tsx holds the deployment answer until the file is
picked: before this, the file went as one POST .../attachments; now it goes in
parts, in Chromium and WebKit.
…anding in

A 10MB PDF picked right after the page loaded was refused with "is too large for
a PDF. The limit is 8MB." on an OpenBot that takes 100MB. The message box
screens a pick against the limits it was drawn with, and until the deployment
answer arrives those are the smallest any OpenBot takes. It is the same race as
choosing parts from a value the screen read too early.

Now a pick made while that answer is on its way, holding a file a stand-in limit
would refuse on size, waits for the answer (attachmentLimitsNow: the one held or
the one on its way, asked once, the defaults only if it can't be read) and is
screened against it. A pick within every stand-in limit is screened at once, as
before, so its tile shows without waiting. Gestures are screened in turn, each
counting its files before the next is screened, so two picks in one tick still
can't pass the per-message cap. The SDK's own size backstop reads the same
limits when it checks a pick, so it can't refuse what the screen passed before
the render that brings the answer. The conversation box, /channel/new and Home
all screen through this.

upload-in-parts-browser.test.tsx holds the deployment answer and picks a 10MB
PDF: before, it was refused at 8MB; now it uploads, in Chromium and WebKit.
composer-upload-group.test.tsx stages two batches in one tick while the limits
load and still stages exactly the cap.
…ain while they may pass

Since the box learned to wait for the server's limits, every pick was screened a
microtask later, even one with nothing to wait for. In WebKit its tile was then
drawn a task after the pick, and a strip read straight after the pick was empty.
The browser test's wait for uploads to settle read exactly that, saw nothing
uploading, and checked the server before the file had gone (CI, WebKit: "a part
the server failed is sent again by itself" found no part 3 at all).

A pick that has nothing to wait for is screened and staged in the event that
brought it again, as it was before; only a pick that waits for the limits, and
any gesture behind it, takes its turn. The test's wait now waits for each picked
file to be on the strip (or refused) before it looks for uploads in flight.

The deployment read a pick or an upload waits on is also asked again, up to
three more times, while its failure may pass (the uploads' own rule, now in
lib/api/may-pass.ts), so one dropped read on a slow connection doesn't send a
large file in one request. A refusal still answers at once.

upload-in-parts-browser.test.tsx: "a picked file's tile is on the strip as soon
as it is picked" fails in WebKit on the previous composer and passes now.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant