Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https: fix memory leak with https.request() #8647

Closed
wants to merge 1 commit into from

Conversation

imyller
Copy link
Member

@imyller imyller commented Sep 18, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

https

Description of change

If calling https.request() with options.headers.host defined and options.servername undefined, https.Agent.createSocket mutates connection options after https.Agent.addRequest has created empty socket pool array with mismatching connection name.

This results in two socket pool arrays being created and only the last one gets eventually deleted by removeSocket - effectively causing a memory leak.

This commit fixes the leak by making sure that addRequest does the same modifications to options object as the createSocket.

createSocket is intentionally left unmodified to prevent userland regressions.

Test case included.

Fixes: #6687

/cc @nodejs/http @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added the http Issues or PRs related to the http subsystem. label Sep 18, 2016
@imyller imyller added memory Issues and PRs related to the memory management or memory footprint. https Issues or PRs related to the https subsystem. labels Sep 18, 2016
@imyller
Copy link
Member Author

imyller commented Sep 18, 2016

options.servername = options.host;
const hostHeader = req.getHeader('host');
if (hostHeader) {
options.servername = hostHeader.replace(/:.*$/, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer just using hostHeader.slice() and hostHeader.indexOf(), but if others are not comfortable with that, at the very least the regex should be /:[\s\S]*$/ to match any character (including newlines).

Copy link
Member Author

Choose a reason for hiding this comment

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

I left the hostHeader cleanup intentionally unmodified to match the original feature implemented PR #1110

I did not want to fix two unrelated issues in single PR dedicated to memory leak.

ca: options.ca,
agent: agent
}, common.mustCall((res) => {

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove the extra blank lines at lines 37, 44, 48, 54, and 56.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Extra blank lines removed

@indutny
Copy link
Member

indutny commented Sep 18, 2016

LGTM

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

LGTM

@JacksonTian
Copy link
Contributor

LGTM

@imyller
Copy link
Member Author

imyller commented Sep 18, 2016

FreeBSD CI failed due to force pushed commit

New CI run: https://ci.nodejs.org/job/node-test-pull-request/4108/

@imyller
Copy link
Member Author

imyller commented Sep 18, 2016

I'm going to add lts-agenda label because this leak affects current LTS release too.
Feel free to remove if fixing this in LTS is not relevant.

@imyller
Copy link
Member Author

imyller commented Sep 19, 2016

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

LGTM

// To verify that no leaks occur, check that no entries
// exist in agent.sockets pool after both request and socket
// has been closed.

Copy link
Member

Choose a reason for hiding this comment

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

I think this line should also be removed? #8647 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Removed extra line.

If calling `https.request()` with `options.headers.host` defined
and `options.servername` undefined, `https.Agent.createSocket` mutates
connection `options` after `https.Agent.addRequest` has created empty
socket pool array with mismatching connection name. This results in two
socket pool arrays being created and only the last one gets eventually
deleted by `removeSocket` - causing a memory leak.

This commit fixes the leak by making sure that `addRequest` does the
same modifications to `options` object as the `createSocket`.

`createSocket` is intentionally left unmodified to prevent userland
regressions.

Test case included.

Fixes: nodejs#6687
@jasnell
Copy link
Member

jasnell commented Sep 20, 2016

Trying CI again due to the AIX error: https://ci.nodejs.org/job/node-test-pull-request/4179/
Seems unrelated but just to be safe...

@imyller imyller self-assigned this Sep 20, 2016
@imyller
Copy link
Member Author

imyller commented Sep 20, 2016

If CI is green, I'll land this.

@imyller
Copy link
Member Author

imyller commented Sep 21, 2016

I'll start landing this:

  • Three Four LGTMs
  • No objections
  • Requested modifications have been made
  • CI tests passed (only unrelated CI failures)

imyller added a commit to imyller/node that referenced this pull request Sep 21, 2016
If calling `https.request()` with `options.headers.host` defined
and `options.servername` undefined, `https.Agent.createSocket` mutates
connection `options` after `https.Agent.addRequest` has created empty
socket pool array with mismatching connection name. This results in two
socket pool arrays being created and only the last one gets eventually
deleted by `removeSocket` - causing a memory leak.

This commit fixes the leak by making sure that `addRequest` does the
same modifications to `options` object as the `createSocket`.

`createSocket` is intentionally left unmodified to prevent userland
regressions.

Test case included.

PR-URL: nodejs#8647
Fixes: nodejs#6687
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@imyller
Copy link
Member Author

imyller commented Sep 21, 2016

landed in db5a879

@imyller imyller closed this Sep 21, 2016
@imyller imyller removed their assignment Sep 21, 2016
@MylesBorins
Copy link
Contributor

@imyller I'm removing the agenda label for right now as there does not appear to be anything controversial about this change. After it has lived in a release for at least two weeks we can backport

Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
If calling `https.request()` with `options.headers.host` defined
and `options.servername` undefined, `https.Agent.createSocket` mutates
connection `options` after `https.Agent.addRequest` has created empty
socket pool array with mismatching connection name. This results in two
socket pool arrays being created and only the last one gets eventually
deleted by `removeSocket` - causing a memory leak.

This commit fixes the leak by making sure that `addRequest` does the
same modifications to `options` object as the `createSocket`.

`createSocket` is intentionally left unmodified to prevent userland
regressions.

Test case included.

PR-URL: #8647
Fixes: #6687
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Nov 18, 2016
If calling `https.request()` with `options.headers.host` defined
and `options.servername` undefined, `https.Agent.createSocket` mutates
connection `options` after `https.Agent.addRequest` has created empty
socket pool array with mismatching connection name. This results in two
socket pool arrays being created and only the last one gets eventually
deleted by `removeSocket` - causing a memory leak.

This commit fixes the leak by making sure that `addRequest` does the
same modifications to `options` object as the `createSocket`.

`createSocket` is intentionally left unmodified to prevent userland
regressions.

Test case included.

PR-URL: #8647
Fixes: #6687
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Nov 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. https Issues or PRs related to the https subsystem. memory Issues and PRs related to the memory management or memory footprint.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

https.request(): Agent name inconsistency when servername not supplied
10 participants