Skip to content

[CB-0] [sdk-superfan] Apply the request timeout to reading the body - #246

Draft
spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/timeout-body
Draft

spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/timeout-body

Conversation

@spicysouvlaki

@spicysouvlaki spicysouvlaki commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What happened

#206: when a server sends headers and then stalls, the request never times out (still pending, not APIConnectionTimeoutError).

Why

fetchWithTimeout() clears its timer once the headers arrive. defaultParseResponse() then reads the body with no timer running.

Fix

defaultParseResponse() arms the request timeout on the request's controller while it reads JSON or text. An abort from that timer becomes APIConnectionTimeoutError. Raw responses (asResponse(), binary) are unchanged, so downloads the caller streams are not cut off.

Tests

  • New local-server test: headers plus {, then a stall, with timeout: 50. Main hits the 5 s jest timeout under native fetch and node-fetch. This branch rejects with APIConnectionTimeoutError in about 60 ms under both.
  • ./scripts/lint, ./scripts/test (203 passed), ./scripts/build all pass.

Risk and rollout

src/core.ts is generated, so this is a sealed edit. The body gets its own timeout window, so the worst case is about 2× timeout and not the remainder of the first one. It ships with the next SDK release.

Fixes #206

🤖 Generated with Claude Code

fetchWithTimeout() clears its timer as soon as fetch() resolves with
headers. defaultParseResponse() then reads response.json()/text() with no
timer, so a server that sends headers and stalls leaves the promise pending
forever, despite the documented timeout and APIConnectionTimeoutError.

defaultParseResponse() now arms the same timeout on the request's controller
while it reads the body and maps that abort to APIConnectionTimeoutError
(a caller abort still surfaces as before). The timeout is passed through the
internal APIResponseProps.

Scope: the body gets its own timeout window (like a read timeout), not the
remainder of the header budget. Raw responses (__binaryResponse,
asResponse()) are untouched, so long downloads the caller streams are not
cut off by the request timeout.

Measured: new test serves headers plus '{' and stalls, timeout 50 ms. On main
it hits the 5 s jest timeout under native fetch and node-fetch; with this
change both reject with APIConnectionTimeoutError in ~60 ms.

src/core.ts is generated; this edit is sealed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

Request timeout does not cover stalled response bodies

1 participant