Skip to content

Connect gate buffers slow downloads without backpressure until the tunnel object runs out of memory #4314

Open

Description

@SawyerHood

Summary

A large HTTP response from a bb server that a visitor reads slower than the server sends it piles up in the connect gate's TunnelDO memory. There is no backpressure anywhere on the path. Once roughly 100+ MB is unread, the Durable Object exceeds its memory limit and resets. The download fails, and the server's tunnel drops for everyone using it. The gate needs flow control so that a slow reader slows the server down instead of filling gate memory.

Versions and environment

  • main at 98706229e4e088b46f1b676280c891f224d4a071: connect gate (apps/connect), @bb/tunnel-client, connect plugin.
  • Reproduced against the staging gate bb-connect-staging (*.vibecodethis.site), running main's gate with main's client code.

Steps to reproduce

Reproduced with a lab harness in bb thread thr_j2j7gu9n5f. The harness runs a real connect plugin ConnectTunnel for a staging server and serves a streaming origin from the same process.

  1. Pair a bb server with the staging gate. Serve a 400 MB response through it: any endpoint that streams 400 MB with Node's normal res.write() backpressure toward the tunnel client works.

  2. Download it through the gate with a slow reader:

    curl -s --limit-rate 256k -o /dev/null -w '%{http_code} %{size_download} %{time_total}\n' \
      -H "x-bb-connect-machine: <machine credential>" \
      "https://<label>.vibecodethis.site/api/v1/big?mb=400"
  3. Watch the server's tunnel and the Durable Object outcome. Use GraphQL durableObjectsInvocationsAdaptiveGroups, or a tail on staging only. Never tail production; see AGENTS.md.

Expected vs actual

Expected: the download completes at the reader's pace, and gate memory stays bounded.

Actual (staging):

200 11501289 43.653391          # curl: cut after 11.5 MB of 400 MB
outcome: exceededMemory         # TunnelDO invocation for /api/v1/big?mb=400
exception: Durable Object's isolate exceeded its memory limit
tunnel closed (code 1006); reconnecting in 1000ms   # server's own tunnel drops

Evidence

Gate: every body-chunk frame is copied and appended to a promise chain of writer.write() calls. Nothing limits how much is queued while the visitor isn't reading. See apps/connect/src/tunnel-do.ts#L541-L549 and the response TransformStream at #L511.

Client: the tunnel client reads the origin response as fast as the origin produces it and sends every frame immediately. It never checks the WebSocket's bufferedAmount, and the gate can't ask it to pause. See packages/tunnel-client/src/session.ts#L314-L318.

Production (bb-connect, Cloudflare GraphQL durableObjectsInvocationsAdaptiveGroups, status: exceededMemory):

  • 2026-09-20: 87 events, all on one server's object.
  • 2026-09-24: 2 events on two different objects.
  • No minute had more than one object crash, so there's no sign that neighbors in the same isolate are affected.

Large responses that complete successfully today and must keep working (Cloudflare GraphQL httpRequestsAdaptiveGroups, status 200, ranked by bytes):

  • /internal/server-move/* archives of about 285–310 MB (several a day on Sep 23 and 24).
  • 127 MB APK downloads.
  • Video streams averaging 20–225 MB through port shares.
  • About 30 MB /install/bb-app.tgz from older servers.

What I ruled out

Proposal

Add stream flow control to the tunnel protocol, gated by PROTOCOL_VERSION. Old clients keep today's behavior.

  1. The gate tracks unread bytes per HTTP stream: bytes queued minus bytes consumed by the visitor. When they pass a high watermark (e.g. 8 MB), it sends a stream-pause { streamId } frame. Once they fall below a low watermark (e.g. 2 MB), it sends stream-resume { streamId }.
  2. The tunnel client pauses the origin IncomingMessage (res.pause() / res.resume()) for that stream. It also stops reading while the tunnel's bufferedAmount is high, so a slow uplink doesn't queue frames ahead of heartbeats.
  3. Apply the same to ws-data for visitor WebSockets and to request bodies (uploads) in the other direction.
  4. Optionally, for protocol-v1 clients only, keep a generous safety cap well above normal backlogs, so a stalled reader can't crash the object. If a cap is kept, errors the response through its TransformStream controller so the stuck write is released.

Acceptance: a 400 MB download read at 256 KB/s through staging completes, the Durable Object reports no exceededMemory, and the unread backlog stays near the watermark. Server-move archives, APKs and video streams still work. Tests cover pause/resume ordering and a client that ignores the frames (v1).

Suggested priority and effort

Medium priority, medium effort. It affects servers that serve big files to slow readers, and it drops that server's tunnel when it happens. The workaround is to download on a faster connection. Nothing is lost beyond the failed download and a reconnect.

Found while investigating connect disconnects in bb thread thr_j2j7gu9n5f; related PR #4313.

AGENT GENERATED

Activity

  1. added
    remote
    connect
    and removed
    remote
    on Sep 25, 2026
  2. added theissue type on Sep 25, 2026
  3. added
    remote
    partial-repro
    on Sep 25, 2026
  4. bb-slop-cop commented on Sep 26, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Classification: Bug · High priority · High effort. Labels: perf, remote, connect, partial-repro.

    Reproduction and root-cause report: PARTIALLY REPRODUCED, with high confidence in the root cause. The focused test failed in two clean origin/main checkouts: the gate accepted 32 MiB before the visitor read it and sent no flow-control signal. The cloud memory reset was not repeated. Reproduction label: partial-repro.

    Root cause: the gate copies response chunks into a stalled write chain while the tunnel client keeps reading the origin without gate-capacity feedback.

    No pull request: a safe fix changes the public tunnel protocol across the gate and client, beyond this rule’s simple-fix limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    connectpartial-reproperfremote

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions