Skip to content

Capped body reads keep downloading after the cap until the request deadline: cancelling the reader does not close Bun's connection #766

Closed

Description

@nc9

readBodyCapped (packages/utils/src/response-body.ts) cancels the body's reader once it reaches its cap. In Bun, cancelling a fetch body frees the JavaScript side but does not close the connection: Bun keeps downloading the rest of the body and throwing it away until the request's own signal fires. For an endless or very large answer, every capped read therefore keeps pulling bytes off the wire up to the caller's deadline. Memory stays flat, but the bandwidth and time are spent, and in the cloud the egress proxy relays that traffic.

Measured on Bun 1.3.14 while reviewing #722 (PR #765), using a raw Bun.listen server that writes an endless body and records when the client closes:

  • With only a reader cancel, the server closed at the 10 s probe timeout, after pushing about 100 GB over loopback.
  • With the request's AbortController aborted after the read, it closed within 5 to 16 ms.

#765 adds that abort to the GraphQL probe. Other callers that read under a cap and do not abort their request after the read include packages/audit-engine/src/external-checker.ts (the 403 WAF body read), cloaking-probe.ts, soft404-confirm.ts and script-fetcher.ts; packages/crawler/src/{llms,agent-access,rsl,well-known,robots}.ts; packages/crawler/src/fetcher.ts and core/crawler.ts; and packages/fetchers/src/index.ts. Each needs checking.

Acceptance Criteria

  • Every request whose body is read through readBodyCapped (or another capped read) has its connection closed once the read ends, by aborting the request's signal or by an equivalent the shared helper provides.
  • A raw-socket test, using a Bun.listen server with an endless body that records when the client closes, shows the connection closing within 1 s of a capped read returning, for the shared helper and for at least the crawler's document fetch and the external link checker.
  • No change to what is read: the existing capped-read and bomb tests still pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2bugperformancesize: M

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions