Repository navigation
fix(engine): close the connection once a capped read returns #766 - #777
Conversation
|
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
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 |
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]>
9b108e3 to
0f9451a
Compare
|
Review of #777 (abort the request controller in No blocking findings. I read the diff but did not run the tests.
The changelog entry is clear and the tests cover each changed call site. |
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-formswas the exception: itsAbortSignal.timeoutbounded each probe at 10 s.The fix
Abort the request's controller once the caller is done with it, in the
finallythat already clears the timeout:withRequestDeadline(crawler): coverssafeFetchWithDeadlineand 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 "usereads or cancels the body before it returns", and every caller buffers.@squirrelscan/fetchers), used for plain-HTTP page fetches when cloud rendering falls backreliability/broken-formsprobes (adonecontroller joined to the signal, as the GraphQL probe does)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.listenservers 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),withRequestDeadlinewith 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 whileusereads 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.