Skip to content

fix(engine): close the connection once a capped read returns #766 - #777

Merged
nc9 merged 3 commits into
mainfrom
fix/capped-read-closes-connection-766
Oct 14, 2026
Merged

nc9 merged 3 commits into
mainfrom
fix/capped-read-closes-connection-766

Conversation

@nc9

@nc9 nc9 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #766.

What was wrong

Cancelling a response body does not close Bun's connection: it keeps downloading whatever the origin sends until the request's signal aborts. Every capped read stopped at its limit and cancelled the stream, and several status checks cancelled a body unread, but these callers then only cleared their timeout. With the timer cleared nothing ever aborted the request, so a site answering with an endless body kept the connection open, and kept sending, for as long as it liked (in practice until the audit ended). The issue said "until the request deadline"; the review measured it running on well past the deadline, gigabytes in a few seconds on loopback. reliability/broken-forms was the exception: its AbortSignal.timeout bounded each probe at 10 s.

The fix

Abort the request's controller once the caller is done with it, in the finally that already clears the timeout:

  • withRequestDeadline (crawler): covers safeFetchWithDeadline and so robots.txt, sitemaps, llms.txt, markdown, well-known, agent-access and RSL probes, plus the seed redirect probe and the CLI request tool. Its documented contract was already "use reads or cancels the body before it returns", and every caller buffers.
  • the fetch document fetcher (@squirrelscan/fetchers), used for plain-HTTP page fetches when cloud rendering falls back
  • the external link checker (the 403 interstitial read, and every HEAD/GET/hop it opened)
  • the script fetcher, the soft-404 confirmation re-fetch and the cloaking probe
  • the link target probe and the resource checker (status checks that drop a body unread, or size from a Content-Length they never read)
  • the reliability/broken-forms probes (a done controller joined to the signal, as the GraphQL probe does)
  • the CLI's forked script fetcher and external checker, and its localhost reachability check

The crawler's own document fetch already aborted on release; its comment claimed the abort was a no-op on success, which is now corrected (it is what closes a read stopped at the cap). readBodyCapped's doc now says the caller must abort.

Aborting after a body was read to the end is a no-op: the connection stays pooled. A test pins that (two sequential requests over one connection).

Tests

Raw Bun.listen servers that send an endless body and record the close (Bun.serve hides a client hang-up). Each asserts the connection closes within 1 s of the call returning, with a 10 s deadline:

  • packages/crawler/tests/capped-read-closes-connection-766.test.ts: safeFetchWithDeadline + readBodyCapped (close-delimited and Content-Length bodies), withRequestDeadline with a body cancelled unread, the crawler's document fetch, the fetch document fetcher, and the keep-alive check.
  • packages/audit-engine/tests/capped-read-closes-connection-766.test.ts: external checker 403 read, script fetcher over its cap, soft-404 confirm, cloaking probe, link target probe (HEAD 405, Range ignored), resource checker (Content-Length never read).
  • apps/cli/tests/crawler/capped-read-closes-connection-766.test.ts: the CLI script fetcher and external checker forks, and the localhost reachability check.
  • packages/rules/tests/broken-forms.test.ts: every probe request's signal is aborted once answered (the rule only probes https actions, so this one uses the file's fetch stub).

Without the fix every one of these fails except the crawler's document fetch and the keep-alive test, which pass either way as guards.

packages/crawler/tests/deadline.test.ts: "the deadline is disarmed" asserted the signal was never aborted after a successful read, which is exactly what changes. It now asserts the signal is still armed while use reads and aborted once it returns, and a new test checks the deadline timer is still cleared.

Existing capped-read, bomb and deadline suites pass unchanged (crawler content-encoding-bombs, deadline, fetch-body-deadline, fetch-interrupt, discovery-probes, redirect-header-security, preamble-budget, decode-failure, entry-fetch-timeout, seed redirect, sitemap and well-known truncation; utils response-body and fetch-undecoded; fetchers fetch-fetcher; engine fetch-deadline, undecoded-probe-fetches, external-link egress, cloaking, soft-404, asset compression, asset egress, resource-checker 416 and image header, link-probe safety, link targets e2e, manifest, bulk checker; rules broken-forms; CLI fetch-deadline, undecoded-fetches, script-fetcher-encoding, request-deadline, reachability). One CLI test, checkReachability returns instead of hanging on a stalled body, fails locally on macOS with or without this change (it dials 127.0.0.2, which macOS does not route); it passes on Linux CI.

Gates: tsgo in crawler, fetchers, audit-engine, rules, utils and apps/cli; CLI format:check and lint; detect-secrets on the changed files.

Docs and skill

No docs change: no documented behaviour or option changes, and what an audit reads is the same. CHANGELOG entry added under Fixed.

The squirrelscan agent skill needs no change.

@nc9
nc9 marked this pull request as ready for review October 14, 2026 18:43
@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #777 (abort the request controller once the caller is done with it, #766)

The fix is consistent. Every site aborts in the same finally that already clears the timeout, and withRequestDeadline already documented that use must consume the body before returning. I found nothing that should block the merge.

  • (medium) packages/crawler/src/deadline.ts:41 withRequestDeadline now aborts unconditionally. If any current or future caller returns a lazy Response, a stream or a .body reader out of use and consumes it later, it will fail with an AbortError. The docstring states the contract, but nothing enforces it. Please grep all withRequestDeadline and safeFetchWithDeadline callers once more to confirm none return an unread body. A test that returns the response from use would document the new behaviour.
  • (low) apps/cli/src/crawler/external-checker.ts:187, packages/audit-engine/src/external-checker.ts:308 and similar sites: calling abort() after a normal completion is a no-op for a fully read body, but it can surface as a rejected promise or an unhandled abort listener if anything still holds the signal. Check that no .catch-less promise or listener is attached to these signals, for example the redirect-hop loops that share one controller.
  • (low) packages/rules/src/reliability/broken-forms.ts:453 The finally is attached to a try that already returns early on some paths. This is fine, but done is never aborted if an exception is thrown before the try starts. That is probably unreachable. Please confirm.
  • (nit) The same three-line comment is repeated at about 12 sites. A short helper or a single reference to withRequestDeadline would reduce the noise.
  • (nit) The tests use fixed thresholds (< 1_000 ms close delay, 3 s wait) on loopback sockets. These may be flaky on slow CI runners. Consider a more generous bound, since the 10 s deadline is the real discriminator.

Test coverage is good. Raw-socket endless-body tests cover the CLI and the engine paths: script fetcher, external checker, soft-404, cloaking, link-target probe and resource checker. Not covered by a dedicated test are withRequestDeadline itself (robots, sitemaps and the other crawler probes), the fetchers package fetch document fetcher, and the broken-forms probes. A direct test of withRequestDeadline would cover most of those callers.

nc9 added 3 commits October 15, 2026 06:13
Cancelling a body does not close Bun's connection: it keeps downloading
until the request's signal aborts, so a capped read of an endless body held
the socket open and receiving until the request deadline. Abort the request
once the read is done in withRequestDeadline (every root probe, the seed
redirect probe, the CLI request tool), the fetch document fetcher, the
external checker, the script fetcher, the soft-404 and cloaking re-fetches,
and the CLI forks. Aborting after a body read to the end is a no-op and
keeps the connection pooled.

Signed-off-by: Nik Cubrilovic <[email protected]>
Review follow-up. The link target probe, the resource checker, the
broken-forms probes and the CLI's localhost reachability check cancel bodies
unread and only cleared their timeout, so an endless body kept downloading
after they returned. Abort each request's controller once it is done. Fix
the wording: with the timer cleared nothing aborted these requests, so the
transfer ran on for as long as the origin sent, not until the deadline.
Tests for each, a Content-Length variant, and a check that the deadline
timer is still cleared.

Signed-off-by: Nik Cubrilovic <[email protected]>
…line timer by delay

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/capped-read-closes-connection-766 branch from 9b108e3 to 0f9451a Compare October 14, 2026 19:14
@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #777 (abort the request controller in finally so Bun closes connections after capped or unread bodies).

No blocking findings. I read the diff but did not run the tests.

  • (low) packages/crawler/src/deadline.ts: withRequestDeadline and safeFetchWithDeadline now abort on exit. The doc comment tells callers to finish with the body before use returns, but nothing enforces it. A future caller that returns a streaming Response or body out of use would see it aborted mid-read. The new comment helps, but consider saying outright that the response must not escape use.
  • (low) Connection reuse: every call site aborts, and only the crawler test checks that a fully read body stays pooled. The pooling claim is for a body read to the end. The 403 and HEAD paths in external-checker and resource-checker abort with the body unread, so those connections are no longer reused. That is probably fine for a link checker, but it could mean more TLS handshakes on large audits. Worth a quick look if audit time regresses.
  • (nit) The endlessServer and lastCloseDelay helpers are copy-pasted across three test files, and the CLI and engine copies are nearly identical. A shared test helper would cut the duplication.
  • (nit) The tests assert a close within 1 s (3 s wait). That is generous, but it could still flake on slow CI runners.

The changelog entry is clear and the tests cover each changed call site.

@nc9
nc9 merged commit 3cf0f22 into main Oct 14, 2026
15 checks passed
@nc9
nc9 deleted the fix/capped-read-closes-connection-766 branch October 14, 2026 19:31
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.

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

1 participant