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

async_hooks: fix leak in AsyncLocalStorage exit #35779

Closed
wants to merge 1 commit into from

Conversation

Qard
Copy link
Member

@Qard Qard commented Oct 23, 2020

If exit is called and then run or enterWith are called within the exit function, the als instance should not be added to the storageList additional times. The correct behaviour is to remove the instance from the storageList before executing the exit handler and then to restore it after.

This PR fixes a memory leak wherein an AsyncLocalStorage instance could be repeatedly inserted to the storageList array which keeps track of active instances to call als._propagate(...) on in the shared async_hooks instance.

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

@Qard Qard added confirmed-bug Issues with confirmed bugs. async_hooks Issues and PRs related to the async hooks subsystem. review wanted PRs that need reviews. request-ci Add this label to start a Jenkins CI on a PR. labels Oct 23, 2020
Copy link
Member

@vdeturckheim vdeturckheim left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot @Qard
I feel like _enable() has been added and removed so many times by now ^^

@Qard
Copy link
Member Author

Qard commented Oct 23, 2020

Yep. The other option I considered was wrapping the exit function in another AsyncResource without the storage slot, which I believe we also had at one point but removed due to people thinking the overhead of another AsyncResource was unnecessary. 😅

@Qard
Copy link
Member Author

Qard commented Oct 23, 2020

Whoops...gotta appease that linter. 😅

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 23, 2020
@nodejs-github-bot

This comment has been minimized.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@puzpuzpuz puzpuzpuz left a comment

Choose a reason for hiding this comment

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

Nice catch!

@codecov-io
Copy link

codecov-io commented Oct 23, 2020

Codecov Report

Merging #35779 into master will increase coverage by 0.22%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master   #35779      +/-   ##
==========================================
+ Coverage   87.92%   88.14%   +0.22%     
==========================================
  Files         477      478       +1     
  Lines      113090   113536     +446     
  Branches    24630    25553     +923     
==========================================
+ Hits        99433   100080     +647     
+ Misses       7952     7698     -254     
- Partials     5705     5758      +53     
Impacted Files Coverage Δ
lib/async_hooks.js 100.00% <100.00%> (ø)
src/connect_wrap.h 25.00% <0.00%> (-75.00%) ⬇️
lib/internal/repl/history.js 88.16% <0.00%> (-4.15%) ⬇️
src/api/utils.cc 28.57% <0.00%> (-2.86%) ⬇️
lib/_http_incoming.js 97.47% <0.00%> (-2.53%) ⬇️
lib/internal/modules/esm/get_source.js 83.33% <0.00%> (-2.39%) ⬇️
lib/internal/dns/utils.js 98.44% <0.00%> (-1.56%) ⬇️
lib/internal/modules/esm/loader.js 86.22% <0.00%> (-1.19%) ⬇️
src/inspector_profiler.cc 76.17% <0.00%> (-1.09%) ⬇️
src/node_worker.cc 76.56% <0.00%> (-0.96%) ⬇️
... and 108 more

@Qard Qard added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 23, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 23, 2020
@nodejs-github-bot

This comment has been minimized.

@Qard
Copy link
Member Author

Qard commented Oct 23, 2020

Seems to me like that codecov result probably shouldn't be getting reported for an incomplete test run. 🤔

@Qard
Copy link
Member Author

Qard commented Oct 23, 2020

CI doesn't want to cooperate. I keep getting seemingly unrelated errors. 🤔

@nodejs-github-bot

This comment has been minimized.

@Trott

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@puzpuzpuz
Copy link
Member

Created #36079 to follow-up the call stack exceeded issue

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot
Copy link
Collaborator

@Flarna Flarna added the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 11, 2020
@github-actions github-actions bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 11, 2020
@github-actions
Copy link
Contributor

Landed in 39a7f76...06f0d78

@github-actions github-actions bot closed this Nov 11, 2020
nodejs-github-bot pushed a commit that referenced this pull request Nov 11, 2020
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@Qard Qard deleted the fix-als-exit branch November 11, 2020 23:19
@Qard
Copy link
Member Author

Qard commented Nov 16, 2020

This should also get backported as any version the AsyncLocalStorage was backported to would also have this issue.

@Flarna Flarna added dont-land-on-v10.x and removed review wanted PRs that need reviews. labels Nov 16, 2020
codebytere pushed a commit that referenced this pull request Nov 22, 2020
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@codebytere codebytere mentioned this pull request Nov 22, 2020
BethGriggs pushed a commit that referenced this pull request Dec 9, 2020
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
BethGriggs pushed a commit that referenced this pull request Dec 10, 2020
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@BethGriggs BethGriggs mentioned this pull request Dec 10, 2020
BethGriggs pushed a commit that referenced this pull request Dec 15, 2020
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
ruyadorno pushed a commit that referenced this pull request Feb 8, 2021
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@ruyadorno ruyadorno mentioned this pull request Feb 9, 2021
ruyadorno pushed a commit that referenced this pull request Feb 10, 2021
If exit is called and then run or enterWith are called within the
exit function, the als instace should not be added to the storageList
additional times. The correct behaviour is to remove the instance
from the storageList before executing the exit handler and then to
restore it after.

PR-URL: #35779
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. confirmed-bug Issues with confirmed bugs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants