Skip to content

Close unread read_url response bodies - #1756

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/read-url-response-cleanup
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/read-url-response-cleanup

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

read_url returns early for an HTTP error, unsupported content type, or an oversized declared response without consuming the body. It clears the request timeout on exit but leaves that body open. A server streaming unsupported content indefinitely therefore keeps its connection after the tool has already returned an error.

Changes

  • Retain the last response for cleanup and cancel its unlocked body in finally. Cancellation errors do not replace the original tool result; bodies already owned by a reader are left alone.
  • Add one regression to the existing loopback HTTP fixture. The server sends an unsupported body without ending it; the test checks the existing error result and observes the actual socket closing.

Verification

Windows, Bun 1.3.14, base c2ce3b69fdbb4a691dfd7d39edc2cb8943d85cc1, with the repository's isolated test environment and no real credentials.

  • The socket regression fails on unmodified main: the tool returns its error, but the connection stays open. It passes after the fix.
  • bun test sdk/src/__tests__/read-url.test.ts sdk/src/__tests__/read-url-dns-pinning.test.ts: 31 passed / 1 failed before; 32 passed / 0 failed after.
  • A separate real HTTP probe also observed socketClosed: false before and true after.
  • Frozen-lockfile install, SDK TypeScript check, SDK build, Windows Freebuff binary build, binary version and official smoke test: passed.
  • Prettier and git diff --check: passed. ESLint: 0 errors, 5 existing import/resolver warnings.

Notes / Scope

No dependency or public API changes. Existing SSRF checks, DNS pinning, redirect policy, error messages and timeout behavior are preserved. The fixture permits only loopback through the existing injected address policy. No external network or authenticated model/TUI session was used. Linux/macOS, a separate native Node runtime and the full monorepo suite were not run; hosted CI is pending publication.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a well-scoped fix. The core change is small and correct: response is hoisted out of the loop so the finally block can see the last response assigned even when the function returns early for an error/unsupported-type/oversized-body case, and then cancels the unlocked body so the underlying socket is released. The guard !response.body.locked correctly avoids double-cancelling a body that's already being streamed/read elsewhere, and .catch(() => {}) avoids a cancellation error masking the original tool result, which is called out explicitly and is good practice.

The added test in read-url-dns-pinning.test.ts is solid: it spins up a loopback server that writes an unsupported content-type and never ends the body, then asserts both the existing error result and that the socket actually closes within a timeout. This gives a concrete regression that fails on main and passes after the patch, which is exactly the kind of evidence this repo wants for a fix like this.

Minor notes for the maintainer porting this by hand:

  • Confirm this doesn't interact oddly with transports (e.g. undici vs fetch polyfills) where response.body might not support .cancel() identically.
  • The test lives in the DNS-pinning file rather than a general read-url body-handling test file; a maintainer may want to relocate it, but that's a nit, not a blocker.
  • Worth double-checking behavior when response is reassigned across redirect hops - only the final response is cancelled, which seems intentional since only the last one is held open.

Overall this is a focused, correctly scoped fix with a real reproducible test. Recommending port-candidate.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 28, 2026

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

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants