Commit edea6da44ff for nodejs
commit edea6da44ff968827bc4b3a7e2f20a4b33d5e027
Author: Orgad Shaneh <orgad.shaneh@audiocodes.com>
Date: Wed Dec 2 22:17:26 2026 +0200
https: keep session listeners from pinning requests
createConnection() adds 'session' and 'close' listeners that live as
long as the socket. As closures created in createConnection(), they
share its context, which also holds `cb` for the proxy tunnel path.
That callback leads to the request that created the socket, so every
keep-alive socket kept its first request, and whatever the request's
listeners capture, reachable until the socket closed.
Add the listeners from a separate function, so that they capture only
the agent, the socket and the session key. Plain http is unaffected.
The CONNECT proxy path had the same problem: the 'free' listener on
the tunneled socket was created in establishTunnel(), whose context
holds `afterSocket`. Move it to a separate function as well.
Assisted-by: Claude Code
Signed-off-by: Orgad Shaneh <orgad.shaneh@audiocodes.com>
PR-URL: https://github.com/nodejs/node/pull/67328
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
diff --git a/lib/https.js b/lib/https.js
index 7481439bf1b..5e5ed04efe8 100644
--- a/lib/https.js
+++ b/lib/https.js
@@ -223,6 +223,16 @@ function getTunnelConfigForProxiedHttps(agent, reqOptions) {
return result;
};
+// Not inlined in establishTunnel(): this listener lives as long as the
+// tunneled socket, and closures created there share a context that holds
+// `afterSocket`, which leads to the request that created the socket.
+function propagateFree(tunneldSocket, socket) {
+ tunneldSocket.on('free', () => {
+ debug('Propagate free event from tunneled socket to tunnel socket');
+ socket.emit('free');
+ });
+}
+
function establishTunnel(agent, socket, options, tunnelConfig, afterSocket) {
const { proxyTunnelPayload, maxHeaderSize } = tunnelConfig;
// By default, the socket is in paused mode. Read to look for the 200
@@ -315,10 +325,7 @@ function establishTunnel(agent, socket, options, tunnelConfig, afterSocket) {
}
if (requestOptions[kPerRequestCheckServerIdentity])
tunneldSocket[kPerRequestCheckServerIdentity] = true;
- tunneldSocket.on('free', () => {
- debug('Propagate free event from tunneled socket to tunnel socket');
- socket.emit('free');
- });
+ propagateFree(tunneldSocket, socket);
tunneldSocket.on('error', onTLSHandshakeError);
}
return true;
@@ -455,22 +462,28 @@ function createConnection(...args) {
if (options[kPerRequestCheckServerIdentity])
socket[kPerRequestCheckServerIdentity] = true;
- if (reuseSession) {
- // Cache new session for reuse
- socket.on('session', (session) => {
- this._cacheSession(options._agentKey, session);
- });
-
- // Evict session on error
- socket.once('close', (err) => {
- if (err)
- this._evictSession(options._agentKey);
- });
- }
+ if (reuseSession)
+ cacheSessions(this, socket, options._agentKey);
return socket;
}
+// Not inlined in createConnection(): these listeners live as long as the
+// socket, and closures created there share a context that holds `cb`, which
+// leads to the request that created the socket.
+function cacheSessions(agent, socket, key) {
+ // Cache new session for reuse
+ socket.on('session', (session) => {
+ agent._cacheSession(key, session);
+ });
+
+ // Evict session on error
+ socket.once('close', (err) => {
+ if (err)
+ agent._evictSession(key);
+ });
+}
+
/**
* Creates a new `HttpAgent` instance.
* @param {{
diff --git a/test/client-proxy/test-https-proxy-request-collectable.mjs b/test/client-proxy/test-https-proxy-request-collectable.mjs
new file mode 100644
index 00000000000..1b582d84363
--- /dev/null
+++ b/test/client-proxy/test-https-proxy-request-collectable.mjs
@@ -0,0 +1,51 @@
+// Flags: --expose-gc
+// A keep-alive socket tunneled through a proxy must not keep the request
+// that created it reachable once it is back in the agent's free pool.
+
+import * as common from '../common/index.mjs';
+import assert from 'node:assert';
+import { once } from 'events';
+import { createProxyServer } from '../common/proxy-server.js';
+import fixtures from '../common/fixtures.js';
+import { gcUntil } from '../common/gc.js';
+
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+
+// https must be dynamically imported so that builds without crypto support
+// can skip it.
+const { default: https } = await import('node:https');
+
+const server = https.createServer({
+ cert: fixtures.readKey('agent8-cert.pem'),
+ key: fixtures.readKey('agent8-key.pem'),
+}, common.mustCall((req, res) => res.end('ok')));
+server.listen(0);
+await once(server, 'listening');
+
+const { proxy } = createProxyServer();
+proxy.listen(0);
+await once(proxy, 'listening');
+
+const agent = new https.Agent({
+ keepAlive: true,
+ proxyEnv: {
+ HTTPS_PROXY: `http://localhost:${proxy.address().port}`,
+ },
+ ca: fixtures.readKey('fake-startcom-root-cert.pem'),
+});
+
+const ref = new WeakRef(https.get({
+ hostname: 'localhost',
+ port: server.address().port,
+ agent,
+}, common.mustCall((res) => res.resume())));
+
+await once(agent, 'free');
+await new Promise(setImmediate);
+assert.strictEqual(Object.keys(agent.freeSockets).length, 1);
+await gcUntil('the first request is collected',
+ () => ref.deref() === undefined);
+agent.destroy();
+proxy.close();
+server.close();
diff --git a/test/parallel/test-https-agent-keepalive-request-collectable.js b/test/parallel/test-https-agent-keepalive-request-collectable.js
new file mode 100644
index 00000000000..fad011748f6
--- /dev/null
+++ b/test/parallel/test-https-agent-keepalive-request-collectable.js
@@ -0,0 +1,39 @@
+// Flags: --expose-gc
+'use strict';
+const common = require('../common');
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+
+// A keep-alive socket in the agent's free pool must not keep the request
+// that created it reachable.
+
+const assert = require('assert');
+const https = require('https');
+const fixtures = require('../common/fixtures');
+const { gcUntil } = require('../common/gc');
+
+const server = https.createServer({
+ key: fixtures.readKey('agent1-key.pem'),
+ cert: fixtures.readKey('agent1-cert.pem'),
+}, (req, res) => res.end('ok'));
+
+server.listen(0, common.mustCall(() => {
+ const agent = new https.Agent({ keepAlive: true });
+ let ref;
+
+ agent.once('free', common.mustCall(() => {
+ setImmediate(common.mustCall(async () => {
+ assert.strictEqual(Object.keys(agent.freeSockets).length, 1);
+ await gcUntil('the first request is collected',
+ () => ref.deref() === undefined);
+ agent.destroy();
+ server.close();
+ }));
+ }));
+
+ ref = new WeakRef(https.get({
+ port: server.address().port,
+ agent,
+ rejectUnauthorized: false,
+ }, common.mustCall((res) => res.resume())));
+}));