node icon indicating copy to clipboard operation
node copied to clipboard

http.Agent: reused keep-alive sockets keep the last request's AsyncLocalStorage context after returning to the free pool

Open anatolzak opened this issue 1 month ago • 4 comments

Version

v26.13.0, v24.22.1, v22.23.2

Platform

Darwin 25.6.0 arm64 (macOS)

Subsystem

http, async_hooks / AsyncLocalStorage

What steps will reproduce the bug?

When http.Agent reuses a keep-alive socket, it re-binds the socket's TCP handle to the current request's async context. When the socket goes back to the free pool, nothing undoes that. So every idle socket keeps the last request's ALS store alive, even when the agent and the connection were both created outside any store.

Core node:http only, with no external server or dependencies. Save this as repro.mjs and run:

node --expose-gc repro.mjs
SOCKETS=4 node --expose-gc repro.mjs
SOCKETS=4 node --expose-gc repro.mjs --isolated
import http from 'node:http';
import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';

const storage = new AsyncLocalStorage();
const isolatedContext = new AsyncResource('transport');
const isolate = process.argv.includes('--isolated');
const socketCount = Number(process.env.SOCKETS ?? 1);
const agent = new http.Agent({
  keepAlive: true,
  maxSockets: socketCount,
  maxFreeSockets: socketCount,
});
const server = http.createServer((request, response) => {
  request.resume();
  request.on('end', () => response.end('ok'));
});
server.keepAliveTimeout = 60_000;
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const url = `http://127.0.0.1:${server.address().port}`;
const references = [];

function request() {
  return new Promise((resolve, reject) => {
    const req = http.request(url, { agent }, response => {
      response.on('error', reject);
      response.on('end', resolve);
      response.resume();
    });
    req.on('error', reject);
    req.end();
  });
}
function send() {
  return isolate ? isolatedContext.runInAsyncScope(request) : request();
}
async function collect() {
  for (let i = 0; i < 20; i++) {
    await new Promise(resolve => setImmediate(resolve));
    global.gc();
  }
}

try {
  // Create the connections before any request context exists.
  await Promise.all(Array.from({ length: socketCount }, send));
  for (let batch = 0; batch < 5; batch++) {
    await Promise.all(Array.from({ length: socketCount }, (_, id) =>
      storage.run({ payload: Buffer.alloc(1024 * 1024), id }, async () => {
        references.push(new WeakRef(storage.getStore()));
        await send();
        if (storage.getStore()?.id !== id) throw new Error('Context lost');
      }),
    ));
  }
  await collect();
  const freeSockets = Object.values(agent.freeSockets).flat().length;
  const beforeDestroy = references.filter(ref => ref.deref()).length;
  agent.destroy();
  await collect();
  const afterDestroy = references.filter(ref => ref.deref()).length;
  console.log({ node: process.version, isolate, freeSockets,
    stores: references.length, beforeDestroy, afterDestroy,
    outsideStoreIsUndefined: storage.getStore() === undefined });
} finally {
  agent.destroy();
  server.closeAllConnections();
  await new Promise(resolve => server.close(resolve));
}

--isolated runs request initiation inside an AsyncResource created at module scope. It's a control that shows the retention comes from the context active when the socket is reused.

How often does it reproduce? Is there a required condition?

Every run, on all three versions, with pool size 1 and 4. The only requirement is that an idle keep-alive socket is still open. The repro server uses keepAliveTimeout = 60_000, and the sockets are warmed before entering any store.

What is the expected behavior? Why is that the expected behavior?

Once a request has finished and its socket is back in the agent's free pool, the socket shouldn't keep that request's async context. The Agent binds the handle to the request's context when it reuses the socket, so it should also release that binding when the socket becomes free.

What do you see instead?

Pool size Transport Stores created Idle sockets Retained before agent.destroy() Retained after
4 Normal 20 4 4 0
4 Isolated AsyncResource 20 4 0 0

Retained stores always equal the number of idle sockets: one per socket, the most recent. Pool size 1 gives 1 and 0. The results are the same on all three versions.

Retainer path from a heap snapshot (v24.22.1, weak edges excluded):

(GC roots) -> (Global handles) -> TCP -> AsyncContextFrame -> internal table -> completed ALS store

Cause

asyncResetHandle in lib/internal/http/agent.js is called on both socket-reuse paths (1, 2):

handle.asyncReset(new ReusedHandle(handle.getProviderType(), handle));

asyncReset captures the current AsyncContextFrame on the handle. That's correct while the request is in flight. But nothing resets the handle when the socket returns to freeSockets, so the frame (and every store in it) lives until the socket is reused or destroyed.

This explains all the results above. Retention is one store per idle socket, warming the sockets outside a store doesn't help (the binding happens on reuse), agent.destroy() releases the stores, and --isolated prevents the retention.

Idle-socket events run in the stale context

Because the idle handle keeps the last request's frame, events on an idle socket run with that completed request's store active. Here the listener is attached outside any store, and the socket closes after the request has finished:

import http from "node:http";
import { AsyncLocalStorage } from "node:async_hooks";

const storage = new AsyncLocalStorage();
const agent = new http.Agent({ keepAlive: true, maxSockets: 1 });
const server = http.createServer((req, res) => {
  req.resume();
  req.on("end", () => res.end("ok"));
});
server.keepAliveTimeout = 60_000;
await new Promise((r) => server.listen(0, "127.0.0.1", r));
const url = `http://127.0.0.1:${server.address().port}`;

function request(onSocket) {
  return new Promise((resolve, reject) => {
    const req = http.request(url, { agent }, (res) => {
      res.on("end", resolve);
      res.resume();
    });
    if (onSocket) req.on("socket", onSocket);
    req.on("error", reject);
    req.end();
  });
}

let socket;
await request((s) => {
  socket = s;
});
socket.on("close", () =>
  console.log("idle socket close ->", storage.getStore()),
);

await storage.run({ requestId: "req-1" }, () => request());
console.log("after request, outside store ->", storage.getStore());
await new Promise((r) => setTimeout(r, 50));

server.closeAllConnections();
await new Promise((r) => setTimeout(r, 50));
agent.destroy();
server.close();

Output on v22.23.4, v24.22.2 and v26.13.0:

after request, outside store -> undefined
idle socket close -> { requestId: 'req-1' }

In practice, logs or tracing spans for idle-socket activity get credited to an unrelated, already-finished request.

Possible fix

When a socket is returned to the free pool (the free handler / freeSockets push), reset the handle's context to the empty frame. A full asyncReset would also fire extra destroy/init hooks for async_hooks users, so resetting only the context frame may be the better option.

Additional information

  • Related: #61882, #61995. Those cover contexts retained by reused HTTP parsers. Here the retainer is the live TCP handle, and http.setMaxIdleHTTPParsers(1) doesn't change the result.
  • I first hit this through @smithy/node-http-handler / @aws-sdk/client-dynamodb. Reducing it to node:http reproduces the same behavior.
  • Workaround: start the outgoing request inside an AsyncResource created outside any request context. This is the same idea core already uses with kRequestAsyncResource for queued requests.

anatolzak avatar Nov 01 '26 11:11 anatolzak

Confirmed both reproducers with the official v26.13.0 macOS x64 binary and a local main build (v28.0.0-pre). With 4 idle sockets, 4 stores remained after GC and were collected after agent.destroy(). The idle socket’s close handler also observed the completed request’s store.

The proposed context-only reset when entering the free pool seems worth exploring. It would be useful to clarify what async context idle-socket events should use, while preserving context propagation on reuse and existing async_hooks notifications. The reset should avoid mutating a context still used by other async work. Timeout timers and HTTPS may also need checking for separate retention paths.

inoway46 avatar Nov 01 '26 15:11 inoway46

The issue stems from the lifecycle transition where asyncResetHandle(socket) rebinds the TCP handle to the in-flight request's AsyncContextFrame upon socket reuse, but no cleanup occurs when the connection is reclaimed into freeSockets.

When a request finishes and the socket returns to the free pool, the handle continues holding the most recent request's AsyncContextFrame until the socket is either re-dispatched to a subsequent request or destroyed. This explains why heap retention scales exactly with maxFreeSockets ($O(\text{freeSockets})$) and why lifecycle events on idle sockets (e.g. timeout or remote termination) execute within the previous request's asynchronous store.

A lightweight context reset (clearing the active AsyncContextFrame back to the root/empty frame upon pushing to freeSockets) would decouple the socket from completed request stores without emitting superfluous destroy/init hooks for consumers of the legacy async_hooks API.

helmipradita avatar Nov 02 '26 06:11 helmipradita

can i work on it ?

Ic3b3rg avatar Nov 02 '26 18:11 Ic3b3rg

Of course, contributions don’t require prior permission, so feel free to open a PR!

That said, this is a sensitive area involving async context propagation and async_hooks behavior, so I expect the proposed approach would need some discussion during review. A PR could be a good starting point for that discussion.

inoway46 avatar Nov 03 '26 05:11 inoway46