Skip to content

https: keep session listeners from pinning requests - #67328

Open
orgads wants to merge 1 commit into
nodejs:mainfrom
orgads:https-agent-session-retention
Open

orgads wants to merge 1 commit into
nodejs:mainfrom
orgads:https-agent-session-retention

Conversation

@orgads

@orgads orgads commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

The https Agent's createConnection() adds 'session' and 'close' listeners that live as long as the socket. As closures created in createConnection(), they share its V8 context. That context also holds cb, which the proxy-tunnel path captures, and cb leads to the request that created the socket. So every keep-alive TLS socket kept its first request reachable until the socket closed, along with everything the request's listeners capture.

 createConnection(...args)
   ...
-  socket.on('session', (session) => this._cacheSession(options._agentKey, session))
-  socket.once('close', (err) => { if (err) this._evictSession(options._agentKey) })
+  cacheSessions(this, socket, options._agentKey)   // closures capture only these

How this shows up in an application: a voice server sends TTS requests through a keep-alive https agent, and its response listeners close over the call that made the request. Under steady load the pooled sockets are never idle, so each one held one finished call. That was 259 finished calls at 800 concurrent calls. Plain http doesn't do this.

New test test/parallel/test-https-agent-keepalive-request-collectable.js: after the first request completes and its socket goes back to the free pool, the request must be garbage-collectable.

  • Without the fix, gcUntil fails.
  • With the fix, it passes.
  • The same check over plain http already passes on main.

tools/test.py on parallel/test-{https,tls,http-agent,http2}-*: 628 passed and 4 failed. The same 4 fail on main in my environment (system CA, getaddrinfo ENOTFOUND localhost).

🤖 Generated with Claude Code

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added https Issues and PRs related to the https subsystem. needs-ci PRs that need a full CI run. labels Nov 7, 2026
@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.70%. Comparing base (bd08ba3) to head (e3d6b4d).
⚠️ Report is 21 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67328   +/-   ##
=======================================
  Coverage   90.70%   90.70%           
=======================================
  Files         802      802           
  Lines      282290   282296    +6     
  Branches    54406    54411    +5     
=======================================
+ Hits       256052   256064   +12     
+ Misses      16354    16348    -6     
  Partials     9884     9884           
Files with missing lines Coverage Δ
lib/https.js 96.91% <100.00%> (+0.02%) ⬆️

... and 33 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pimterry pimterry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find! LGTM

@pimterry pimterry added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 12, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 12, 2026
@nodejs-github-bot

This comment was marked as outdated.

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@gurgunday gurgunday added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Nov 12, 2026
@pimterry pimterry added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 13, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 13, 2026
@nodejs-github-bot

This comment was marked as outdated.

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.

Assisted-by: Claude Code
Signed-off-by: Orgad Shaneh <orgad.shaneh@audiocodes.com>
@orgads
orgads force-pushed the https-agent-session-retention branch from 7d2dce4 to e3d6b4d Compare November 14, 2026 20:46
@orgads

orgads commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Rebased.

Comment thread lib/https.js
Comment on lines 319 to 322
tunneldSocket.on('free', () => {
debug('Propagate free event from tunneled socket to tunnel socket');
socket.emit('free');
});

@gurgunday gurgunday Nov 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after a second check... I think the CONNECT proxy path still has the same leak

this listener lives as long as tunneldSocket and its closure keeps the context alive

we might need a module level helper such as:

function propagateFree(tunneldSocket, socket) {
  tunneldSocket.on('free', () => {
    debug('Propagate free event from tunneled socket to tunnel socket');
    socket.emit('free');
  });
}

I'd prefer if you fixed it in this PR as well

@gurgunday gurgunday removed the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Nov 15, 2026
@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

https Issues and PRs related to the https subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants