Skip to content

Hosted request limits; paced X media downloads - #57

Merged
RhysSullivan merged 1 commit into
mainfrom
large-bodies-and-paced-media
Nov 27, 2026
Merged

RhysSullivan merged 1 commit into
mainfrom
large-bodies-and-paced-media

Conversation

@RhysSullivan

Copy link
Copy Markdown

Two gaps on the hosted emulators.

1. Large request bodies reset an instance, and every stream in it. A 100 MiB body to a hosted OpenAI instance answered 500 (error code: 1101). wrangler tail showed the cause: "Durable Object's isolate exceeded its memory limit and was reset". On the previous code, a paced stream on the same instance was cut the moment the 100 MiB request arrived. Streams on 32 other instances, in three runs, survived; Cloudflare can place several objects in one isolate, so it is possible, but it did not happen in those runs. Bodies from about 24 MiB failed now and then, because the body was held several times over: the Worker buffered it, the ledger's clone kept a second copy, and each save kept the JSON of the whole store.

  • Refuse before reading. The Worker checks content-length against the service's limit and answers 413 in the provider's error shape where it has one. It streams every other body to the instance; a body of unknown length is read up to the limit. The hosted limit is 16 MiB.
  • OpenAI: 32 MiB, stated. OpenAI documents no overall request limit for these endpoints (only 50 MB across the files in one request), so the emulator's limit is its own: 33,554,432 bytes. A larger body answers 413 with {"error":{"message":…,"type":"invalid_request_error","param":null,"code":"request_too_large"}}, before the body is read, locally and hosted.
  • Fewer copies. The ledger records a body declared over 1 MiB by its size, without a clone. A save keeps a length and 64-bit hash of each stored value instead of its JSON.
  • A store budget. A write that would take an instance's stored data past 32 MiB answers 507 instance_storage_full rather than running the isolate out of memory. GET /_emulate/persistence shows stored_bytes and budget_bytes.
  • Local serve ends the connection after answering a request whose body it did not read, so a client still uploading gets the answer.

Measured on a staging copy of the hosted worker:

  • OpenAI: 100 MiB answers 413. Ten 32 MiB requests, three concurrent ones, and three 32 MiB bodies of non-ASCII text all answer 200.
  • GitHub: a 16 MiB issue body answers 201, and 17 MiB answers 413. On one instance, three 8 MiB issues are kept and the fourth answers 507, with no 500s.
  • Streams: a paced stream on the same instance completes when a 100 MiB request (413) or a 32 MiB request (200) arrives, and 8 streams on other instances all completed.

2. Paced X DM media downloads. dmConversationsMediaDownload can send an attachment as exactly chunks pieces interval_ms apart, the same controls the OpenAI emulator has for a paced answer. It is set with attachment.stream in the seed or on /_emulate/x/dm/arrive, or with POST /_emulate/x/media/:media_key/stream. It is an emulator control, listed in the manifest. Unpaced stays the default. On staging, 90 MiB as 300 pieces 100 ms apart arrived whole in 30.1 s, with no gap over 135 ms.

Tests:

  • request-limits.test.ts: the oversized body is refused unread in both forms, and bodies at the limit pass in both forms.
  • hosted-writes.test.ts: the store budget.
  • OpenAI tests: the 413 through the official SDK, and undeclared lengths.
  • X tests: paced and unpaced downloads, and the control route.

…nloads

A 100 MiB request body to a hosted OpenAI instance answered 500 (Cloudflare
1101): the Durable Object's isolate exceeded its 128 MB memory limit and was
reset, ending every request in it, a paced stream on the same instance
included. From about 24 MiB it failed now and then, as the body was held
several times over: the Worker buffered it, the ledger's clone kept a second
copy, and each save kept the JSON of the whole store.

- The Worker refuses a body over the service's limit with a 413 before
  reading it, in the provider's error shape where it has one, and streams
  every other body to the instance. Hosted limit 16 MiB.
- The OpenAI emulator accepts request bodies up to 32 MiB, locally too, and
  refuses a larger one with 413 request_too_large in OpenAI's error shape.
  OpenAI documents no overall limit for these endpoints.
- The ledger records a body declared over 1 MiB by size, without a clone.
- A save keeps a fingerprint of each stored value, not its JSON.
- A write that would take an instance's stored data past 32 MiB is refused
  with a 507; GET /_emulate/persistence shows the size and the budget.
- A local server ends the connection after answering a request whose body
  it did not read.

X: a DM attachment's download can be paced, the same controls as the OpenAI
emulator's paced answers: attachment.stream { chunks, interval_ms } in the
seed or on /_emulate/x/dm/arrive, or POST /_emulate/x/media/:media_key/stream.
Unpaced stays the default.
@RhysSullivan
RhysSullivan marked this pull request as ready for review November 27, 2026 07:34
@RhysSullivan
RhysSullivan merged commit 7a320da into main Nov 27, 2026
1 check passed
@RhysSullivan RhysSullivan mentioned this pull request Nov 27, 2026
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