How Unread Error Bodies Filled a 20-Connection fetch Pool and Hung Every Shipping-Rate Request for 26 Minutes
← Back
December 1, 2026Node.js9 min read

How Unread Error Bodies Filled a 20-Connection fetch Pool and Hung Every Shipping-Rate Request for 26 Minutes

Published December 1, 20269 min read

09:52 UTC. Datadog monitor "checkout p99 > 8s" fires. A minute later the second one: "shipping-rates request count flat at 0". Requests are arriving. Nothing is leaving.


the scramble

The shipping-rates service is a small Node 20 app. At checkout it asks a carrier API for a quote, adds our margin, and returns the result. The carrier API is the only upstream. It had been fine for months.

Meera was on call. She paged me at 09:55 because she had already restarted the service once and it had recovered for about ninety seconds before going quiet again. Her first theory: the carrier was down. Reasonable guess, wrong answer. The carrier's status page was green, and when I curled their quote endpoint from my laptop it answered in 340 ms.

What the dashboard showed was stranger than an outage. Inbound requests to our service were normal, about 45 per second. Event loop lag was under 5 ms. CPU was at 6%. Memory was flat. The service looked idle and was completely stuck. That usually means every request is waiting on something that will never answer.

the hunt

First dead end: the carrier. I ran the same quote request from inside the pod with the same API key.

$ kubectl exec shipping-rates-7c9d-x2k4p -- curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" \ https://api.carrier.example/v2/quote -d @sample.json 200 0.412s

Fine from the pod too. So the network path and the credentials were good, and the service was hanging anyway.

Second dead end: DNS. We have been bitten by conntrack and resolver trouble before, so I checked it before anything else. The lookup took 2 ms. Not that.

Third: the logs. They were nearly empty, and that was the clue. Every request logs a line when it enters the handler and another when the carrier call completes. In the last ten minutes there were 27,000 "entered" lines and 61 "completed" lines. The handlers were starting and never finishing. Nothing was throwing, because nothing was returning.

I attached the inspector with SIGUSR1 and looked at the undici pool stats we expose on a debug route:

$ curl -s localhost:9229-proxy/debug/pool | jq { "origin": "https://api.carrier.example", "connected": 20, "free": 0, "running": 20, "pending": 8841, "queued": 8841 }

Twenty connections, all running, 8,841 requests waiting. The pool was a hard cap we had set on purpose, because the carrier contract allows 25 concurrent connections per key. The question was why 20 requests had been "running" for minutes against an API that answers in under half a second.

I checked the sockets from the OS side. All 20 were ESTABLISHED, all idle, with nothing in the send or receive queue. They were not waiting on the carrier. They were held by us.

the find

The carrier had started returning intermittent 503s at about 09:30, roughly one in six requests, with a tiny JSON error body. Our client wrapper handled that like this:

async function quote(payload: QuoteRequest, attempt = 1): Promise<Quote> { const res = await fetch(CARRIER_URL, { method: 'POST', body: JSON.stringify(payload), headers: { 'content-type': 'application/json', 'x-api-key': KEY }, dispatcher: carrierAgent, }); if (!res.ok) { if (attempt < 3 && res.status >= 500) { await sleep(100 * attempt); return quote(payload, attempt + 1); } throw new CarrierError(res.status); } return (await res.json()) as Quote; }

Look at the !res.ok branch. It retries or throws without ever touching res.body. With undici, a response does not release its connection back to the pool until the body has been read to the end, cancelled, or garbage collected. The success path calls res.json(), which drains the stream. The error path never reads anything, so each 503 left its socket checked out.

The pool did not leak forever, which is why I was slow to see it. Eventually the garbage collector finalizes an abandoned response and undici reclaims the connection. But our heap was small and flat, so major GC was rare. At one failed request in six and 45 requests per second, we were abandoning about 7 connections per second against a pool of 20. It filled in three seconds. Collection could not keep up.

checkout request | v quote() --- fetch --- [ pool: 20 sockets ] --- carrier | ^ | 503 -> throw | socket still owned by unread body v | (released only on drain, cancel, or GC) retry / CarrierError -------+ | +-- next requests: queue behind a full pool, no timeout

The second half of the failure was the missing timeout. We had set headersTimeout and bodyTimeout on the agent, and both only start counting once a request has been dispatched on a socket. A request that is still sitting in the pool's queue has no clock running. So the 8,841 waiting requests could wait indefinitely, and every checkout waiting on a quote waited with them.

It also explained the 90 seconds of health after Meera's restart. A fresh process starts with an empty pool and nothing abandoned. The 503s began filling it again immediately.

the fix

Two changes, deployed at 10:18. The first: always dispose of the body on the failure path.

if (!res.ok) { // Release the socket whether we retry or throw. await res.body?.cancel().catch(() => {}); if (attempt < 3 && res.status >= 500) { await sleep(100 * attempt); return quote(payload, attempt + 1); } throw new CarrierError(res.status); }

If we wanted the error text for logging, we would read it with await res.text() instead, since that drains the stream too. Either way, every code path now ends with the body consumed or cancelled.

The second: put a clock on the queue. Undici has no queue timeout of its own, so the deadline goes on the call itself:

const res = await fetch(CARRIER_URL, { method: 'POST', body: JSON.stringify(payload), headers: { 'content-type': 'application/json', 'x-api-key': KEY }, dispatcher: carrierAgent, signal: AbortSignal.timeout(4000), // covers queue wait + headers + body });

An aborted fetch also releases its socket, so a stuck request can no longer pin one. A checkout that cannot get a quote in four seconds now fails fast with a clear error and falls back to a flat-rate estimate, instead of hanging until the load balancer gives up at 60.

the aftermath

  • Impact: 26 minutes of degraded checkout, 09:52 to 10:18. About 1,900 checkout attempts hit the 60 second gateway timeout. Support counted 212 abandoned carts that were later recovered by the cart-reminder email.
  • Pool saturation is now an alert: pending > 50 for one minute pages. The pool stats route we had used for debugging is now a Datadog gauge. Before this incident we had no signal for "requests waiting for a socket", only "requests failing".
  • We added an ESLint rule to the carrier client package that flags any fetch result with a branch that returns or throws without reading the body. It is a blunt check, and it has already caught one more instance in our tax-service client.
  • A test now runs a local server that answers 503 with a body 100 times in a row against a pool of 5, and asserts the 101st request completes in under a second. With the old wrapper it hangs.

What annoys me is how healthy it all looked. CPU at 6%, flat memory, an empty error log. A blocked service looks a lot like a calm one until you compare how many requests entered the handler with how many finished.

Share this
← All Posts9 min read