Skip to content

http: fix http agent keep alive - #43380

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
theanarkh:fix_http_agent_keep_alive
Jun 18, 2022
Merged

http: fix http agent keep alive#43380
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
theanarkh:fix_http_agent_keep_alive

Conversation

@theanarkh

@theanarkhtheanarkh commented Jun 11, 2022

Copy link
Copy Markdown
Contributor

When options.keepAlive in createSocket function is true it leads to a bug. because createSocket will call this.createConnection(options, oncreate) which will create a socket and set two fields in socket.

this[kSetKeepAlive]=Boolean(options.keepAlive);// truethis[kSetKeepAliveInitialDelay]=~~(options.keepAliveInitialDelay/1000);// 0

this[kSetKeepAliveInitialDelay] will be 0 because options.keepAliveInitialDelay is undefined. When the connection is finished, afterConnect will be called and use this two fields, the related code is as follow.

if(self[kSetKeepAlive]&&self._handle.setKeepAlive){self._handle.setKeepAlive(true,self[kSetKeepAliveInitialDelay]);}

It calls setKeepAlive with 0 (self[kSetKeepAliveInitialDelay]).

Then when the free event of agent is emitted, agent will call setKeepAlive in keepSocketAlive, the code is as follow.

// enable is true and this[kSetKeepAlive] is true tooif(this._handle.setKeepAlive&&enable!==this[kSetKeepAlive]){this[kSetKeepAlive]=enable;this[kSetKeepAliveInitialDelay]=initialDelay;this._handle.setKeepAlive(enable,initialDelay);}

enable !== this[kSetKeepAlive] will return false, so the agent do nothing which lead to a bug.

Currently http agent only set keepalive on some sockets (when free event is emitted). Maybe we can set keepalive for all sockets ? otherwise i think we should delete the keepAlive field of options before call this.createConnection in createSocket.

Refs: #41965.

  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Affected subsystem: http

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-botnodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Jun 11, 2022
@theanarkh
theanarkhforce-pushed the fix_http_agent_keep_alive branch from 565464c to 8eaff1eCompareJune 11, 2022 12:42
@mscdex

Copy link
Copy Markdown
Contributor

This needs a test

@theanarkh
theanarkhforce-pushed the fix_http_agent_keep_alive branch 2 times, most recently from 99536b6 to 95d6aadCompareJune 11, 2022 15:59
Comment threadtest/parallel/test-http-agent-keepalive.js Outdated
@theanarkh
theanarkhforce-pushed the fix_http_agent_keep_alive branch from 95d6aad to 9ed9d4dCompareJune 11, 2022 18:21

@ShogunPandaShogunPanda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@mcollinamcollina 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

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment threadtest/parallel/test-http-agent-keepalive-delay.js Outdated
Comment threadtest/parallel/test-http-agent-keepalive-delay.js Outdated
@theanarkh
theanarkhforce-pushed the fix_http_agent_keep_alive branch from 9ed9d4d to 0d1ad29CompareJune 13, 2022 16:29
@theanarkh
theanarkhforce-pushed the fix_http_agent_keep_alive branch from 0d1ad29 to aeadf39CompareJune 14, 2022 02:05
@rickyesrickyes added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 14, 2022
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 14, 2022
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@theanarkh

Copy link
Copy Markdown
ContributorAuthor

@ShogunPanda Hi, can you help trigger CI again ? Thanks !

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollinamcollina added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed needs-ci PRs that need a full CI run. labels Jun 18, 2022
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 18, 2022
@nodejs-github-bot
nodejs-github-bot merged commit fe776b8 into nodejs:mainJun 18, 2022
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in fe776b8

targos pushed a commit that referenced this pull request Jul 12, 2022
PR-URL: #43380
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
@targostargos mentioned this pull request Jul 12, 2022
targos pushed a commit that referenced this pull request Jul 18, 2022
PR-URL: #43380
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
targos pushed a commit that referenced this pull request Jul 31, 2022
PR-URL: #43380
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
@targostargos mentioned this pull request Aug 3, 2022
guangwong pushed a commit to noslate-project/node that referenced this pull request Oct 10, 2022
PR-URL: nodejs/node#43380
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.commit-queue-squashAdd this label to instruct the Commit Queue to squash all the PR commits into the first one.httpIssues or PRs related to the http subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@theanarkh@nodejs-github-bot@mscdex@mcollina@ShogunPanda@jasnell@trivikr@rickyes