Skip to content

fix(fetch): reuse keep-alive HTTP Agents across requests - #13182

Open
SebTardif wants to merge 1 commit into
continuedev:mainfrom
SebTardif:fix/reuse-fetch-http-agent
Open

fix(fetch): reuse keep-alive HTTP Agents across requests#13182
SebTardif wants to merge 1 commit into
continuedev:mainfrom
SebTardif:fix/reuse-fetch-http-agent

Conversation

@SebTardif

Copy link
Copy Markdown

Description

Reuse keep-alive HTTP Agents in @continuedev/fetch instead of allocating a new Agent({ keepAlive: true }) on every request.

fetchwithRequestOptions is on the path for LLM and other HTTP calls. Each call created a new follow-redirects / proxy Agent with keepAlive: true and a 2 hour timeout, then dropped the reference. Idle sockets stayed on those abandoned Agents and could exhaust file descriptors under chat load.

keepAlive stays enabled. #8378 already dropped keepAliveMsecs so TCP keep-alive pings work. This change makes that keep-alive pool actually get reused.

Agents are keyed by protocol, proxy (or bypass), timeout, verifySsl, CA bundle path, and client certificate fields. Concurrent first requests for the same key share one in-flight create so a startup burst does not leak extra Agents.

Checklist

  • I've read the contributing guide
  • The relevant tests, if any, have been updated or created

Tests

  • packages/fetch/src/httpAgentCache.test.ts: same options reuse one Agent; verifySsl, timeout, protocol, and proxy isolate the cache; bypassed proxy matches no proxy; keepAlive is true; clearHttpAgentCache drops the instance; concurrent first requests share one Agent.
  • packages/fetch vitest: 105 passed, including existing e2e. e2e afterEach now destroys cached sockets so the next test does not reuse a closed server on the same port.

Related: #8378, #8099. Agent-per-request dates to #1750; keepAlive: true was extracted in #5715.

Creating a new Agent({ keepAlive: true }) on every fetch left idle
sockets on abandoned Agents. Cache Agents by protocol, proxy, TLS,
timeout, and client cert options, including in-flight first requests.
@SebTardif
SebTardif requested a review from a team as a code owner August 24, 2026 05:02
@SebTardif
SebTardif requested review from sestinj and removed request for a team August 24, 2026 05:02
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@SebTardif

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

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.

1 participant